Nested switch problem

For my uni coursework I need to make 10 different functions and put them into one .cpp file in a switch so that one the program is run you enter the question number and it takes you to the solution to that question.
So I made each function separately and then put them into the switch. The problem is that one of functions(which worked on it's own) doesn't work in this new switch.
Here is the code just up to the first function which is the only one with a problem so far.
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
63
64
65
66
67
  #include <iostream>     
#include <algorithm>   
#include <string>
#include <random>       
#include <chrono>  
#include <ctype.h>
#include <conio.h>
#include <vector>
#include <sstream>

using namespace std;
using std::string;

int main()
{
	while (true)
	{
		cout << "Enter question number 0(quit), 1,...,10 > ";
		int q;
		cin >> q;
		switch (q)
		{
		case 0: return 0;
		case 1:
		{
			{
				char x;
				int result;
				cout << "Enter a character" << endl;
				cin >> noskipws >> x;
				x = toupper(x);

				if (x >= 'A' && x <= 'Z')
				{
					result = x - 'A';
					cout << result << endl;
				}

				else
				{
					switch (x)
					{
					case '.':
						cout << "26" << endl;
						break;

					case ',':
						cout << "27" << endl;
						break;

					case '\'':
						cout << "28" << endl;
						break;

					case ' ':
						cout << "29" << endl;
						break;

					default:
						cout << "-1" << endl;
					}
				}

				system("pause");
			}
		}
		break;


The problem I have is when I enter "1" initially to take me to function 1 it just says "enter a character" and then outputs -1, and then says "press any key to continue" and takes me back to the start of the first switch.
How do I sort this out?
First, inputting a character is best done like this

1
2
3
char x;
cin.get(x);
cin.ignore(1000,'/n');



Second, I still don't understand exactly what this is supposed to do, but I popped it into my eclipse and got it to work with changes. I think the problem was you weren't properly clearing the input buffer. Try this and see if it does what you need, keep in mind I may be using a different compiler.
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
* main.cpp
 *
 *  Created on: Apr 19, 2016
 *      Author: nsida
 */

#include <iostream>

#include <string>

#include <sstream>

using namespace std;


int main()
{
	int q;
	while (q !=0)
	{
		cout << "Enter question number 0(quit), 1,...,10 > ";

		cin >> q;
		cin.ignore(1000, '\n');
		switch (q)
		{
		case 0:
		case 1:
		{
			{
				char x;
				int result;
				cout << "Enter a character" << endl;
				cin.get(x);
				cin.ignore(1000,'\n');
				x = toupper(x);

				if (x >= 'A' && x <= 'Z')
				{
					result = x - 'A';
					cout << result << endl;
				}

				else
				{
					switch (x)
					{
					case '.':
						cout << "26" << endl;
						break;

					case ',':
						cout << "27" << endl;
						break;

					case '\'':
						cout << "28" << endl;
						break;

					case ' ':
						cout << "29" << endl;
						break;

					default:
						cout << "-1" << endl;
						break;
					}
				}


			}


		}
		}
	}
	return 0;
}
Last edited on
Thank you, that seems to have solved the problem.
Topic archived. No new replies allowed.