Thread: a lil help with a if else statement please

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    4

    Wink a lil help with a if else statement please

    Ok. i am trying to write andd if else statement , so that a user can choose if he wants to convert celcius to farenhiet (spelling?) or the other way around. I can only get the conversion to go one way, i cannot get the else statment to recognized. could some one please explain to me why no matter what i put in it will always do the if statement? here is my code
    Code:
    #include <iostream>
    using namespace std;
    
    float convert(float);
    float convert2(float);
    
    
    int main()
    {
    float tempfar;
    float tempcel;
    char f = 1;
    char c = 2;
    char answer;
    
    
    
    cout<<"would you like convert to Farenhiet(1) or to celcius(2)? ";
    cin>>answer;
    cin.ignore();
    
                 if (answer > 1)
                 {
                            cout<<"Please enter the temp in Farenhiet: \n";
                            cin>>tempfar;
                            cin.ignore();
                            tempcel = convert(tempfar);
                            cout<<"The temp converted to celcius is : ";
                            cout<< tempcel << endl;
                            
                            }
                            
                            else
                            cout<<"Please enter the temp in celcius: \n";
                            cin>>tempcel;
                            cin.ignore();
                            tempfar = convert2(tempcel);
                            cout<<"Here is youre answer in farenheit: ";
                            cout<< tempfar << endl;
                            cin.get();
                            
    }
    
    float convert (float far)
    {
          float cel;
          cel = ((far - 32) * 5) / 9;
          return cel;
          
          }
          
    float convert2 (float cel)
    {
          float far;
          far=((cel*9) / 9) + 32;
          return far;
          
    }

    any help will be appreciated and any tips on coding also i have only been coding for about 3 days
    Last edited by cam123666; 02-05-2005 at 10:05 AM.

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Code:
    //...
    int answer;
    
    //...
    
                 if (answer == 1)
                 {
                            cout<<"Please enter the temp in Farenhiet: \n";
                            cin>>tempfar;
                            cin.ignore();
                            tempcel = convert(tempfar);
                            cout<<"The temp converted to celcius is : ";
                            cout<< tempcel << endl;
                            
                  }else if(answer ==2) {
                            cout<<"Please enter the temp in celcius: \n";
                            cin>>tempcel;
                            cin.ignore();
                            tempfar = convert2(tempcel);
                            cout<<"Here is youre answer in farenheit: ";
                            cout<< tempfar << endl;
                            cin.get();
                 }
    You were asking for a char return value vice an int for answer and the character value of '1' is indeed > 1. I also made a little change with your if else chain.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  3. #3
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    I improved your code some

    Code:
    #include <iostream>
    
    using namespace std;
    
    float convert (float far1);
    float convert2 (float cel);
    
    int main()
    {
        float tempfar;
        float tempcel;
        char f = 1;
        char c = 2;
        int answer;
        cout<<"would you like convert to Farenhiet(1) or to celcius(2)? ";
        cin >> answer;
        if (answer == 2)
        {
            cout<<"Please enter the temp in Farenhiet: \n";
            cin>>tempfar;
            cin.ignore();
            tempcel = convert(tempfar);
            cout<<"The temp converted to celcius is : ";
            cout<< tempcel << endl;           
        }         
        else
        {
            cout<<"Please enter the temp in celcius: \n";
            cin>>tempcel;
            cin.ignore();
            tempfar = convert2(tempcel);
            cout<<"Here is youre answer in farenheit: ";
            cout<< tempfar << endl;
            cin.get();
        }
        return 0;
    }
    
    float convert (float far1)
    {
        float cel2;
        cel2 = ((far1 - 32) * 5) / 9;
        return cel2;
    }
    
    float convert2 (float cel)
    {
        float far2;
        far2=((cel*9) / 9) + 32;
        return far2;
    }
    EDIT
    Two things to remember are that int main() needs to return a value, and use tabs rather than spaces for better looking code
    Last edited by MadCow257; 02-05-2005 at 10:10 AM.

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    4
    ahh yes thank you very much.

    ok so else if w/ a definition from now on instead of just and else .

    thanks for the help i am just learning about if else statements

    ok i will remember ty very much
    Last edited by cam123666; 02-05-2005 at 10:17 AM.

  5. #5
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    EDIT
    Two things to remember are that int main() needs to return a value,
    We just had a discussion about this on this thread. I do agree with you in that I prefer to explicitly return a value from main() but according to the standard
    Originally Posted by Bjarne Stroustrup
    In C++, main() need not contain an explicit return statement. In that case, the value returned is 0, meaning successful execution. For example:

    Code:
    Code:
        #include<iostream>
    
        int main()
        {
            std::cout << "This program returns the integer value 0\n";
        }
    So as you can see in C++, the return value is implicit. If you do not explicitly use a return value a return of 0 is implied.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 07:18 AM
  3. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM
  4. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 09:56 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM