Hello guys I'm new here this is my first post. I'm also new to c++ and programming in general so don't be to hard on me if my code is sloppy. I'm taking my first programming course in my college career so yes this is a question about my homework I did read the rules about homework but if this post isn't within the rules let me know. Ok now that I've given you some background that you probably don't care about here is my code:
Code:
#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
using namespace std;
int main ()
{
int c,i,y,x; // c is menu choice x and y are random numbers
double a; // user answer
int t=0; // to track number of correct answers
char repeat='y';
srand(time(NULL) + getpid()); // seed random number from current time
while (repeat == 'y')
{
// menu
cout << "Which math skill would you like to test? (Press a number between 0 and 4)" << "\n";
cout << "0. Quit" << "\n";
cout << "1. Addition" << "\n";
cout << "2. Subtraction" << "\n";
cout << "3. Multiplication" << "\n";
cout << "4. Division ";
cin >> c;
if (c>4 || c<0) // display an error if there is an invalid choice
{
cout << "Error! Terminating program." << endl;
}
else if (c == 0) // quit the program
{
cout << "Quitting program. Goodbye!" << endl;
}
for (i=0; i<4; i++) // repeat statements 4 times
{
x=((int)rand() % 10+1);
y=((int)rand() % 10+1);
if (c == 1) // addition
{
cout << x << " + " << y << " = ";
cin >> a;
if (a == x + y)
{
cout << "Correct!" << endl;
t++;
}
else if (a != x + y)
{
cout << "Incorrect!" << endl;
}
}
else if (c == 2) // subtraction
{
cout << x << " - " << y << " = ";
cin >> a;
if (a == x - y)
{
cout << "Correct!" << endl;
t++;
}
else if (a != x - y)
{
cout << "Incorrect!" << endl;
}
}
else if (c == 3) // multiplication
{
cout << x << " * " << y << " = ";
cin >> a;
if (a == x * y)
{
cout << "Correct!" << endl;
t++;
}
else if (a != x * y)
{
cout << "Incorrect!" << endl;
}
}
else if (c == 4) // division
{
cout << x << " / " << y << " = ";
cin >> a;
if (a == x / y)
{
cout << "Correct!" << endl;
t++;
}
else if (a != x / y)
{
cout << "Incorrect!" << endl;
}
}
}
cout << t << " correct out of 4" << endl; // display number of correct answers
cout << "Would you like to try again? (y/n) "; // prompt user to repeat or quit program
cin >> repeat;
}
}
As you see the program is pretty simple. The problem I am having is that if the user inputs 0 or 0>c>4 then it still displays the amount correct and gives them the option to repeat the program. If they press 0 it should completely quit the program and if they input anything less than 0 or greater than 4 it should ask them if they would like to repeat, but not display the total correct. I have tried placing the last two cout statements inside of the for (i=0; i<4; i++) braces but that just repeated the total correct four times. I'm not sure where to place these statements or how I should edit my code. Thanks in advance for any help!