id returned 1 exit staus. ...?

i hae no idea whats wrong. the next two parts are also part of the program. when i put the loops after or before the percentage statement it eithers skips the letter grade or the percentage. help? please?

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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Multiple Choice Test for Final

#include <iostream.h>
#include <iomanip.h>
main()
{
char choice;
int total;
int correct = 0;

cout << "Mathematics Exam: Please choose the best answer. Take your time as you will only have one chance to amswer each question.\n";
cout << "Make sure to answer in capital letters.\n";
cout << "Find x.\n";

cout << "1. 10 / 2 = x \n"; 
cout << "A) 8\n";
cout << "B) 5\n";
cout << "C) 0\n";
cout << "D) 10\n";
cout << "Enter the letter of the answer you have chosen. ";
cin >> choice;

switch(choice)
{
case 'B':
{
cout << "Correct.\n";
++correct;
break;
}
default:
{
cout << "Incorrect. The answer is B.\n";
break;
}
}

cout << "2. 5 / 2 = x \n";
cout << "A) 2.5555555\n";
cout << "B) 2\n";
cout << "C) 2.500000\n";
cout << "D) None of the above\n";
cout << "Enter the letter of the answer you have chosen. ";
cin >> choice;


switch (choice)
{
case 'C':
{
cout << "Correct.\n";
++correct;
break;
}
default:
{
cout << "Incorrect. The answer is C.\n";
break;
}
}

cout << "3. 9 / x = 2 \n";
cout << "A) 4.50\n";
cout << "B) 4\n";
cout << "C) 3.95\n";
cout << "D) none of the above\n";
cout << "Enter the letter of the answer you have chosen. ";
cin >> choice;

switch (choice)
{
case 'A':
{
cout << "Correct.\n";
++correct;
break;
}
default:
{
cout << "Incorrect. The answer is A.\n";
break;
}
}


cout << "4. x / 5 = 20 \n";
cout << "A) 100.0\n";
cout << "B) 10000 * 0.01\n";
cout << "C) 10^2\n";
cout << "D) All of the above\n";
cout << "Enter the letter of the answer you have chosen. ";
cin >> choice;

switch (choice)
{
case 'D':
{
cout << "Correct.\n";
++correct;
break;
}
default:
{
cout << "Incorrect. The answer is D.\n";
break;
}
}

cout << "5. x ^ 2 = 144\n";
cout << "A) 12\n";
cout << "B) -12\n";
cout << "C) 14\n";
cout << "D) Both A and B\n";
cout << "Enter the letter of the answer you have chosen. ";
cin >> choice;

switch (choice)
{
case 'D':
{
cout << "Correct.\n";
++correct;
break;
}
default:
{
cout << "Incorrect. The answer is D.\n";
break;
}
}

cout << "6. 2 + (2^0) = x \n";
cout << "A) 4\n";
cout << "B) 3\n";
cout << "C) 0\n";
cout << "D) None of the above\n";
cout << "Enter the letter of the answer you have chosen. ";
cin >> choice;

switch(choice)
{
case 'B':
{
cout << "Correct.\n";
++correct;
break;
}
default:
{
cout << "Incorrect. The answer is B.\n";
break;
}
}

cout << "7. 90/30 = x\n";
cout << "A) 3\n";
cout << "B) 30\n";
cout << "C) 13\n";
cout << "D) None of the above.\n";
cout << "Enter the letter of the number you have chosen. ";
cin >> choice;

switch (choice)
{
case 'A':
{
cout << "Correct.\n";
++correct;
break;
}
default:
{
cout << "Incorrect. The answer is A.\n";
break;
}
}

cout << "8. 1+1 = x\n";
cout << "A) 2\n";
cout << "B) 1\n";
cout << "C) 0\n";
cout << "D) None of the above\n";
cout << "Enter the letter of the answer you have chosen. ";
cin >> choice;
Last edited on
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
switch (choice)
      {
             case 'A':
                  {
                      cout << "Correct.\n";
                      ++correct;
                      break;
                  }
             default:
                     {
                            cout << "Incorrect. The answer is A.\n";
                            break;
                     }
      }
      
      cout << "9. (10^1099) / (1^100)\n";
      cout << "A) 10332543235\n";
      cout << "B) ERR: OVERFLOW\n";
      cout << "C) 50000000000000000000\n";
      cout << "D) 10^1099\n";
      cout << "Enter the letter of the answer you have chosen. ";
      cin >> choice;
      
      switch (choice)
      {
             case 'D':
                  {
                      cout << "Correct.\n";
                      ++correct;
                      break;
                  }
             default:
                     {
                            cout << "Incorrect. The answer is D.\n";
                            break;
                     }
      }
      
      cout << "10. x! = 362880\n";  //10
      cout << "A) 10\n";
      cout << "B) 9\n";
      cout << "C) 8\n";
      cout << "D) All of the above\n";
      cout << "Enter the letter of the answer you have chosen. ";
      cin >> choice;
      
      switch(choice)
      {
                    case 'B':
                         {
                             cout << "Correct.\n";
                             ++correct;
                             break;
                         }
                    default:
                            {
                                   cout << "Incorrect. The answer is B.\n";
                                   break;
                            }
      }
      
      cout << "11. 12+1 = x\n";
      cout << "A) 13\n";
      cout << "B) 12\n";
      cout << "C) 11\n";
      cout << "D) None of the above\n";
      cout << "Enter the letter of the answer you have chosen. ";
      cin >> choice;
      
      switch (choice)
      {
             case 'A':
                  {
                      cout << "Correct.\n";
                      ++correct;
                      break;
                  }
             default:
                     {
                            cout << "Incorrect. The answer is A.\n";
                            break;
                     }
      }
      
      cout << "12. 45 = 9x\n";
      cout << "A) 5\n";
      cout << "B) 9\n";
      cout << "C) 7\n";
      cout << "D) None of the above\n";
      cout << "Enter the letter of the answer you have chosen. ";
      cin >> choice;
      
      switch (choice)
      {
             case 'A':
                  {
                      cout << "Correct.\n";
                      ++correct;
                      break;
                  }
             default:
                     {
                            cout << "Incorrect. The answer is A.\n";
                            break;
                     }
      }
      
      cout << "13. 5 * (7^2) = x\n";
      cout << "A) 240\n";
      cout << "B) 345\n";
      cout << "C) 245\n";
      cout << "D) None of the above\n";
      cout << "Enter the letter of the answer you have chosen. ";
      cin >> choice;
      
      switch (choice)
      {
             case 'C':
                  {
                      cout << "Correct.\n";
                      ++correct;
                      break;
                  }
             default:
                     {
                            cout << "Incorrect. The answer is C.\n";
                            break;
                     }
      }
      
      cout << "14. 1 * 1 = x\n";
      cout << "A) 2\n";
      cout << "B) 1\n";
      cout << "C) 0\n";
      cout << "D) None of the above\n";
      cout << "Enter the letter of the answer you have chosen. ";
      cin >> choice;
      
      switch(choice)
      {
                    case 'B':
                         {
                             cout << "Correct.\n";
                             ++correct;
                             break;
                         }
                    default:
                            {
                                   cout << "Incorrect. The answer is B.\n";
                                   break;
                            }
      }
      
      cout << "15. 18:3 = x:9\n";
      cout << "A) 36\n";
      cout << "B) 72\n";
      cout << "C) 54\n";
      cout << "D) None of the above\n";
      cout << "Enter the letter of the answer you have chosen. ";
      cin >> choice;
      
      switch (choice)
      {
             case 'C':
                  {
                      cout << "Correct.\n";
                      ++correct;
                      break;
                  }
             default:
                     {
                            cout << "Incorrect. The answer is C.\n";
                            break;
                     }
      }
      
      cout << "16. 92+0 = x\n";
      cout << "A) 92\n";
      cout << "B) 90\n";
      cout << "C) 0\n";
      cout << "D) None of the above\n";
      cout << "Enter the letter of the answer you have chosen. ";
      cin >> choice;
      
Last edited on
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
151
152
153
154
155
156
157
158
159
160
161
switch (choice)
      {
             case 'A':
                  {
                      cout << "Correct.\n";
                      ++correct;
                      break;
                  }
             default:
                     {
                            cout << "Incorrect. The answer is A.\n";
                            break;
                     }
      }
      
      cout << "17. 7^3 = x\n";
      cout << "A) 288\n";
      cout << "B) 343\n";
      cout << "C) 340\n";
      cout << "D) None of the above\n";
      cout << "Enter the letter of the answer you have chosen. ";
      cin >> choice;
      
      switch(choice)
      {
                    case 'B':
                         {
                             cout << "Correct.\n";
                             ++correct;
                             break;
                         }
                    default:
                            {
                                   cout << "Incorrect. The answer is B.\n";
                                   break;
                            }
      }
      
      cout << "18. 1 * 0.5 = x\n";
      cout << "A) .05\n";
      cout << "B) 0.5\n";
      cout << "C) 1/2\n";
      cout << "D) Both B and C\n";
      cout << "Enter the letter of the answer you have chosen. ";
      cin >> choice;
      
      switch (choice)
      {
             case 'D':
                  {
                      cout << "Correct.\n";
                      ++correct;
                      break;
                  }
             default:
                     {
                            cout << "Incorrect. The answer is D.\n";
                            break;
                     }
      }
      
      cout << "19. 3! = x\n";
      cout << "A) 6\n";
      cout << "B) 1\n";
      cout << "C) 9\n";
      cout << "D) None of the above\n";
      cout << "Enter the letter of the answer you have chosen. ";
      cin >> choice;
      
      switch (choice)
      {
             case 'A':
                  {
                      cout << "Correct.\n";
                      ++correct;
                      break;
                  }
             default:
                     {
                            cout << "Incorrect. The answer is A.\n";
                            break;
                     }
      }
      
      cout << "20. 9090909090909090 * b^0 = x\n";
      cout << "A) \n";
      cout << "B) 9090909090909090\n";
      cout << "C) Not enough information\n";
      cout << "D) None of the above\n";
      cout << "Enter the letter of the answer you have chosen. ";
      cin >> choice;
      
      switch(choice)
      {
                    case 'B':
                         {
                             cout << "Correct.\n";
                             ++correct;
                             break;
                         }
                    default:
                            {
                                   cout << "Incorrect. The answer is B.\n";
                                   break;
                            }
      }
      
      double percentage = (correct / 20.0f) * 100;
      
      cout << "CONGRATULATIONS!! You have completed the test with a " << percentage << "%" << endl; 
      cin >> total;
      
      while (percentage >= 93)
      {
            cout << "You got an A" << endl;
      }
      while (percentage >= 90 && percentage <= 92)
      {
            cout << "You got an A-" << endl;
      }
      while (percentage >= 87 &&  percentage <= 89)
      {
            cout << "You got a B+" << endl;
      }
      while (percentage >= 83 && percentage <= 86)
      {
            cout << "You got a B" << endl;
      }
      while (percentage >= 80 && percentage <= 82)
      {
            cout << "You got a B-" << endl;
      }
      while (percentage >= 77 && percentage <= 79)
      {
            cout << "You got a C+" << endl;
      }
      while (percentage >= 73 && percentage <= 76)
      {
            cout << "You got a C" << endl;
      }
      while (percentage >= 70 && percentage <= 72)
      {
            cout << "You got a C-" << endl;
      }
      while (percentage >= 67 && percentage <= 69)
      {
            cout << "You got a D+" << endl;
      }
      while (percentage >= 60 && percentage <= 66)
      {
            cout << "You got a D" << endl;
      }
      while (percentage >= 0 && percentage <= 59)
      {
            cout << "You got an F" << endl;
      }                         
      
      system ("pause");
      return 0;
      }
        
