컴퓨터에서 사용자가 추측해야하는 임의의 숫자를 선택하는 게임을 작성 중입니다. 그런 다음 컴퓨터가 추측해야하는 숫자 (추측 한 숫자)를 선택하는 것은 사용자입니다. 문제는 내가 게임의 두 번째 부분에서 사용하는 rand() 함수가 때로는 새 범위 밖의 숫자를 제공한다는 것입니다. 예를 들어, 새로운 범위가 low = 4이고 high = 10 인 경우, rand()는 12를 제공하는데 이는 올바르지 않습니다. 나는 왜 그 오류를 이해하려고 노력하고 있지만 그것을 찾을 수 없습니다. 나는 Dev-C++에서 글을 쓰고 있습니다. 코드 바로 아래에 출력이 있습니다. 도와 주셔서 감사합니다.rand() 함수가 C++에서 제대로 작동하지 않습니다.
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main()
{
int computer_guess, nbr, nbrguesses, count_user_guess=0, count_computer_guess=0;
char user_comparison;
int low=1, high=99; //initialize low to the lower boundary and high to the higher boundary
srand(time(0));
nbr=rand()% 99 + 1; //computer chooses a random number between 1 and 99...
do
{
cout << "guess a number now! " << endl;
cin >> nbrguesses; //user enters the number he guessed...
if (nbrguesses>nbr)
cout << "too big" <<endl;
else if (nbrguesses<nbr)
cout << "too small" << endl;
else if (nbrguesses==nbr)
cout << " you found the correct number! " <<endl;
count_user_guess++; //count the number of guesses from the user
}
while (nbrguesses!=nbr);
cout << "you tried " << count_user_guess <<" times to find the right number " << endl<<endl;
cout << "----------------------------------------------------------------------" << endl;
cout << "Now, the computer will guess your number " << endl;
do
{
srand(time(0));
computer_guess=rand()% high + low; //computer guesses the number
cout << "computer guess: " << computer_guess <<endl;
cout << "is it the correct number?" << endl;
cin >> user_comparison; //if user enter
if (user_comparison=='>') // character '>' it means the number guessed by computer is too big
{
cout << "too big" <<endl;
high= computer_guess-1; //high is given a new boundary
cout << "***Current value of high = " << high << " and low = " << low << endl; //display current boundaries
}
else if (user_comparison=='<') //the number guessed by computer is too small
{
cout << "too small" << endl;
low= computer_guess+1; //low is given a new boundary
cout << "***Current value of low = " << low << " and high = " << high << endl; //display current boundaries
}
else if (user_comparison=='=')
cout << "Computer found the correct number! " <<endl;
count_computer_guess++; //count number of guesses from computer
}
while (user_comparison!='=');
cout << "The computer tried " << count_computer_guess <<" times to find the right number " << endl;
cout << "The Game is over now, have a good day!" << endl;
getch();
return 0;
}
//**************Output******************************************
guess a number now!
50
too big
guess a number now!
25
you found the correct number!
you tried 2 times to find the right number
----------------------------------------------------------------------
Now, the computer will guess your number
computer guess: 11
is it the correct number?
>
too big
***Current value of high = 10 and low = 1
computer guess: 3
is it the correct number?
<
too small
***Current value of low = 4 and high = 10
computer guess: 12
is it the correct number?
그건 스택 오버플로 질문에 대한 ** 너무 많은 코드입니다. 문제가'rand '와 관련된다고 생각된다면 문제를 설명하기 위해 짧은 (즉, <10 줄) 테스트 프로그램을 만들어야합니다. –
'rand'는 확실히 여기서 문제가되지 않습니다. – ildjarn