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.
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.
#include <iostream>
#include <time.h>
usingnamespace std;
int main()
{
///////////////////////////////////////////////////////////////////////
srand(time(0));
constint SPACES=100; // making space where name will go
char Name[SPACES]={};
cout<< "Please enter your name: ";
cin>> Name;
constint PLUS=0;// giving each mathamatical
constint MINUS =1; // operation a number to
constint DIVIDE=2; // initiate at op for random use
constint MULTIPLY=3;
constint MOD=4;
constint 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;
}