Infinite loop

I have a code like this. When I enter the number like 2345567, the prompt prints out the answer, but it doesn't stop until I exit the prompt. What does it mean?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
	cout << "Guessing game! The computer will choose randomlly the numbers, and you have to guess!\n";
	int guess;
	srand(time(NULL));
	int num = rand() % 100;
	cout << "Enter your guess: ";
	cin >> guess;
	if (guess == num)
	{
		cout << "Woo hoo! You're right! We have a smart guy!\n";
	}
	else
	{
		cout << "You fool! Wrong!\n";
		if (guess < num)
		{
			cout << num << " is the number!\n";
			cout <<"You guess the lower number! You idiot!\n";
		}
		if (guess > num)
		{
			cout << num << " is the number!\n";
			cout <<"Imbecile! You guess the higher number!\n";
		}
	}
	cout << endl;
	main();
	return 0;
}
Last edited on
You shouldn't call main again. Do you want to ask the player if he wants to play again?
You are calling main unconditionally from main. That is endless recursion.

Aside from that, you are not allowed to call main in any circumstance. Doing so results in undefined behavior.
No, I meant to use main (); return 0;. It prompt: You fool! Wrong! Imbecile! You guess the higher number! many times, and it won't stop print that sentence when I enter a long numberlike 7896344
When you play GTA Vice City, sometimes it says: "You kill the woman you fool!" or "You fool! We need that car!"
Sorry, I missed the # when I copied
You can't use main () again its not correct. if you want to enter a new number put it in a while loop like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
while(true)
{
cout << "Guessing game! The computer will choose randomlly the numbers, and you have to guess!\n";
	int guess;
	srand(time(NULL));
	int num = rand() % 100;
	cout << "Enter your guess: ";
	cin >> guess;
	if (guess == num)
	{
		cout << "Woo hoo! You're right! We have a smart guy!\n";
	}
	else
	{
		cout << "You fool! Wrong!\n";
		if (guess < num)
		{
			cout << num << " is the number!\n";
			cout <<"You guess the lower number! You idiot!\n";
		}
		if (guess > num)
		{
			cout << num << " is the number!\n";
			cout <<"Imbecile! You guess the higher number!\n";
		}
	}
	cout << endl;
}
Hey, who reported gentleguy's comments?
No. I mean when I enter a long number like 3939086, it promts:

You fool! Wrong!
Imbecile! You guess the higher number!

And it doesn't let me to enter another number, it just runs that lines over and over again!
closed account (E0p9LyTq)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <chrono>
#include <random>

int main()
{
   // obtain a seed from the system clock:
   unsigned seed = static_cast<unsigned> (std::chrono::system_clock::now().time_since_epoch().count());

   // seeds the random number engine, using the default random engine
   std::default_random_engine generator(seed);

   // discard values for better randomization
   generator.discard(generator.state_size);

   // set a distribution range (1 - 100)
   std::uniform_int_distribution<int> distribution(1, 100);

   // pick and store the random number
   int numPicked = distribution(generator);

   std::cout << "Welcome to the number guessing game!!!\n";
   std::cout << "I have picked a number between 1 and 100.\n\n";

   int guess    = 0; // stores the number the user guessed
   int guessNum = 0; // stores the number of guesses

   for (guessNum = 0; guess != numPicked; guessNum++)
   {
      std::cout << "What would you like to guess? ";
      std::cin >> guess;

      if (guess < numPicked)
      {
         std::cout << "\nYou guessed too low!!!\n\n";
      }
      else if (guess > numPicked)
      {
         std::cout << "\nYou guessed too high!!!\n\n";
      }
   }
   std::cout << "\nYou guessed it!!!\n"
      << "It took you " << guessNum << " guesses.\n";
}
Topic archived. No new replies allowed.