Thread: C++ newbie needs help with simple conversion programming

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    4

    C++ newbie needs help with simple conversion programming

    Hi, I'm trying to make a program that converts Fahrenheit into Celsius and Kelvin.

    #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;
    }
    Last edited by djayyyyy; 10-16-2010 at 10:43 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You either need to add
    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

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also do not
    Code:
    fflush(stdin);
    Jim

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    And to quibble, zero degrees celcius is 273.15 kelvin
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    Thanks for the replies!

    But I get another error in next line

    #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;
    }

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    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);

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    Quote Originally Posted by QuadraticFighte View Post
    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

    #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;
    }
    Last edited by djayyyyy; 10-17-2010 at 02:12 PM.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    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.

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    Quote Originally Posted by QuadraticFighte View Post
    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.
    SWEEEET. THANK YOU SOO MUCH for helping this newbie out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MFC newbie --- simple problem
    By gemini_shooter in forum Windows Programming
    Replies: 3
    Last Post: 04-03-2006, 04:17 PM
  2. Newbie Help: Currency Converter
    By Ashfury in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 01:21 PM
  3. Configurations give different results
    By Hubas in forum Windows Programming
    Replies: 2
    Last Post: 04-11-2003, 11:43 AM
  4. Need some really simple help (complete Linux newbie)
    By Hannwaas in forum Linux Programming
    Replies: 11
    Last Post: 12-10-2001, 03:16 PM
  5. Im a Newbie with a graphics design problem for my simple game
    By Robert_Ingleby in forum C++ Programming
    Replies: 1
    Last Post: 11-23-2001, 06:41 PM