-
Flash card
How can I add the following conditions:
1) Make the child ask for a new problem.
2) Display score as percent (numberRight/numberAttempted*100)
3) Ability child to end the program.
Code:
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,i;
int x,y;
int answer;
int wrong=0;
cout << "Answer Each of the Following: \n";
n=1;
for(i=0;i<n;i++)
{
x=(rand()%10);
// select two random numbers to ask
y=(rand()%10);
cout << x << " * " << y << " = ";
cin >> answer; // get the answer
if(answer!=x*y)
{
cout << "Wrong. Correct answer = " <<
x*y << endl;
wrong=wrong+1;
} // if statement
} // for loop
cout << "Score = " << (n-wrong)*100/n << "%\n";
cout << "Do you want to play again?";
/* char k;
cout << "Do you want to play again?";
cin >> k;
if(k=="Y")
{
cout << x << " * " << y << " = ";
cin >> answer; // get the answer
}
else
{
break;
}
*/
return 0;
} // main
Thanks
-
>1) Make the child ask for a new problem.
>3) Ability child to end the program.
You have to *have* a child first. I don't see any forkage going on in your code. Or by 'child' do you mean 'user'? Just so you know, in programming terminology, 'child' is used to mean a new process forked off of a parent process using fork or CreateProcess, or something similar. Anyway, throw everything in a loop and provide a menu system:
Code:
int main ( ) {
...
while (true) {
cout<<"Quit? ";
if ((answer = cin.get()) && (answer == 'y' || answer == 'Y'))
break;
cin.ignore(); // Die, little newline, torment us no further!
...
}
...
}
Just so you know, the ...'s mean 'the rest of your code goes here'.
-
>>if(k=="Y")
should be
>>if(k=='Y')
note the single quotes instead of double, which are used when comparing a single character.