I am a bit confuse to where the last part of the program that asked me to calculate and display the number of correct and incorrect answers. Please help.
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
//Declare variables
int x,n,y,yourans,choice;
srand(time(0));
//Display menu
cout<<"----------Subjects menu----------\n";
cout<<"\t1. Addition\n";
cout<<"\t2. Substraction\n";
cout<<endl;
//Ask for choice and questions
cout<<"\tEnter your choice: ";cin>>choice;
cout<<"\tEnter the number of questions: ";cin>>n;
//questions
for (int i=1;i<=n;++i)
{
x=rand()%40+1; //random number from 1~39 for x
y=rand()%40+1; //random number from 1~39 for y
switch (choice)
{
case 1:yourans=x+y;
cout<<"\t\t"<<x<<" + "<<y<<" = ";cin>>yourans;
if (x+y==yourans)
{
cout<<"\t\t Correct\n";break;
}
else
{
cout<<"\t\t Wrong\n";break;
}
case 2:yourans=x-y;
cout<<"\t\t"<<x<<" - "<<y<<" = ";cin>>yourans;
if (x-y==yourans)
{
cout<<"\t\t Correct\n";break;
}
else
{
cout<<"\t\t Wrong\n";break;
}
cout<<endl;
}
}
//Count the number of correct & incorrect answers
cout<<"\tNumber of correct answers: "; //<------what must I do to have the program compute and display how many correct answers?
cout<<endl;
cout<<"\tNumber of wrong answer: "; //<------what must I do to have the program compute and display how many wrong answers?
cout<<endl;
add a two counter variables and do ++counter whenever a problem is right or wrong, then output those variables and your good to go. system("pause");remove that please.
ui uiho, I used dev C++ and system("pause"); is actually required or else the command prompt disappear without displaying the result. Just thought I should mention it in case hes also using it
that is wrong. read the second post in the beginner forum and that explains it. but system commands are never actually needed and are huge holes in the program.