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?
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace 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;
}
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
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;
}
#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";
}
elseif (guess > numPicked)
{
std::cout << "\nYou guessed too high!!!\n\n";
}
}
std::cout << "\nYou guessed it!!!\n"
<< "It took you " << guessNum << " guesses.\n";
}