Can this be cleaned up/improved? And an unrelated problem...

Hello all,

I'm in a C++ class for this fall and have been working on a few questions that I have given me a bit of trouble so soon into the class.

I had to write a program that would quiz students on simple addition/subtraction, and this is what I got. It works just fine, so my real question is... are there any places I can make this more efficient? Is there anything I did in a way that doesn't make sense to a more experienced person? I battled with this thing for quite some time before I could get it all to work right, so if I can learn for next time, that would be nice.

Here's what I have.
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>

using namespace std;

int main()
{	
	// constants for menu choices
	const char  ADDITION = '1',
			   SUBTRACTION = '2',
			   EXIT = '3';

	// declaring variables.
	int addright = 0,
		addwrong = 0,
		addition = 0,
		subright = 0,
		subwrong = 0,
		subtraction = 0;
	int	num1,
		num2,
		correctanswer,
		studentanswer;
	char category;
	
	// used for random number generation.
	unsigned seed = time(0);
	srand(seed);

	do
	{
		// display the main menu and input of category.
		cout << setw(27) << "           MENU           " << endl;
		cout << "---------------------------" << endl;
		cout << "1: Enter 1 for Addition" << endl;
		cout << "2: Enter 2 for Subtraction" << endl;
		cout << "3: Enter 3 to Exit" << endl << endl;
		cout << "Enter your selection: ";
		cin >> category; 
				


		// validate menu selection.
		while (category < ADDITION || category > EXIT)
		{
			cout << "Please enter a valid menu option: " << endl;
			cin >> category;
		}
		// run program as long as exit isn't selected.
		if (category != EXIT)
		{
			switch (category)
			{
			case ADDITION:
				
				do
				{
					// random number generation for problems.
					num1 = 1 + rand() % 9;
					num2 = 1 + rand() % 9;
					correctanswer = num1 + num2;
					cout << "What is the sum of " << num1 << " and " << num2 << "?" << endl;
					cout << "Enter your answer, or enter \"-1\" to return to the menu" << endl << endl;
					cin >> studentanswer;
					if (studentanswer != -1)
						// if the student opts to try a problem, increment the addition problem counter.
						++addition;
					
					// if the student enters -1, they return to the main menu.
					while (studentanswer != -1)
					{
						// if the student is correct, increment the addright counter, congratulate them and move to the next problem.				
						if (studentanswer == correctanswer)
						{
							addright++;
							cout << "Very good!" << endl;
							break;
						}			
									
						else 
						{
							// if the student is wrong, increment the addwrong counter tell them and let them try until they get it right.
							if (studentanswer != correctanswer)
							addwrong++;
							cout << "No, please try again." <<endl;
							cout << "Enter your answer, or enter \"-1\" to return to the menu" << endl << endl;
							cin >> studentanswer;
							
						}
					}
				} while (studentanswer != -1);

				// provide results for their addition problems when they are completed.
				cout << "Summary: " << endl;
				cout << "Addition Problems Played: " << addition << endl;
				cout << "Number of times answered correctly: " << addright << endl;
				cout << "Number of times answered incorrectly: " << addwrong << endl << endl;
				break;

				case SUBTRACTION:
				
				do
				{
					// random number generation for problems.
					num1 = 10 + rand() % 9;
					num2 = 1 + rand() % 9;
					correctanswer = num1 - num2;
					cout << "What is the difference between " << num1 << " and " << num2 << "?" << endl;
					cout << "Enter your answer, or enter \"-1\" to return to the menu" << endl << endl;
					cin >> studentanswer;
					if (studentanswer != -1)
						// if the student opts to try a problem, increment the subtraction problem counter.
						subtraction++;

					while (studentanswer != -1)
					{							
						if (studentanswer == correctanswer)
						{
							// if the student is correct, increment the subright counter, congratulate them and move to the next problem.
							subright++;
							cout << "Very good!" << endl;
							break;
						}			
									
						else if (studentanswer != correctanswer)
						{
							// if the student is wrong, increment the subwrong counter tell them and let them try until they get it right.
							subwrong++;
							cout << "No, please try again." <<endl;
							cout << "Enter your answer, or enter \"-1\" to return to the menu" << endl << endl;
							cin >> studentanswer;
							
						}
					
					}
				} while (studentanswer != -1);
				// provide results for their subtraction problems when they are completed.
				cout << "Summary: " << endl;
				cout << "Subtraction Problems Played: " << subtraction << endl;
				cout << "Number of times answered correctly: " << subright << endl;
				cout << "Number of times answered incorrectly: " << subwrong << endl << endl;
			}
		}
						
	} while (category != EXIT);
	
	return 0;
}








I also have another one that I don't even know where to get started on. I'm not asking for full solutions, but can someone tell me where to get started on this?

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
Use for loop, while loop, and do while loops to generate a table of decimal numbers, 
as well as the binary, octal, and hexadecimal equivalents of the decimal numbers, 
in the range 1-256.

Note: See Appendix for number systems. 

The program should print the results to the Console without using any string formats . 

Design Details: 
The Main program should prompt the user for input.   
The output is shown in the sample output below.
           
Note: To generate the binary numbers your code should use a while loop to generate 
the binary numbers, a “for” loop to generate the octal numbers and a “do .. while” to 
generate the hexadecimal numbers.

Sample Output:

Enter the low number:
1
Enter the high number:
10
Decimal Binary          Octal   Hexadecimal
1       1               1       1
2       10              2       2
3       11              3       3
4       100             4       4
5       101             5       5
6       110             6       6
7       111             7       7
8       1000            10      8
9       1001            11      9
10      1010            12      A

****Resetting low and high ****
Low = 10, High = 15
Decimal Binary          Octal   hexadecimal
10      1010            12      A
11      1011            13      B
12      1100            14      C
13      1101            15      D
14      1110            16      E
15      1111            17      F

Press any key to continue . . .


My problem starts with the different number formats...do I have to create a file that has all 256 options for each of those formats? I can't figure out how I can just tell it to print out hexidecimal numbers for a certain range without me telling it what those are...I googled it and there appear to be shortcuts (found stuff involving %d and similar commands using the "older" style of C++), but we're only 5 chapters in and I've seen nothing like that, so I don't think that's how they want it done. Any suggestions?
Last edited on
HINT: ostream has dec, hex, and oct flags for output format of integrals.
you missed a break statement between line 143 and 144
Thanks for the info guys!

a couple of questions regarding the first one: If when you first choose a category, and you type a character that is not valid, it will tell you that and ask you to try again. But if you put in TWO or more characters there, it will repeat the "Please enter a valid menu option: " as many times as there were characters. If you enter "111111111111" or "22222222222", it goes into an infinite loop.

This also happens if you enter a number 10 or more characters long while answering a problem.

Why does it happen? I know I can use cin.ignore on the menu choice but as far as I understand I have to ignore how many characters it ignores...I'd rather have it ignore anything after 1.

And I'm not even sure why those inputs would cause loops during the answering of a question.


*worth noting that the validation was not part of the original problem (aside from telling them if an answer was incorrect), and that I've already turned in what I have, but I hate leaving stuff open-ended so I'm trying to figure out how to properly validate it anyway.
Anyone else with input on this?

It repeats because you are using a loop that repeatedly reads one character at a time, checking to see if it is valid. If you enter many invalid characters, it loops to check each one.

The issue with the numbers when answering the question may be because an integer is not large enough to hold the number that you typed. cin would then enter into an error state, and you can no longer receive input until it is corrected.
Topic archived. No new replies allowed.