我正在嘗試創建一個遊戲,其中使用1和9之間的數字的乘法問題會被重複詢問,直到有人犯錯。當他們錯誤時,程序應該顯示「oops」。C++隨機化和循環困難
我的代碼:
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int main()
{
srand (time(NULL));
int a= rand()%9+1;
int b= rand()%9+1;
int c;
while (c == a*b)
{
cout<<a<<" * "<<b<<"= ";
cin>>c;
}
cout<<"oops";
return 0;
}
我有兩個困難。首先,程序中會生成相同的「隨機」數字。其次,當有人犯錯時,不會顯示「oops」。
感謝您的幫助。
首選''標題。 –
chris
查看手冊中的「srand」功能。 –
現在你已經改變了代碼,它仍然可能會得到一個編譯器警告,並且根本不可能輸入while循環。 – chris