Not sure what you quite mean, could you please rephrase what your problem is.
Also before you output the grade why do you try to take the variable "total" from cin?
when i run the program it does not run the while loops. so it will say the percent that the test taker got but not the letter grade. and if i dont put the cin >> total there it wont display the percentage ??
Why are you using switches? Your previous code used if conditions to check to see if the answer was right and was perfect for your application.

Also, you don't want while loops for your percentages, you want if conditions there as well.

This line of code is supposed to do what exactly?
1
2
     cout << "CONGRATULATIONS!! You have completed the test with a " << percentage << "%" << endl; 
      cin >> total;


I believe you want to remove the cin from there.

Also, here:
double percentage = (correct / 20.0f) * 100;

You can change this to:
int percentage = (correct * 5);

Or:
int percentage = (correct * 100 / 20);
Last edited on
I'm not sure how to fix your problem However, someone told me to just recheck all of my work when i had a problem i couldn't figure out.
I refined the code, made it more efficient and tidier to see what's going on and pretty much re-write the whole thing. It should've essentially worked exactly the same but i ended up doing something to fix it.

Basically go through your program, make it as efficient and well working as you can, then if you still get the same problem it's much easier to see and figure out what your code is doing!
Hi haNatootoo192,

@Volatile Pulse
Why are you using switches? Your previous code used if conditions to check to see if the answer was right and was perfect for your application.


