Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with a Switch statement 1

Status
Not open for further replies.

GBONGIOVANO

Programmer
Jan 17, 2003
16
0
0
US
I have a menu with selections 1-9.
However I would like a Q or q for quit.
How do I do this with my variable makeChoice as an int
my code below thanls
g

void simulatorMenu()
{
int makeChoice;

cout << &quot;\n\n\n\t\t\t Please Make A Choice \n&quot;;
cout << &quot;\n\n\n\t\t [1] To Add a Node to the Network \n&quot;;
cout << &quot;\n\t\t [2] To Delete a Node from the Network \n&quot;;
cout << &quot;\n\t\t [3] To Add Link Between Two Nodes \n&quot;;
cout << &quot;\n\t\t [4] To Remove th Link Between Two Nodes\n&quot;;
cout << &quot;\n\t\t [5] To Add a Process ID to the Node ID \n&quot;;
cout << &quot;\n\t\t [6] To Add a Child Process ID to Parent and Node ID(s) \n&quot;;
cout << &quot;\n\t\t [7] To Remove a Child Process \n&quot;;
cout << &quot;\n\t\t [8] To Remove a Parent Process \n&quot;;
cout << &quot;\n\t\t [9] To View How many Nodes are on the Network \n&quot;;
cout << &quot;\n\t\t [Q] or [q] to Quit the Programme \n&quot;;

cin >> makeChoice;
system(&quot;cls&quot;);


switch(makeChoice)

{
case 1:
//cout << &quot;Add Node to Network&quot; << endl;
return(addNode());
break;

case 2:
return (notYet());
break;

case 3:
return (notYet());
break;

case 4:
return (notYet());
break;

case 5:
return (notYet());
break;

case 6:
return (notYet());
break;

case 7:
return (notYet());
break;

case 8:
return (notYet());
break;

case 9:
return (notYet());
break;

/* Comments for debugging
case 'Q':
case 'q':
//cout << &quot;n\ Test \n&quot;;
return (copyRight());
break;



//default:
// cout << &quot; Please Follow Menu Instructions \n&quot;;
// system(&quot;cls&quot;);
//return simulatorMenu();
*/
}
return simulatorMenu();
 
the ASCII values are 81 and 113 respectively. Therefore:

[tt]switch(makeChoice)
{
case 81:
case 113:
// do quit
break;
}[/tt]

- Nick
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
Yeah, but that's not gonna work with the code you have; cin and an extraction operator won't translate a 'q' or a 'Q' as an integer ASCII value... it'll just fail.

You could use characters for your selections in the first place; this works if you're not going to add a tenth choice.

Otherwise, read them as strings and compare them. Or read as strings, translate to an int (with a stringstream), and special-case 'q' and 'Q' to translate to some unused integer (perhaps 0) and you can keep your switch statement.
 
Or simply use

switch(makeChoice)
{
case '1':
case '2':
...
case 'Q':
case 'q':

}

The '1' will evaluate to the correct ascii value. 'Q' will do the same. For readability, you may want to change &quot;makeChoice&quot; to a char.


Matt
 
I must've had what is known as a &quot;senior moment&quot;. I was thinking along the lines of Matt but with ASCII values. :)
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
Not for &quot;readability&quot;, you MUST to change makeChoice to char if switch ... case 'Q': ... case '1'... using.
It's the best way, remember immortal &quot;keep it simple, ...&quot;.
Avoid ASCII codes where possible in XXI...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top