Alright first off, here's my code:
Code:
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
void greeting ();
int goodresponses ();
int badresponses ();
int x = rand() % 10 + 1;
int y = rand() % 10 + 1;
int ctr=0;
int rightanswer=1;
int totalanswer=1;
int wronganswer=1;
int answer;
int guess;
char decision;
int main ()
{
srand(time(0));
greeting ();
prob:
int arithmetic;
int x = rand() % 10 + 1;
int y = rand() % 10 + 1;
while (ctr<=3 && guess != answer) {
arithmetic = rand() % 4 + 1;
switch(arithmetic)
{
case 1:
cout << "What is " << x << " + " << y << "?" << endl;
int answer= x + y;
break;
case 2:
cout << "What is " << x << " - " << y << "?" << endl;
int answer= x - y;
break;
case 3:
cout << "What is " << x << " * " << y << "?" << endl;
int answer= x * y;
break;
case 4:
cout << "What is " << x << " / " << y << "?" << endl;
int answer=(x*y)/x;
break;
}
cin >> guess;
if (guess == answer){
goodresponses();
rightanswer++;
totalanswer++;
}
else {
badresponses();
cin >> guess;
if (guess != answer){
badresponses();}
else {
goodresponses();
}
wronganswer++;
totalanswer++;
}
cout << "Another problem (y/n)?" << endl;
cin >> decision;
if (decision=='y'){
goto prob;
}
else {
goto esc;
}
}
esc:
cout << "Here is your score:" << endl;
float finalscore= rightanswer/totalanswer;
cout << rightanswer << " right," << wronganswer<< " wrong==="<< finalscore;
return 0;
}
void greeting () {
cout << "***Welcome to Math-o-matic!***" << endl;
}
int goodresponses () {
int goodresponses = rand() % 4 + 1;
switch(goodresponses)
{
case 1:
cout << "Very good!" <<endl;
break;
case 2:
cout << "Excellent!" <<endl;
break;
case 3:
cout << "Nice work!" <<endl;
break;
case 4:
cout << "Way to go!" << endl;
break;
}
}
int badresponses () {
int badresponses = rand() % 4 + 1;
switch(badresponses)
{
case 1:
cout << "No. Please try again." <<endl;
break;
case 2:
cout << "Wrong. Try again." << endl;
break;
case 3:
cout << "Don't give up!" << endl;
break;
case 4:
cout << "No. Keep trying." << endl;
break;
}
}
I have to store all four possibile types of answers into one variable, so I wanted to store that all into int answer, the problem is that what I have doesn't work. I thought it would because it's in a random generator so only one of those four will be picked, but apparantly my program wants to assign all of those things to int answer and so the program doesn't compile. Is there a command I have to type somewhere to clear int answer of what was inside of it before the loop goes through again? That's the only solution I can think of but I haven't learned anything like that in C++ yet that I know of.