This is a simple C++ program I made. This program is designed to help a elementa
ID: 3886168 • Letter: T
Question
This is a simple C++ program I made.
This program is designed to help a elementary student learn the multiplication tables.
The program will output two positive one digit integers and then, the student types the answer.
If it is correct, the program will print "Very good".
If it is incorrect, the program will print "No. Please try again." and will ask the same question.
Everything works correctly. However, I need to put a programmer defined function besides rand() inside the program and I don't know how to do that.
Please Help.
#include
#include
#include
#include
using namespace std;
int main()
{
char yes;
int answer, digit1, digit2, run, sentinel = -1, temp1, temp2;
srand((unsigned int)time(NULL));
cout << "This test will help you learn the multiplication tables from 0 to 9 :D" << endl;
cout << "Enter -1 to terminate the program anytime." << endl;
cout << " Type Y to continue: ";
cin >> yes;
if (yes != 'Y' && yes != 'y')
{
cout << "Error. Please type Y to continue: ";
cin >> yes;
}
do
{
digit1 = rand() % 9 + 0;
digit2 = rand() % 9 + 0;
temp1 = digit1;
temp2 = digit2;
cout << " How much is " << digit1 << " x " << digit2 << ": ";
cin >> answer;
if (answer == sentinel)
return 0;
if (answer == digit1*digit2)
cout << "Very good!" << endl;
if (answer != digit1*digit2)
{
do
{
cout << "No. Please try again." << endl;
cout << " How much is " << temp1 << " x " << temp2 << ": ";
cin >> answer;
} while (answer != temp1*temp2);
}
} while (answer != sentinel);
return 0;
#include iostream» #include #include #include using namespace std; in main() char yes; int, answer, digit!, digit2, run, sentinel = -1, temp1, temp2; scand.(unsigned int)time (NULL)) SoutExplanation / Answer
Hi
I have added a user defined function to handle. Highlighted the code changes below
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <time.h>
using namespace std;
void checkAnswers() {
int answer, digit1, digit2,temp1, temp2, sentinel=-1;
digit1 = rand() % 9 + 0;
digit2 = rand() % 9 + 0;
temp1 = digit1;
temp2 = digit2;
cout << " How much is " << digit1 << " x " << digit2 << ": ";
cin >> answer;
if (answer == sentinel)
exit(0);
if (answer == digit1*digit2)
cout << "Very good!" << endl;
if (answer != digit1*digit2)
{
do
{
cout << "No. Please try again." << endl;
cout << " How much is " << temp1 << " x " << temp2 << ": ";
cin >> answer;
} while (answer != temp1*temp2);
}
}
int main()
{
char yes;
int answer, run, sentinel = -1;
srand((unsigned int)time(NULL));
cout << "This test will help you learn the multiplication tables from 0 to 9 :D" << endl;
cout << "Enter -1 to terminate the program anytime." << endl;
cout << " Type Y to continue: ";
cin >> yes;
if (yes != 'Y' && yes != 'y')
{
cout << "Error. Please type Y to continue: ";
cin >> yes;
}
do
{
checkAnswers();
} while (answer != sentinel);
return 0;
}
Output:
This test will help you learn the multiplication tables from 0 to 9 :D
Enter -1 to terminate the program anytime.
Type Y to continue: Y
How much is 1 x 8: 8
Very good!
How much is 1 x 5: 5
Very good!
How much is 5 x 5: 24
No. Please try again.
How much is 5 x 5: 25
How much is 4 x 5: -1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.