the following is the code...
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
float start_temp,stop_temp;
float step,F,C;
cout<<"start temperature:";
cin>>start_temp;
cout<<"stop temperature:";
cin>>stop_temp;
cout<<"step temperature:";
cin>>step;
float i=0;
cout<<setw(10)<<"celsius"<<setw(15)<<"fahrenheit"<<endl;
cout<<setw(25)<<"celsius"<<setw(15)<<"fahrenheit"<<endl;
for(i=start_temp;i<=stop_temp;i=i+step)
{
C=(5.0/9.0)*(i-32);
F=32+(9.0/5.0)*i;
cout<<setprecision(2);
cout.setf(ios::fixed);
cout<<setw(10)<<C<<setw(10)<<i<<setw(10)<<F<<endl;

}

}
and the output for the above code is:

start temperature: 0
stop temperature: 11
step temperature: 1.1

-17.78 0.00 32.00
-17.17 1.10 33.98
-16.56 2.20 35.96
-15.94 3.30 37.94
-15.33 4.40 39.92
-14.72 5.50 41.90
-14.11 6.60 43.88
-13.50 7.70 45.86
-12.89 8.80 47.84
-12.28 9.90 49.82
-11.67 11.00 51.80

but i am not able to print the last line of output.i.e.,-11.67 11.00 51.80
i dont know why it is not printing the last line of output..

that simple: floating point values on a computer always approximate the real value to some round-off error (about to the 7th significant digit for float and to the 20th significant digit for double). So the comparison i<=stop_temp is not going to work all the time because even when i is 11, it might actually be 11.0000034 or something like that and thus, not equal to stop_temp (which also will not necessarily be exactly 11 either).

So to fix the problem, use an integer-valued iterator, like this:

int iter = ceil((stop_temp - start_temp) / step); //ceil will get the smallest integer value that is greater than "(stop_temp - start_temp)/step".
  for(i=start_temp; iter>=0 ;iter--,i=i+step)
  {
    C=(5.0/9.0)*(i-32);
    F=32+(9.0/5.0)*i;
    cout<<setprecision(2);
    cout.setf(ios::fixed);
    cout<<setw(10)<<C<<setw(10)<<i<<setw(10)<<F<<endl;

  }

EDIT: ceil is found in <cmath> by the way. (so you need also #include <cmath> at the start)

hi thanks for your reply...
but when i used ceil as integer.. it showed me an error.. so i declared iter as float...
because all the variables in the program are float...
but it only worked for the input...
input:
start temperature: 0
stop temperature: 11
step temperature: 1.1
output:
-17.78 0.00 32.00
-17.17 1.10 33.98
-16.56 2.20 35.96
-15.94 3.30 37.94
-15.33 4.40 39.92
-14.72 5.50 41.90
-14.11 6.60 43.88
-13.50 7.70 45.86
-12.89 8.80 47.84
-12.28 9.90 49.82
-11.67 11.00 51.80

but it did not work for the following input...
input:
start temperature: 30
stop temperature: 60
step temperature: 3.5
output:
-1.11 30.00 86.00
0.83 33.50 92.30
2.78 37.00 98.60
4.72 40.50 104.90
6.67 44.00 111.20
8.61 47.50 117.50
10.56 51.00 123.80
12.50 54.50 130.10
14.44 58.00 136.40

extra lines of output are displayed for the above input.. the extra line is 16.39 61.50 142.70

You replaced one problem with the same problem. The main point with my solution was that you cannot do an exact number of iterations with a float because float is not exact, regardless of how you put it. You need to use an integer value to count the iterations, not a float.

If the ceil function gave an error like "cannot convert double to int" then use "int iter = int(ceil(..));" to explicitly cast to int.

yes i type casted in to int..
int iter=(int)ceil((stop_temp-start_temp)/step)
but still i am getting an extra line for the second set of input...

To get rid of the extra line, use this instead:

int iter = (int)floor((stop_temp - start_temp + step / 2.0) / step);

nope its not working still..:(((((((((((((((((
i am still getting the extra line for the last set of inputs....

floats can be used as a counter, but you must be very careful in comparing them because they are approximations of values and have inaccuracy where two values that seem the same are not exactly the same. A fuzzy comparison is usually used for floating point. Here is your code with the fuzzy comparisons added.

#include<iostream>
#include<iomanip>
#include<limits>
#include<math.h>
using namespace std;

inline bool less_than(float lhs, float rhs) {
    float sig = fabs(lhs) < fabs(rhs) ? fabs(rhs) : fabs(lhs);
    return rhs-lhs > sig * numeric_limits<float>::epsilon();
}

inline bool equal_to(float lhs, float rhs) {
    float sig = fabs(lhs) < fabs(rhs) ? fabs(rhs) : fabs(lhs);
    return fabs(lhs-rhs) <= sig * numeric_limits<float>::epsilon();
}

inline bool less_or_equal(float lhs, float rhs) {
    return less_than(lhs, rhs) || equal_to(lhs, rhs);
}

int main() {
    float start_temp,stop_temp;
    float step,F,C;
    cout<<"start temperature:";
    cin>>start_temp;
    cout<<"stop temperature:";
    cin>>stop_temp;
    cout<<"step temperature:";
    cin>>step;
    float i=0;
    cout<<setw(10)<<"celsius"<<setw(15)<<"fahrenheit"<<endl;
    cout<<setw(25)<<"celsius"<<setw(15)<<"fahrenheit"<<endl;
    for(i=start_temp; less_or_equal(i, stop_temp); i=i+step) {
        C=(5.0/9.0)*(i-32);
        F=32+(9.0/5.0)*i;
        cout<<setprecision(2);
        cout.setf(ios::fixed);
        cout<<setw(10)<<C<<setw(10)<<i<<setw(10)<<F<<endl;
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.