Switchs are a much better idea than a load of else if statements if the case expression is constant as they are here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
cout << "20. 9090909090909090 * b^0 = x\n";
      cout << "A) \n";
      cout << "B) 9090909090909090\n";
      cout << "C) Not enough information\n";
      cout << "D) None of the above\n";
      cout << "Enter the letter of the answer you have chosen. ";
      cin >> choice;
      
      switch(choice)
      {
                    case 'B':
                         {
                             cout << "Correct.\n";
                             ++correct;
                             break;
                         }
                    default:
                            {
                                   cout << "Incorrect. The answer is B.\n";
                                   break;
                            }
      }


The other thing is that if you are using cout then you should use endl like this:

 
cout << "Correct." << endl;


You have done it correctly later in the code.

Now, you have a lot code here - a bunch of code that is kind of similar for each question asked. The code is not the same because the questions are different.

A more elegant way to do this is to have a 2 dimensional array that holds the questions and their answers . Loop through the array to ask the question and process the answers.

It will be harder to do, but a much better solution.

Let us know how you go.

TheIdeasMan

To be honest it's not hard to do at all. It'd be wise to have a separate file with the code to declare the 2xArray with [0][i] being the questions and [1][i] being the answers to the questions (the separate file to keep your code clear).

Then you have a simple for loop asking questions taken from the 2xArray and asking for an answer.

