My Totals aren't working

I'm sure its something i'm not seeing.

I the answers are not accumulating correctly which is not making my totals accumulate correctly.

For some reason the wrong works ok.

I added the last 2 lines just to see what i was getting for numbers.

I really only need a percentage rate. Any help is greatly appreciated.

Also when i submitted this it was all indented with tabs, what am i supposed to do so that my formatting will stay?


#include <cstdlib>
#include <ctime>
#include<iostream>
using std::cin;
using std::cout;

int main()
{

srand(time(0));

const int SPACES=100; // making space where name will go
char Name[SPACES]={};
cout<< "Please enter your name: ";
cin>> Name;

const int PLUS=0;// giving each mathamatical
const int MINUS =1; // operation a number to
const int DIVIDE=2; // initiate at op for random use
const int MULTIPLY=3;
const int MOD=4;
int a,b,op,userGuess,total;
int answer=0,wrong=0;
float myfloat=1.00, percentage=0;

// the for loop is created so that the switch then
// if else statements are selected 22 times
// before proceeding
for(int i=1 ; i<=10; i++)
{
a = rand() % 21; // range 0-20
b = (rand() % 20) + 1; //range 1-20
op = rand() % 5; //range 0-4 to match a operation

// this allows all operations and numbers
// to be pulled by the switch from
// the random selection above
switch (op)
{
case PLUS:
answer = a + b;
cout << "\n\n\nWhat is " << a << " + " << b << "? ";
cin >> userGuess;
break;

case MINUS:
answer = a - b;
cout << "\n\n\nWhat is " << a << " - " << b << "? ";
cin >> userGuess;
break;

case DIVIDE:
answer = a / b;
cout << "\n\n\nWhat is " << a << " / " << b << "? ";
cin >> userGuess;
break;

case MULTIPLY:
answer = a * b;
cout << "\n\n\nWhat is " << a << " * " << b << "? ";
cin >> userGuess;
break;

case MOD:
answer = a % b;
cout << "\n\n\nWhat is " << a << " % " << b << "? ";
cin >> userGuess;
}

// depending on the user input will depend on what
// if else statement is produced
if(answer==userGuess)
{
cout << "\n\nYou are correct.";
++answer;
}
else
{
cout<< " \n\nWrong answer, the correct answer is: "<< answer;
++wrong;
}
}
// a myfloat variable was initiated for 1.00 to help
// calculate what the percentage rate is this will
// make the answer a whole number. When total is
// divided into answer the decimal place needs to be
// moved 2 places to the right multiplying by 100
// does this which will then give you the correct percentage.

total=answer+wrong;
percentage = 100* (answer / (total/myfloat) );
cout << "\n\n\nYour grade is: " << percentage << "% \n";


cout << "\n\n\nYou answers: " << answer << " correctly \n";
cout << "\n\n\nthe total is: " << wrong << "\n";

return 0;
}
Last edited on
When you place code tags you get two sets of angle brackets with "code" and "code/". One goes at the start of your code the other at the end.
I just tried putting those tags on the line before on the same line for the beginning and end. It still looks the same. Its all very aggravating.
Last edited on
there are a few issues, your math for totals was all wrong, you are using the answer variable to hold both the answer as well as the number of right answers. if the last answer is 20 and i get it right then it will say i have 21 right.
you are mixing int and float with division, you need to use floating point numbers or cast them to floating point numbers.
make a small project and play with that, set a floating point number to two integers divided and see what you get. then try casting and etc..play with it to learn.

here is what i changed, look at the differences, you still have major issues in here though.
For one, you have division which can cause large floating point numbers. cout only shows a certain number of decimal places normally, you can change this with cout.precision(...)
but the problem is still there, some answers are long such as 6.3333333333
you can't expect someone to input that. you can use all integers maybe and drop all floating point numbers but it may look weird that way.


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
#include <iostream>
#include <time.h>
using namespace std;


int main()
{
///////////////////////////////////////////////////////////////////////
	
	srand(time(0));

	const int SPACES=100; // making space where name will go
	char Name[SPACES]={};
	cout<< "Please enter your name: ";
	cin>> Name;

	const int PLUS=0;// giving each mathamatical
	const int MINUS =1; // operation a number to
	const int DIVIDE=2; // initiate at op for random use
	const int MULTIPLY=3;
	const int MOD=4;
	const int numQuestions = 10;
	int op;
	float a,b;
	float right=0,wrong=0, total;
	float percentage=0;
	float answer, userGuess;

	// the for loop is created so that the switch then
	// if else statements are selected 22 times
	// before proceeding
	for(int i=1 ; i<=numQuestions; i++)
	{
		a = rand() % 21; // range 0-20
		b = (rand() % 20) + 1; //range 1-20
		op = rand() % 5; //range 0-4 to match a operation

		// this allows all operations and numbers
		// to be pulled by the switch from
		// the random selection above
		op = DIVIDE;
		a = 19;
		b = 3;
		switch (op)
		{
		case PLUS:
			answer = a + b;
			cout << "\n\n\nWhat is " << a << " + " << b << "? "<<answer<<": ";
			cin >> userGuess;
			break;

		case MINUS:
				answer = a - b;
			cout << "\n\n\nWhat is " << a << " - " << b << "? "<<answer<<": ";
			cin >> userGuess;
			break;

		case DIVIDE:
			answer = a / b;
			cout << "\n\n\nWhat is " << a << " / " << b << "? "<<answer<<": ";
			cin >> userGuess;
			break;

		case MULTIPLY:
			answer = a * b;
			cout << "\n\n\nWhat is " << a << " * " << b << "? "<<answer<<": ";
			cin >> userGuess;
			break;

		case MOD:
			answer = (int)a % (int)b;
			cout << "\n\n\nWhat is " << a << " % " << b << "? "<<answer<<": ";
			cin >> userGuess;
		}

		// depending on the user input will depend on what
		// if else statement is produced
		if(answer==userGuess)
		{
			cout << "\n\nYou are correct.";
			++right;
		}
		else
		{
			cout<< " \n\nWrong answer, the correct answer is: "<< answer << " not "<<userGuess;
			++wrong;
		}
	}
	// a myfloat variable was initiated for 1.00 to help
	// calculate what the percentage rate is this will
	// make the answer a whole number. When total is
	// divided into answer the decimal place needs to be
	// moved 2 places to the right multiplying by 100
	// does this which will then give you the correct percentage.
	cout << "\nright: "<<right<<"  wrong: "<<wrong<<endl;

	percentage = 100* (right/numQuestions);
	cout << "\n\n\nYour grade is: " << percentage << "% \n";

///////////////////////////////////////////////////////////////////////////
	system("PAUSE");
    return 0;
}


Thank you. I understand what I need to do fix this. I couldn't have done it without you. It works perfectly now.
Topic archived. No new replies allowed.