iffy if else...I'm stumped

This is a discussion on iffy if else...I'm stumped within the C++ Programming forums, part of the General Programming Boards category; I have been going over and over this, and for the life of me I can't figure out what I'm ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    100

    iffy if else...I'm stumped

    I have been going over and over this, and for the life of me I can't figure out what I'm doing wrong. All this is supposed to do is take a number and a letter, determine weather to use celsius or fahrenheit conversion (or neither) then run the conversion five times. this is the code:

    Code:
        #include <iostream>
        #include <iomanip>
        using namespace std;
        float getinfo (float&, char&);
        float selscale (char&,int&,float&,float,float);
        float celscl (int, float&, float);
        float calccel ( float&,float);
        float fahscl(int, float&, float);
        float calcfah (float&, float);
        void CnorF();
        
    int main ()
    {
        char scl;
        float deg;
        int count;
        float degC;
        float degF;
        getinfo (deg, scl);
        selscale (scl,count,deg,degF,degC);
        celscl (count,deg,degF);
        calccel ( deg, degF);
        fahscl(count, deg,degC);
        calcfah (deg, degC);
        CnorF();
        
        system ("pause");
        return 0;
    }
    
    float getinfo (float& deg, char& scl)
    {
        cout<<"Please enter the temperature followed by C or F (celsius or fahrenheit):"
            <<endl;
        cin>>deg;   //temperature
        cin>>scl;    //scale (celsius or fahrenheit)
        
    }
    float selscale (char& scl,int& count,float& deg,float degF, float degC)
    {  
        if (scl == 'c'|| scl == 'C')         //goes to celsius
            {
            
             celscl(count,deg,degF);
             calccel (deg,degF);
            }
         
            else if(scl == 'f'|| scl == 'F')   //goes to fahrenheit
            {
            
            fahscl(count,deg,degC);
            calcfah (deg,degC);
            }
            else
            {
            CnorF ();                //goes to error message
            }
    }
    float celscl (int count,float& deg,float degF) 
    {
        cout<<"celsius to fahrenheit conversion"<<endl;
        cout<<setw(8)<<"celsius"<<setw(38)<<"fahrenheit"<<endl;
        cout<<setw(8)<<"-------"<<setw(38)<<"----------"<<endl;
        cout<<fixed<<showpoint<<setprecision(2);
        
         
        for (count=1;count<=5;count++)  //runs calccel 5 times
            {
             calccel (deg, degF);
             }
             
    }
    float calccel ( float& deg,float degF)  //.celcius to fahrenheit
    {
              degF = ((9.0/5.0)*deg) + 32;
              cout<<setw(7)<<deg;
              cout<<setw(36)<<degF<<endl;
              deg++;
    }
    float fahscl (int count, float& deg,float degC)
    {
        cout<<"fahrenheit to celsius conversion"<<endl;
        cout<<"fahrenheit"<<setw(34)<<"celsius"<<endl;
        cout<<"----------"<<setw(34)<<"-------"<<endl;
        cout<<fixed<<showpoint<<setprecision(2);
        
        for (count=1;count<=5;count++)  //runs calcfah 5 times
            {
             
             calcfah (deg, degC);
             }
          
    }
    float calcfah (float& deg, float degC)  //converts fahrenheit to celsius
    {
        degC = (5.0/9.0)*(deg-32);
        cout<<setw(7)<<deg;
        cout<<setw(36)<<degC<<endl;
        deg++;
    }
    void CnorF()
    {
        cout<<"The letter you entered was not C or F, please try again."<<endl;
        return;                                   //error message
    }
    If you enter c or C, it goes through the celsius conversion twice, (first five times, then six), fahrenheit once (six times), then the error message. And it increments all the way through, so if I start with 32, the last temperature it converts in fahrenheit is 49. If I enter fahrenheit or the default it does similar (and similarly frustrating) things.
    Is there something obvious that I'm missing? As you might have noticed, I'm new at this. I've hacked this thing into bits and pieces but have yet to find out where the problem is.

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    In this function,
    Code:
    float getinfo (float& deg, char& scl)
    {
        cout<<"Please enter the temperature followed by C or F (celsius or fahrenheit):"
            <<endl;
        cin>>deg;   //temperature
        cin>>scl;    //scale (celsius or fahrenheit)
    }
    scl is a character. When you get to the cin>>scl; you must type "C[ENTER]" -- two characters. The [ENTER] will activate the read. Both these characters go into the input stream.

    The statement will then read the 'C' leaving [ENTER] in the stream. The next input reads the [ENTER] and continues. Place a cin.ignore(); after the character read.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    100
    I'm too burnt out and brain dead to try that right now, but I'll thank you in advance!

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    100
    Ok, I am fairly certain that what you are suggesting is on the right track Walt. cin.ignore(); does seem like the solution here, but I still can't seem to make it cooperate. The text for this class seems to have no reference to cin.ingore at all, but fortunately I have the internet and other (slightly dated) reference that I got from the library that does.
    Here's where I'm at, it seems that if you leave the parenthesis open, nothing happen. Makes sense. Nothing else that I put in there seems to do what I want it to do. if I put in cin.ignore('\r'); it just stops. I understand what you you're saying that c[enter] is two characters, although that hadn't occoured to me.
    OK, let me post a slightly cleaned up version of what I had, and then a couple of questions.

    Code:
        #include <iostream>
        #include <iomanip>
        using namespace std;
        float getinfo (float&, char&);
        float selscale (char&,int&,float&,float,float);
        float celscl (int, float&, float);
        
        float fahscl(int, float&, float);
        
        void CnorF();
        
    int main ()
    {
        char scl;
        float deg;
        int count;
        float Cdeg;
        float Fdeg;
        getinfo (deg, scl);
        selscale (scl,count,deg,Fdeg,Cdeg);
        celscl (count,deg,Fdeg);
        
        fahscl(count, deg,Cdeg);
        
        CnorF();
        
        system ("pause");
        return 0;
    }
    
    float getinfo (float& deg, char& scl)
    {
        cout<<"Please enter the temperature followed by C or F (celsius or fahrenheit):"
            <<endl;
        cin>>deg;        //enter temp.
        cin>>scl;         //enter letter
        //cin.ignore();  Do I want this here?
    }
    float selscale (char& scl,int& count,float& deg,float Fdeg, float Cdeg)
    { 
       
    
     
        if (scl == 'c'|| scl == 'C')
            {
            
             celscl(count,deg,Fdeg);             //if it's c, do celsius to fahrenheit
            
            }
         
            else if(scl == 'f'|| scl == 'F')
            {
            
            fahscl(count,deg,Cdeg);              //if it's f, do fahrenheit to celsius
            
            }
            else
            {
            CnorF ();                            //if it's neither, give an error message
            }
    }
    float celscl (int count,float& deg,float Fdeg) 
    {
        cout<<"celsius to fahrenheit conversion"<<endl;
        cout<<setw(8)<<"celsius"<<setw(38)<<"fahrenheit"<<endl;
        cout<<setw(8)<<"-------"<<setw(38)<<"----------"<<endl;
        cout<<fixed<<showpoint<<setprecision(2);
        
         
        for (count=1;count<=5;count++)
            {
             Fdeg = ((9.0/5.0)*deg) + 32; // convert celsius to fahrenheit
             cout<<setw(7)<<deg;          // increment five times
             cout<<setw(36)<<Fdeg<<endl;
             deg++;
             }
             
    }
    
    float fahscl (int count, float& deg,float Cdeg)
    {
        cout<<"fahrenheit to celsius conversion"<<endl;
        cout<<"fahrenheit"<<setw(34)<<"celsius"<<endl;
        cout<<"----------"<<setw(34)<<"-------"<<endl;
        cout<<fixed<<showpoint<<setprecision(2);
        
        for (count=1;count<=5;count++)
            {
             Cdeg = (5.0/9.0)*(deg-32);   //convert fahrenheit to celsius
             cout<<setw(7)<<deg;          // increment five times
             cout<<setw(36)<<Cdeg<<endl;
             deg++;
             }
             
          
    }
    
    void CnorF()
    {
        cout<<"The letter you entered was not C or F, please try again."<<endl;
        return;          //error message
    }
    1. Do I want cin.ignore in the getinfo function after cin>>scl; ?
    2. Is there somewhere that I could find a rough and ready explaination of what goes on within the parenthesis after that command?
    3. I'm still curious as to what is telling it to go from:
    celscl to celscl to fahscl to CnorF if it get C,
    fahscl to celscl fahscl to CnorF if it gets F,
    and CnorF to celscl to fahscl to CnorF if it gets anything else..

    I understand the first part of each, that's what I want it to do.. but why does the (deleting expletives) thing go through the functions the other three times??
    4. Since I want to allow for upper or lowercase C or F, will [shift] C [enter] result in three characters being entered?

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Quote Originally Posted by exluddite
    Ok, I am fairly certain that what you are suggesting is on the right track Walt. cin.ignore(); does seem like the solution here, but I still can't seem to make it cooperate. The text for this class seems to have no reference to cin.ingore at all, but fortunately I have the internet and other (slightly dated) reference that I got from the library that does. Here's where I'm at, it seems that if you leave the parenthesis open, nothing happen. Makes sense. Nothing else that I put in there seems to do what I want it to do. if I put in cin.ignore('\r'); it just stops. I understand what you you're saying that c[enter] is two characters, although that hadn't occoured to me.
    OK, let me post a slightly cleaned up version of what I had, and then a couple of questions.


    1. Do I want cin.ignore in the getinfo function after cin>>scl; ?
    yes

    2. Is there somewhere that I could find a rough and ready explaination of what goes on within the parenthesis after that command?
    Try This

    3. I'm still curious as to what is telling it to go from:
    celscl to celscl to fahscl to CnorF if it get C,
    fahscl to celscl fahscl to CnorF if it gets F,
    and CnorF to celscl to fahscl to CnorF if it gets anything else..

    I understand the first part of each, that's what I want it to do.. but why does the (deleting expletives) thing go through the functions the other three times??
    I thought I saw someone post an answer to this question either here or on another board

    4. Since I want to allow for upper or lowercase C or F, will [shift] C [enter] result in three characters being entered?
    No. Just 'C[enter]'
    See it that works
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    100
    YEEEEHAAAA!!!!
    I fixed it. A small victory, I know, but I was pretty darn pleased.
    It was all in main () (sort of).

    Code:
    int main ()
    {
        char scl;
        float deg;
        int count;
        float Cdeg;
        float Fdeg;
        getinfo (deg, scl);
        selscale (scl,count,deg,Fdeg,Cdeg);
        celscl (count,deg,Fdeg);
        fahscl(count, deg,Cdeg);
        CnorF();  // These were my problem!!
        
        system ("pause");
        return 0;
    }
    Code:
    float selscale (char& scl,int& count,float& deg,float Fdeg, float Cdeg)
    { 
       
    
     
        if (scl == 'c'|| scl == 'C')
            {
            
            celscl(count,deg,Fdeg);             //if it's c, do celsius to fahrenheit
            
            }
         
            else if(scl == 'f'|| scl == 'F')
            {
            
            fahscl(count,deg,Cdeg);              //if it's f, do fahrenheit to celsius
            
            }
            else
            {
            CnorF ();                            //if it's neither, give an error message
            }
    }
    Because I had alread called them from selscale().

    Then I called them again from main(). Essentially I was making more calls than a 17 year old girl on prom night.
    I didn't need to use the cin.ignore() , but I appreciate you sending me to that page, there is a lot of good information there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bubble sort program stumped
    By matthughes in forum C Programming
    Replies: 12
    Last Post: 05-17-2007, 01:28 PM
  2. System import but can't find function - stumped please help
    By hpteenagewizkid in forum C# Programming
    Replies: 3
    Last Post: 05-15-2007, 05:40 AM
  3. Can someone help me im totally stumped
    By Joe123 in forum C++ Programming
    Replies: 2
    Last Post: 10-18-2005, 12:17 PM
  4. Writing a "Protocol" - Stumped.
    By mrpickle in forum Game Programming
    Replies: 8
    Last Post: 01-21-2004, 06:37 PM
  5. help with stl search/find, or something else- stumped!
    By Terranc in forum C++ Programming
    Replies: 3
    Last Post: 12-21-2002, 03:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21