[英]C++ guessing game problem with random numbers
Whenever a number is given as an input, it always say too low even if I put negative value while the less and max random number is 1-100
.每当给出一个数字作为输入时,它总是说太低,即使我输入负值,而 less 和 max 随机数是1-100
。 I don't really understand why it is not working.我真的不明白为什么它不起作用。
#include<iostream>
Is there a problem in here?这里面有问题吗?
#include<cstdlib>
#include<time.h>
above以上
using namespace std;
void again();
void play_game();
void again()
{
int schoice;
while(true)
{
cout<<"\n\nYou want to play again?\n\n";
cout<<"1) Play Again"<<endl;
cout<<"0) Exit\n\n";
cout<<"Input answer:";
cin>>schoice;
if(schoice == 1)
{
play_game();
}
else if(schoice == 0)
{
cout<<"\nOh ok see ya!\n\n";
break;
}
}
}
void play_game()
{
Or the problem is in here?还是问题出在这里?
srand(time(0));
int RanNum;
RanNum = rand() % 100 + 1;
This part I don't really understand (above)这部分我不太明白(上)
cout<<"\n\n*The game is being played*\n\n"<<endl;
cout<<"Guess the number between 1 to 100\n"<<endl;
while(true)
{
cout<<"Input number:";
int maxNum = 100;
int leastNum = 1;
int guess;
cin>>guess;
if(guess < leastNum || guess > maxNum)
{
cout<<"It must be higher or equal than"<<" "<<leastNum<<endl<<"and it must not exceed"<<" "<<maxNum<<endl;
}
else if(guess < RanNum)
{
cout<<"\nYour number is LOW\n"<<endl;
}
else if(guess > RanNum)
{
cout<<"\nYour number is HIGH\n"<<endl;
}
else
{
cout<<"\nYour correct!";
again();
break;
}
}
}
int main()
{
int choice;
cout<<"Want to play the game?\n\n"<<endl;
cout<<"Type 1 if want to play"<<endl<<"Type 0 if not\n\n"<<endl;
cout<<"Input here:";
cin>>choice;
switch(choice)
{
case 1 :
play_game();
break;
case 0 :
cout<<"\nOh ok see ya!\n\n";
break;
default :
cout<<"\n\nThe choices are\n\n1 or 0 \n\nplease try again\n\n";
break;
}
return 0;
}
I copied the code and everything worked just fine on my end.我复制了代码,最后一切正常。 Maybe try recompiling it and making sure you are saving and compiling the updated version of the code.也许尝试重新编译它并确保您正在保存和编译代码的更新版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.