C++ program to show questions until the user gives the wrong answer


I am sure you all are very clear about infinite loops that we have already discussed in our previous post. We discussed how while true can be used to implement an infinite loop in c++. The majority thinks that there is no logical need for while true in c++ programming. Besides, they think infinite loops are not worthy to use. But I think there are some situations where while true and infinite loops can help us reduce complexity. So in this article, I want to demonstrate the importance of while true in c++ programming with a C++ program to show questions until the user gives the wrong answer. This program is a simple c++ game that can help us understand when to use while true or infinite loops. Let's get started.

C++ program to show questions until the user gives the wrong answer

For the purpose of demonstration, we want to write a c++ program to show questions. The questions will consist of two random numbers and the user will be asked to answer their sum. The program will keep on asking questions until he is answering correctly. When he gives the wrong answer, the game will end showing the message that your answer is wrong. In other words, we will break the infinite loop when he gives the wrong answer.


#include <iostream>
#include <stdlib.h>// for generating random number
#include <time.h> // for using time ftn
using namespace std;
int main() {
//1)------------declaring variables--------------

 int num1,num2, answer;
 srand(time(0));
//2)------------creating while true loop--------------
while (true) {
  int sum = 0;
//3)------------generating and saving random numbers in variables------------
  num1=rand() % 1000 + 1;
  num2 = rand() % 1000 + 1;
//4)------------displaying question-----------
  cout << "What is the sum of " << num1 << "and " << num2<<"?" << endl;
  sum = num1 + num2;
//5)------------getting and comparing answer----------------
  cin >> answer;

  if (answer == sum) {
   cout << "hurrah!your answer in correct" << endl;
  }
  else {
   cout << "alas!your answer is wrong" << endl;
   cout << "game over" << endl;
   break;
  }
 }
 system("pause");
 return 0;
}


There are a few things that I think I must address before starting the explanation of the code. We used stdlib and time.h library to use their built-in functions for generating random numbers.

Rand() function

This function is responsible for generating random numbers. We use the modulus operator to limit the random numbers up to a specific number e.g generate random numbers less than 100. We use +1 to avoid zero as a random number. This function is available in stdlib library.

Srand() function

This function seeds the random numbers used by rand(). This function is helpful if you want to get different random numbers on every program call. The best practice of calling this function is to avail the call to srand(time(0)). Time() function returns a value which varies every time and allows us to get varying random numbers on every call.

I hope you got the basic idea about the built-in functions. Now let's look at the code.

If you look at the above code, you can understand that while true is crucial in this situation because we have no condition to control the loop. The above code is divided into few parts to make it easy to understand.
  • First of all, we declared variables num1, num2, and answer. Num1 and num2 are declared to hold two random numbers.
  • Then we wrote while true infinite loop. We also took one more variable sum to store the sum of two random numbers.
  • Thirdly, we generated and stored the random numbers using rand() function in variable num1 and num2.
  • Then we displayed the question to the user to answer. In addition, we also calculated the sum of num1 and num2.
  • In the last part, we took input from the user in variable answer and then compared it with sum using if-else. If the value answer is equal to sum then the program prints that your answer is correct. If the value of answer is not equal to sum, the program prints that your answer is not correct. 
Output of the program

That's all about while true. We can conclude that while true or infinite loops are useful when there is no condition to control the loop. We can break the loop using if-else when some undesired action is done. In our above demonstration, the undesired action was the wrong answer. Hope you understood the concept and my point. thanks for reading and supporting us.


  




Post a Comment

0 Comments