How to equal Q to q?

At the near end of the code you'll see how I tried to capture the capitol Q but it didn't work. Any advice?


#include <iostream>
#include <stdexcept>

using namespace std;

//global constants
//currently none

//function prototypes

//variables
char mainoption; //declare main menu option variable

int main()

{
// The MAIN MENU, which is the start screen for the game.
cout <<"MAIN MENU:\n\n";
cout <<"1- Create a Character\n";
cout <<"2- Load an Existing Character\n"; // Most common selected
cout <<"3- Delete a Character\n";
cout <<"4- View Existing Character List\n"; // For those poor forgetful folk like me
cout <<"Q- Quit Game (AT ANY TIME WITH OUT SAVING)\n\n";

//main menu loop
do
{

cout <<"Please select an option above: ";
cin >> mainoption;

switch (mainoption)
{
case '1':
cout <<"create character\n"; //replace with call to newplayer
break;
case '2':
cout <<"load character\n"; //replace with call to loadplayer in town
break;
case '3':
cout <<"delete character\n"; //replace with call to deleteplayer
break;
case '4':
cout <<"view character list\n"; //replace with call to charlist
break;
case 'q': //is there a way to account for capitol "Q" also?
//try to quit
continue;
default:
cerr <<"Selection " << mainoption << " is not a valid option.\n";
}
}
while (mainoption != 'q' && 'Q');

return 0;
}
Please use code tag next time.

Enter one more case for 'Q'.
One more method I could think of is use :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

do
{

cout <<"Please select an option above: ";
cin >> mainoption;	

if (mainoption == 'Q')
{
      mainoption = 'q';
}

switch (mainoption)
{
//THE REST 

Last edited on
Convert mainoption to lower case before the switch
mainoption = tolower(mainoption);
Last edited on
Sorry I was unaware of the "code tag" and will look for it next time. =+S

And thank you for the help! The latter version was what I was trying to express, but I was having a massive mind block on the subject. Also would I be able to safely remove the &&'Q' from the while (mainoption...) ?
1
2
3
4
while (mainoption != 'q');

return 0;
}


Since you lower from Q to q before,you don't need &&'Q',

besides this is wrong method...

while (mainoption != 'q' && mainoption !='Q');

is correct.

1
2
default:
cerr <<"Selection " << mainoption << " is not a valid option.\n";


You better have a continue to let the user input again.
Last edited on
I'm unfamiliar with "continue" and must assume that the reason my program functioned correctly in the do while is because of my capture error? I will test again with the prescribed adjustments, but I'd like to know more about this "continue" you speak of hentaiw. My thanks to all of you who posted advice here to include Peter87. =+}
Just replace your "while" code with the one below:

while (mainoption != 'q' && mainoption != 'Q');

The other way doesn't work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <stdexcept>

using namespace std;

//global constants
//currently none

//function prototypes

//variables
char mainoption; //declare main menu option variable

int main()

{
// The MAIN MENU, which is the start screen for the game.
cout <<"MAIN MENU:\n\n";
cout <<"1- Create a Character\n";
cout <<"2- Load an Existing Character\n"; // Most common selected
cout <<"3- Delete a Character\n";
cout <<"4- View Existing Character List\n"; // For those poor forgetful folk like me
cout <<"Q- Quit Game (AT ANY TIME WITH OUT SAVING)\n\n";

//main menu loop
do
{

cout <<"Please select an option above: ";
cin >> mainoption;	


if (mainoption == 'Q')
{
      mainoption = 'q';
}

switch (mainoption)
{
case '1':
cout <<"create character\n"; //replace with call to newplayer
break;
case '2':
cout <<"load character\n"; //replace with call to loadplayer in town
break;
case '3':
cout <<"delete character\n"; //replace with call to deleteplayer
break;
case '4':
cout <<"view character list\n"; //replace with call to charlist
break;
case 'q': //is there a way to account for capitol "Q" also?
//try to quit
break;
default:
cerr <<"Selection " << mainoption << " is not a valid option.\n";
}
}
while (mainoption != 'q');

return 0;
}


No I'm sorry,because you don't have a code tag so I had a mistake looking at your code...It works fine,I rewrote
Last edited on
My thanks again, and I will try to utilize the code tag. I apologize for the standard posting violation, I hope to learn more and help others here also. =+}
Topic archived. No new replies allowed.