Thread: Is it ok to have if else in my switch?

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    28

    Is it ok to have if else in my switch?

    I'm trying to compile my code and i cannot see anything wrong with it as it compiled grand before i added the if and else statements to my program.

    c:\docume~1\blackg~1\desktop\untitl~2.cpp: In function `int main()':
    c:\docume~1\blackg~1\desktop\untitl~2.cpp:51: parse error before `{'
    c:\docume~1\blackg~1\desktop\untitl~2.cpp:60: confused by earlier errors, bailing out

    I would be gratefull if someone could help me out .
    Thanks so much.
    blackgold>>



    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    
    void started ( void );            // function prototype :1
    void option ( void );              // function prototype :2
    void gears ( void );               // function prototype :3
    void lets_go ( void );             // function prototype :4
    
    int main()
    {
        {
    
      char game;                            // game is local variable
      int  engine_go = 0,
           clutch_in = 0;
    
      cout <<"Hi welcome to the driving program" << endl
           <<"\nPlease press: s to start engine" << endl
           <<"\n" << endl;
    
      while ( ( game = cin.get() ) != EOF ) {
    
         switch ( game ) {                                 // switch nested in while
    
            case 's':
               started();                                  // engine started
               engine_go = 1;                              // engine is started
               break;                                      // to exit switch
    
            case 'o':
               option();                                   // options before moving
               break;
    
            case '1':
               clutch_in = 1;                               // clucth is in
               gears();                                     // selection of gears
               break;
    
            case '\n':                                      // ignore newlines,
            case '\t':                                      // tabs,
            case ' ' :                                      // and spaces in input
               break;
    
               default:                                     // catch all other charecters
               if ( engine_go == 1 && clutch_in == 1 ) {
                  cout <<"\n ok clutch is pushed in and you are in first gear" << endl;
                      lets_go();
               }
               else ( clutch_in != 1 ) {
                  cout <<"\nPlease enter correct choice" << endl;
                      option();
               }
    
               break;
    
             }
          }
       }
    
    // broke out of local while loop as EOF has been pressed.
    
    return 0;
    }
    
    void started ( void )            // function def :1
    {
       cout << "Engine started" << endl
            << "Press o for options" << endl;
    }
    
    void option ( void  )              // function def :2
    {
       cout <<"Ok lets see what you think?" << endl
            <<"\noptions"
            <<"\n1) clutch in"
            <<"\n2) brake"
            <<"\n3) handbrake on"
            <<"\n4) handbrake off"
            <<"\nEnter number: " << endl;
    }
    
    void gears ( void )                // function :3
    {
       cout <<"Great work!"
            <<"\nlets put it in gear"
            <<"\n0)\treverse"
            <<"\n1)\tfirst"
            <<"\n2)\tsecond"
            <<"\n3)\tthird"
            <<"\n4)\tfourth"
            <<"\n5)\tfifth"
            <<"\nWhich gear choose please: " << endl;
    }
    
    void lets_go ( void )
    {
       cout <<"\n Hold down: CTRL and press c to release clutch and start moving " << endl;
    }

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    exactly what line is line 51? I don't have an IDE readily available, and don't feel like counting...

    your answer: yes, I'm pretty sure you can have an if statement in a switch statement

    EDIT: line 51:
    Code:
               default:                                     // catch all other charecters
               if ( engine_go == 1 && clutch_in == 1 ) 
               {
                  cout <<"\n ok clutch is pushed in and you are in first gear" << endl;
                      lets_go();
               }  //line 51
               else ( clutch_in != 1 ) 
               {
                  cout <<"\nPlease enter correct choice" << endl;
                      option();
               }
    EDIT 2: fixt
    Code:
               default:   
                        {                                  // catch all other charecters
               if ( engine_go == 1 && clutch_in == 1 ) 
               {
                  cout <<"\n ok clutch is pushed in and you are in first gear" << endl;
                      lets_go();
               }
               else if( clutch_in != 1 ) 
               {
                  cout <<"\nPlease enter correct choice" << endl;
                      option();
               }
    
                break;
                }
    the orange isn't necessary, just nice, but the red was what you needed... you can either put what I did, or just get rid of the condition and use else...
    Last edited by major_small; 04-28-2004 at 10:37 AM. Reason: found an IDE, fixt problem
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Code:
    cout <<"Hi welcome to the driving program" << endl
           <<"\nPlease press: s to start engine" << endl
    Code:
    cout << "Engine started" << endl
    Code:
    cout <<"Ok lets see what you think?" << endl
    dont those require a semicolon?
    When no one helps you out. Call google();

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Quote Originally Posted by InvariantLoop
    dont those require a semicolon?
    No, because a statement can run more than one line:
    Code:
    cout << "Engine started" << endl
         << "Press o for options" << endl;
    is equivalent to:
    Code:
    cout << "Engine started" << endl << "Press o for options" << endl;
    This would be wrong:
    Code:
    cout << "Engine started" << endl
    cout << "Press o for options" << endl;

  5. #5
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159

    Arrow re

    after case use
    that facilate your work
    Code:
    {//begin
    }//end
    so i advice you to use a function that say welcome and press s ...
    /* exuse mefor mistakes because i use a translator */
    Last edited by enjoy; 04-28-2004 at 04:02 PM. Reason: i english

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    actually, they do, just at the end of the statements... the first example needs one after the end of teh second line, and the second and third examples need one at the end of that line...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  3. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 06:40 PM
  4. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 03:51 AM