C++ newbie needs help with simple conversion programming

• 10-16-2010
djayyyyy
C++ newbie needs help with simple conversion programming
Hi, I'm trying to make a program that converts Fahrenheit into Celsius and Kelvin.

Quote:

#include <iostream>
#include <cmath>
#include <cstdlib>

int main(int argc, char *argv[])
{
double fah, cel, kel;
char ans;
do
{
cout<<"Enter the temperature in Fahrenheit: "; // I get error here..."cout" undeclared
cin>>fah;

cel = (5.0 / 9.0) * (fah - 32.0);
kel = (cel + 273.0);

cout<<"The temperature is "<<cel<<" degree Celsius and "<<kel<<" Kelvin."<<endl;
cout<<endl<<endl
<<"Do you want to do this again? ";
cin>>ans;
fflush(stdin);
}while(ans=='Y' || ans == 'y');
system("PAUSE");
return EXIT_SUCCESS;
}
• 10-16-2010
jimblumberg
Code:

`using namespace std;`
before main() or use
Code:

```std::cout<<"Enter the temperature in Fahrenheit: "; std::cin>>fah;```
and all other things in the std namespace.

Jim
• 10-16-2010
jimblumberg
Also do not
Code:

`fflush(stdin);`
Jim
• 10-17-2010
grumpy
And to quibble, zero degrees celcius is 273.15 kelvin
• 10-17-2010
djayyyyy
Thanks for the replies! :)

But I get another error in next line

Quote:

#include <iostream>
#include <cmath>
#include <cstdlib>

int main(int argc, char *argv[])
{
double fah, cel, kel;
char ans;
do
{
cout<<"Enter the temperature in Fahrenheit: ";
cin>>fah; // I get error here now... it says expected 'while' '(' before 'cin' and ')' before ';'

cel = (5.0 / 9.0) * (fah - 32.0);
kel = (cel + 273.0);

cout<<"The temperature is "<<cel<<" degree Celsius and "<<kel<<" Kelvin."<<endl;
cout<<endl<<endl
<<"Do you want to do this again? ";
cin>>ans;
}while(ans=='Y' || ans == 'y');
system("PAUSE");
return EXIT_SUCCESS;
}
• 10-17-2010
You code ran just fine for me. I have no idea what you're talking about. you might add using namespace std somewhere. Also, I have no such program as "PAUSE", so there was a runtime error on that. If you just want it to wait a second before exiting, you can sleep(1000);
• 10-17-2010
djayyyyy
Quote:

You code ran just fine for me. I have no idea what you're talking about. you might add using namespace std somewhere. Also, I have no such program as "PAUSE", so there was a runtime error on that. If you just want it to wait a second before exiting, you can sleep(1000);

What I wanted to do is ask the person if he/she wants to try another conversion.
And maybe I might have messed up something while I was copying and pasting and fixing stuff at the same time...

I'll try to program this from scratch.

----------------------------------------------------------EDIT---------------------------------------------------------------------

Okay I started the program from scratch again and I get no errors and its perfectly fine until
I get to the last part where it asks me if I want to do it again. This is what happens http://img202.imageshack.us/img202/1919/11111lj.png

Quote:

#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

int main(int argc, char *argv[])
{
char ans;
double f,c,k;
{
cout<<"Enter the temperature in Fahrenheit: ";
cin>>f;

c = (5.0 / 9.0) * (f - 32.0);
k = (c + 273.15);

cout<<"The temperature is "<<c<<" degree Celsius and "<<k<<" Kelvin."<<endl;

cout<<"Do you want to do this again? ";
cin>>ans;
}while(ans=='Y' || ans == 'y');
system("PAUSE");
return EXIT_SUCCESS;
}
• 10-17-2010
That's because you left out the do in the do while loop. Right before your bracket after the line double f,c,k; On my text editor, it's line 12. What was happening was without the do, you just had a block of code and then

Code:

`while(ans=='Y' || ans == 'y');`
This is a valid C statement which means "while ans is either y or Y do nothing" So you were stuck in an infinite loop.
• 10-17-2010
djayyyyy
Quote:

`while(ans=='Y' || ans == 'y');`