@TheIdeasMan@ Switches are only better if you're expecting at least 3 different comparisons, here we only want CORRECT or INCORRECT, i.e. true/false
An array of structs would be even better than a 2d array, now that I think about it a bit more. The struct would hold the Question and the answers as strings, and this setup would be easier to reference than 2d array.

That is a C approach.

The C++ approach would be to have the Questions and answers in a <vector>, then make a <list> of the vectors. Use an iterator to cycle through the list to ask the Q's and checks the A's and keep tabs on the score.

If this is an assignment, I am not sure what your teacher's view is, - it could be construed as cheating a bit because C++ has a lot of stuff that makes things like this fairly easy. The whole point of your course might be to learn about arrays, structs etc.

@TheIdeasMan@ Switches are only better if you're expecting at least 3 different comparisons, here we only want CORRECT or INCORRECT, i.e. true/false


Fair enough, I have switches on the brain at the moment - If you read some of my other posts you will see why.
Ok, would you mind advancing my knowledge and explain about structs please?
Ok a struct is like a record in a database 1 row of info. An array of structs is like a database table.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct Person {       //uses C++ string won't work in C
    string FirstName;   //The persons first name
    string LastName;   //The persons last name
    string DateOfBirth;  //in "dd/mm/yyyy" format
    unsigned AgeInYears; // The age in years as of today
    float Salary;               //Annual Pay in dollars
};

struct Person Me;
struct Person You;

Me.FirstName = "Fred";
Me.LastName = "Smith";
Me.DateOfBirth = "01/01/1900";
Me.AgeInYears = 112;
Me.Salary = 1000000.00;


Check out the reference documentation for some other examples. typedef is handy


Last edited on
Cheers (I think i sort of get it, not enough to use it yet but i'll do some research)
:)

EDIT: Right, i've found out it's like an object that's able to hold a group of different types of objects. Then I am able to create an instance of this object to hold my values. I assume that this is correct (I'm checking cos the info's from wikipedia) as it seems to fit in nicely with your example.

If so this is indeed a good idea of how to do this. However it's obviously more benifitial with the more variables that's needed.
Last edited on
You can also do pointers to structs, this is handy for you because you are going to have an array of structs and a reference to an array index is a pointer.

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
struct Person {       //uses C++ string won't work in C
    string FirstName;   //The persons first name
    string LastName;   //The persons last name
    string DateOfBirth;  //in "dd/mm/yyyy" format
    unsigned AgeInYears; // The age in years as of today
    float Salary;               //Annual Pay in dollars
};

struct Person Me;
struct Person You;

Me.FirstName = "Fred";
Me.LastName = "Smith";
Me.DateOfBirth = "01/01/1900";
Me.AgeInYears = 112;
Me.Salary = 1000000.00;

struct Person* pPersonObj ;  //a pointer to a Person struct
pPersonObj = &Me;  //pointer initialisation, it now points to the Me Person struct

cout >> "My First name is "  >> pPersonObj->FirstName >> endl;

//if you have an array of Person struct you can use the -> like this

PeopleArray[0]->FirstName = "Bevis";


Look for some examples on how to make an array of structs.

Yeah, i've been doing some reading of the downloadable pdf tutorial on this site.
It cover's quite a range including structs so i've learned enough about them to recognize what it does and how it's used in a program to be able to help if anyone has a problem (well, i can try :D )
Topic archived. No new replies allowed.