Math Game quiz is made with C++ program.
Steemian Friends,
Today, I will create a program to see my total score by answering five quizzes correctly using a C++ program. The purpose of creating my program is to write a blog, allowing me to practice, learn, and inspire other users. I have divided the program into thirteen steps. Anyone can create a C++ program by looking at each step. I have shown the results of five quizzes. I have named the program Math Game.
I first wrote the C++ program on a blank sheet of paper. Since I write it step by step on paper, the chances of making mistakes in the program are reduced. I will use the online compiler on my laptop to get the output from the blank sheet.
In a C++ program, the header file is always given at the beginning. There are many header files in a C++ program. I have memorized some header files because header files are required to write a C++ program. I wrote three header files for my program today.
iostream is used for input and output written through cout and cin.
cstdlib used for rand() & srand() functions.
The ctime header file is used to take time.
Using std: Because of the use of std, the program will work if you just write cout instead of std::cout.
using namespace std;
Every C++ program needs to have a main function. There are several main functions to declare in a program, depending on the type of program.
int main()
{
The main work of a program begins with the main function.
A C++ program has to declare variables. Declaring variables is done based on the function of the program.
int a, b, ans, userAns;
int score = 0;
a, b, ans, and userAns are variables. The program will count the number of integer types.
score=0 will start the score from zero.
You need to write a function to generate random numbers within the program.
srand(time(0));
This is very important. If it is not used, the same number will be displayed every time. The program will change the number by calculating the time.
Now I have given the program a title called Math Game. This is used here to see if any program has a title.
cout << "Math Game: Addition Quiz" << endl;
Some loops are used in C++ programs. Today I have used a for loop for my created program. By using this, the loop will run five times. But here, in place of five, we can change the program by using any number.
for(int i = 1; i <= 5; i++) {
In this part of the program, I have written a program to generate random numbers.
a = rand() % 10 + 1;
b = rand() % 10 + 1;
rand() will work with %10 from 0-9 and +1 from (1-10). a & b will be the values.
This part of the program will show the question and take the answer. For example, after writing 3+4=, the program will start working after I answer. Remember that the numbers will be of integer type.
cout << "\nQuestion " << i << ": ";
cout << a << " + " << b << " = ";
cin >> userAns;
The program will generate the correct answer in this part.
ans = a + b;
This part of the program looks at the answer and determines whether the answer is correct or incorrect through an if-else statement.
if(userAns == ans) {
cout << "Correct!" << endl;
score++;
} else {
cout << "Wrong! Correct answer: " << ans << endl;
}
This part of the program will show the output. If two out of 5 questions are correct, then the score will be 2/5, thus shown in the output.
cout << "\nYour Score: " << score << "/5" << endl;
Every C++ program uses a code to terminate it. To terminate the program, you need to write the following code.
return 0;
}
I had a problem with my code blocks programming software so I used an online compiler to get the output. After I clicked on RUN, the output showed 9+7=. After I gave the correct answer 16, the program executed and gave the correct answer. I have shown the screenshot below.
Then I see question 2 in the output. 1+1=? is given. I give 3 as the wrong answer. The program executes and shows the Wrong answer. I have shown it with the screenshot below.
Then I answered a total of five Math questions in this way. Out of my five answers, two are correct. So my score shows 2/5. I have shown it with the screenshot below.
Hopefully, any user will be able to create ten or twenty Math Quizzes with my C++ program. I'm leaving today by thanking everyone for watching.
.png)










This is my Twitter share link :
https://x.com/i/status/2008456661514715484
Congratulations! This post has been voted through steemcurator08 We support quality posts, good comments anywhere and any tags.