anyone knows how to solve the following question?

Does anyone know how to solve the following question?
Please take a look. Thank you !!

Question:
Modify SubtractionQuiz.cpp so that it will become a SimpleMathematicalQuiz.cpp that has the following features:

The SubtractionQuiz:

#include <iostream>
#include <ctime> // for time function
#include <cstdlib> // for rand and srand functions
using namespace std;

int main()
{
// 1. Generate two random single-digit integers
srand(time(0));
int number1 = rand() % 10;
int number2 = rand() % 10;

// 2. If number1 < number2, swap number1 with number2
if (number1 < number2)
{
int temp = number1;
number1 = number2;
number2 = temp;
}

// 3. Prompt the student to answer “what is number1 – number2?”
cout << "What is " << number1 << " - " << number2 << "? ";
int answer;
cin >> answer;

// 4. Grade the answer and display the result
if (number1 - number2 == answer)
cout << "You are correct!";
else
cout << "Your answer is wrong.\n" << number1 << " - " << number2
<< " should be " << (number1 - number2) << endl;

return 0;
}

i) A text menu will be displayed below to ask user to select an option: 1) Addition, 2) Subtraction, 3) Multiplication, 4) Exit.
ii) Then, after the user select option 1, 2 or 3, it will prompt user to input the number of operands (Maximum is 4 and minimum is 2).
iii) Then, the program will generate random numbers for the operands, which should be either one, two or three digits.
iv) It will display the formula and ask user to input the guessing answer.
v) Then, the program will evaluate whether the guessing answer is correct. If correct, state “It is correct.”; otherwise, state “It is wrong.” and give correct answer.
vi) Then, it will go back to i) to show text menu again which prompts user to select an option.
vii) At the end, if user selects option 4, it will show the total number of questions answered; the number of answers correct and wrong respectively and the mark (number of answers correct / total number of questions answered * 100).

Sample output: (Contents in bold type are input values)
1. Addition
2. Subtraction
3. Multiplication
4. Exit
Please select an option: 1
How many operands: 3
34 + 8 + 126
Please input your answer: 180
It is wrong! The answer should be 168

1. Addition
2. Subtraction
3. Multiplication
4. Exit
Please select an option: 3
How many operands: 2
56 * 28
Please input your answer: 1568
It is correct!

1. Addition
2. Subtraction
3. Multiplication
4. Exit
Please select an option: 2
How many operands: 3
86 – 7 - 139
Please input your answer: -50
It is wrong! The answer should be -60
Last edited on
Hello LoMoyi,


Does anyone know how to solve the following question?

Modify SubtractionQuiz.cpp so that it will become a SimpleMathematicalQuiz.cpp


No. You did not share the "SubtractionQuiz.cpp", so no one knows what needs modified.

If you want to modify a program then show the original program then what you have done or what youe are not understanding.

Andy
I've modified my question.

Thank you for reminding :)
Hello LoMoyi,

Much better.

I would start with this:

i) A text menu will be displayed below to ask user to select an option: 1) Addition, 2) Subtraction, 3) Multiplication, 4) Exit.
ii) Then, after the user select option 1, 2 or 3, it will prompt user to input the number of operands (Maximum is 4 and minimum is 2).


And the code would look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <ctime> // for time function
#include <cstdlib> // for rand and srand functions

using namespace std;  // <--- Best not to use.
// The most recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/

int main()
{
	// Display menu.

	// Get user input

	return 0;

	// 1. Generate two random single-digit integers
	//srand(time(0));
	srand(static_cast<size_t>(time(nullptr)));

This is the best way to write "srand" although the other will work.
This video is worth watching. It will have more information than you might not be ready for, but you will learn about the problems with using "rand" and maybe some ways to avoid them.

https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful

For now working on the menu and getting the menu choice should be easy to do. After that the "get input" part could be for the menu, but more for the initial user input, what would be step (ii).

Until you have something to work with the rest of the program could be just guess work.

I do have a question. Have you studied functions yet? This would be a big help if you can use them.

This may be a little ahead of what you know, but I like to put the menu in a loop, usually a do/while loop, and only leave the loop when a valid choice is made. I also like to do this in a function that returns a valid choice.

See what you can come up with down to the first return.

Andy
I haven't learn about function.

I will learn it in next chapter haha :)

But the most confusing part for me is 👇

iii) Then, the program will generate random numbers for the operands, which should be either one, two or three digits.

Anyway, I will check the resources you recommended.❤️

Thank you for your instruction and suggestions !!
Last edited on
A number that is 1, 2, or 3 digits is just another way of saying "a number between 0 and 999." So the code is:
int number = rand() % 1000;

With a maximum of 4 operands, I'd store the operands in an array. If you use number1, number2, number3, number4, you'll probably find the code getting really long.
Topic archived. No new replies allowed.