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