Thread: help with swich cases

  1. #1
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91

    help with swich cases

    I want to have my users of my programs to have the choice to start over by pressing y for yes and n for no, also, after it get results, u must press enter again for the optains to appear


    Heres part of the code
    Code:
    int cels(){
     
      float x;  
      cout << "Temperature Celsius:";
      cin>> x;
      cin.ignore();
      cout << "Fahrenheit:" << cel (x);
      cin.get();
     int input3;
      cout<<"Start over 1.yes / 2.no :\n";
      cin>> input3;
      switch ( input3 ) {
      case 1:            
        main();
        break;
      case 2:            
        cout:"/n";
        break;
      default:            
        cout<<"Error\n";
        break;
      }
     }
    i had to have yes and no be choesen by 1 or 2
    Verbal Irony >>

    "I love english homework!" When really nobody like english homework.
    -Mrs. Jennifer Lenz (English Teacher)

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    I don't see what you're asking?
    My computer is awesome.

  3. #3
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    Look, ive posted this code b4, but i want to b able to restart it by pressing y intstead of 1 or 2
    Code:
    #include <iostream>
    
    float far ( float x )
    {
     return (x-32)*(5.0/9.0);
    }
    
    float cel ( float x )
    {
     return (x*1.8)+32;
    }
    
    using namespace std;
    
    int faren();
    
    int cels();
    
    int main ()
    {
      int input;
      
      cout<<"1. Celsius to Fahrenheit\n";
      cout<<"2. Fahrenheit to Celsius\n";
      cout<<"Selection: ";
      cin>> input;
      switch ( input ) {
      case 1:            
        cels();
        break;
      case 2:            
        faren();
        break;
      default:            
        cout<<"Error\n";
        break;
      }
    }
    
    int faren() {
      
      float x;  
      cout << "Temperature Farenhieht:";
      cin >> x;
      cin.ignore();
      cout << "Celsius:" << far(x);
      cin.get();
      
    int input2;
    cout<<"Start over 1.yes / 2.no :\n";
      cin>> input2;
      switch ( input2 ) {
      case 1:            
        main();
        break;
      case 2:            
        cout:"/n";
        break;
      default:            
        cout<<"Error\n";
        break;
      }
    }
      
    int cels(){
     
      float x;  
      cout << "Temperature Celsius:";
      cin>> x;
      cin.ignore();
      cout << "Fahrenheit:" << cel (x);
      cin.get();
     int input3;
      cout<<"Start over 1.yes / 2.no :\n";
      cin>> input3;
      switch ( input3 ) {
      case 1:            
        main();
        break;
      case 2:            
        cout:"/n";
        break;
      default:            
        cout<<"Error\n";
        break;
      }
     }
    Verbal Irony >>

    "I love english homework!" When really nobody like english homework.
    -Mrs. Jennifer Lenz (English Teacher)

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    44
    Then instead of

    case 1 : ....

    you want

    case 'y' : ....

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    44
    Oops, you'll also want to make sure the input variable is a char.

  6. #6
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    I did that and it gives me an error of 'y' undeclaired ( first use this fuction) and

    'n' undeclaired ( first use this fuction)


    i didnt do that, well i tried case char and just char, but it gave me even more errors
    Verbal Irony >>

    "I love english homework!" When really nobody like english homework.
    -Mrs. Jennifer Lenz (English Teacher)

  7. #7
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    I had to.. edit.. this. Recursive calls to main() are bad, as were a few other things.

    Code:
    #include <iostream>
    
    int menu();
    float far(float x);
    float cel(float x);
    
    bool quitflag = 0;
    
    int main()
    {
        while (quitflag != 1)
        {
            menu();
        }
          
        return(0);
    }
    
    int menu()
    {
        int ans;
        char again;
      
        std::cout << "1. Celsius to Fahrenheit";
        std::cout << "\n2. Fahrenheit to Celsius";
      
        std::cout << "\n\nSelection: ";
        std::cin >> ans;
        
        if (ans == 1)
        {
            int temp = 0;
            
            std::cout << "\nTemperature: ";
            std::cin >> temp;
            
            std::cout << "\nFinal temp is: " << cel(temp);
        }
        else if (ans == 2)
        {
            int temp = 0;
            
            std::cout << "\nTemperature: ";
            std::cin >> temp;
            
            std::cout << "\nFinal temp is: " << far(temp);
        }
        else
        {
            std::cout << "\nInvalid input, please try again.\n\n";
            return(0);
        }
        
        std::cout << "\n\nCalculate another? ";
        std::cin >> again;
        std::cout << std::endl;
        
        tolower(again);
            
        if (again == 'n') // Only need to test an 'n' condition, the rest will auto-repeat. Switch this to 'y' and take out the 'quitflag' line if you want it to exit on anything except 'y'.
        {
            quitflag = 1;
            return(0);
        }
        
        return(0);
                
    }
    
    float far(float x)
    {
        return((x-32)*(5.0/9.0));
    }
    
    float cel(float x)
    {
        return((x*1.8)+32);
    }

  8. #8
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    error of 'y' undeclaired ( first use this fuction)
    Did your case statements say (for example) case y: or were they properly case 'y': ? There's a big difference.
    Last edited by Scribbler; 02-14-2005 at 06:30 PM.

  9. #9
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    my bad, i used that first one
    Verbal Irony >>

    "I love english homework!" When really nobody like english homework.
    -Mrs. Jennifer Lenz (English Teacher)

  10. #10
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    I got it working
    Code:
    #include <iostream>
    
    float far ( float x )
    {
     return (x-32)*(5.0/9.0);
    }
    
    float cel ( float x )
    {
     return (x*1.8)+32;
    }
    
    using namespace std;
    
    int faren();
    
    int cels();
    
    int main ()
    {
      int input;
      
      cout<<"1. Celsius to Fahrenheit\n";
      cout<<"2. Fahrenheit to Celsius\n";
      cout<<"Selection: ";
      cin>> input;
      switch ( input ) {
      case 1:            
        cels();
        break;
      case 2:            
        faren();
        break;
      default:            
        cout<<"Error\n";
        break;
      }
    }
    
    int faren() {
      
      float x;  
      cout << "Temperature Farenhieht:";
      cin >> x;
      cin.ignore();
      cout << "Celsius:" << far(x);
      cin.get();
      
    char input2;
    cout<<"Start over y / n :\n";
      cin>> input2;
      switch ( input2 ) {
      case 'y':            
        main();
        break;
      case 'n':            
        cout:"/n";
        break;
      default:            
        cout<<"Error\n";
        break;
      }
    }
      
    int cels(){
     
      float x;  
      cout << "Temperature Celsius:";
      cin>> x;
      cin.ignore();
      cout << "Fahrenheit:" << cel (x);
      cin.get();
     char input3;
      cout<<"Start over y / n :\n";
      cin>> input3;
      switch ( input3 ) {
      case 'y':            
        main();
        break;
      case 'n':            
        cout:"/n";
        break;
      default:            
        cout<<"Error\n";
        break;
      }
     }
    Ok but i still have one problem, the user must press return to get the y / n option. I know it is a really easy thing, but i cant figure it out
    Verbal Irony >>

    "I love english homework!" When really nobody like english homework.
    -Mrs. Jennifer Lenz (English Teacher)

  11. #11
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    Code:
    if (again == 'n') // Only need to test an 'n' condition, the rest will auto-repeat. Switch this to 'y' and take out the 'quitflag' line if you want it to exit on anything except 'y'.
        {
            quitflag = 1;
            return(0);
        }
        
        return(0);
    What's quitflag
    Verbal Irony >>

    "I love english homework!" When really nobody like english homework.
    -Mrs. Jennifer Lenz (English Teacher)

  12. #12
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    quitflag is a boolian variable that is either FALSE (0) or TRUE (1). Since I am using a while() that keeps you in the menu until the user quits, I can say this:

    WHILE quitflag IS false
    ENTER menu() function

    Then, when the user types N or n, I make 'quitflag' true. The while loop evaluates false, and the program exits.

  13. #13
    Registered User
    Join Date
    Feb 2005
    Posts
    44
    read his code, it's very simple

    With a little common sense, you could do a search (CTRL+F) to see where else he uses that variable. Oh, surely enough - he has a while loop that will terminate once quitflag is true. Funny how that works!

    The problem is you don't seem to be trying. You don't even need five MINUTES of C++ education to use the trick I just used above.

  14. #14
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    i dont even kno what BOOLEn is, whenever it said that in the tutorial, i just skipped it, my bad, but i think i got it working
    Verbal Irony >>

    "I love english homework!" When really nobody like english homework.
    -Mrs. Jennifer Lenz (English Teacher)

  15. #15
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    http://en.wikipedia.org/wiki/George_Boole

    The creator of 'boolean algebra'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to handle multiple cases which use common code?
    By tmaxx in forum C Programming
    Replies: 3
    Last Post: 10-03-2008, 07:42 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. I need to use 0 to quit a program
    By cwest in forum C Programming
    Replies: 2
    Last Post: 12-15-2001, 08:37 AM
  5. Please help me with cases!!!
    By halifax in forum C Programming
    Replies: 1
    Last Post: 10-14-2001, 01:29 PM