2015-12-21 108 views
0

我編寫的以下程序提示用戶輸入一個數字1 - 100.該程序有一個來自隨機數發生器的預選號碼,用戶需要猜測。如果用戶的猜測過高或過低,程序會通知用戶,直到用戶的猜測是正確的。我的程序運行良好,但是當提示輸入一個數字時,我必須在某些情況下輸入數字四次或更多次才能提示我。有人能讓我看看或幫我找到我的錯誤嗎?C++:提示需要多次輸入數據才能繼續

#include <iostream> 
#include <ctime>//A user stated that using this piece of code would make a true randomization process. 
#include <cstdlib> 
using namespace std; 

int main()//When inputing a number, sometimes the user has to input it more than once. 
{ 
    int x; 
    int ranNum; 
    srand(time(0)); 

    ranNum = rand() % 100 + 1; 

    cout << "Please input your guess for a random number between 1 - 100.\n"; 
    cin >> x; 

    while (x > ranNum || x < ranNum) 
    { 
     { 
      if (x > ranNum) 
       cout << "Your input was greater than the system's generated random number. Please try again.\n"; 
      cin >> x; 
      if (x > 100 || x < 1) 
       cout << "Input invalid. Please input a number between 1 - 100.\n"; 
      cin >> x; 
     } 
     { 
      if (x < ranNum) 
       cout << "Your input was less than the system's generated random number. Please try again.\n"; 
      cin >> x; 
      if (x > 100 || x < 1) 
       cout << "Input invalid. Please input a number between 1 - 100.\n"; 
      cin >> x; 
     } 
     { 
      if (x == ranNum) 
       cout << "You guessed right!\n"; 
     } 
    } 

    return 0; 
} 
+1

'while(x> ranNum || x

回答

1

您的括號內的地方不對。一個如果具有多發線的說法應該是在

if (condition) 
{ 
    code1 
    code2 
    code3 
    ... 
} 

形式,你有什麼是

{ 
    if condition 
     code1 
     code2 
     code3 
     ... 
} 

所以code1當條件是真實的,但code2code3其餘的將運行將只運行不有什麼關係。縮進不包括C++中的任何內容。我們可以有(請不要這樣做):

  if (condition) 
    { 
code1 
code2 
     code3 
} 

而且所有三行將在if語句中運行。

您的代碼修正爲括號在正確的位置將是

while (x != ranNum) 
{ 
    if (x > ranNum) 
    { 
     cout << "Your input was greater than the system's generated random number. Please try again.\n"; 
     cin >> x; 
     if (x > 100 || x < 1) 
     { 
      cout << "Input invalid. Please input a number between 1 - 100.\n"; 
      cin >> x; 
     } 
    } 
    if (x < ranNum) 
    { 
     cout << "Your input was less than the system's generated random number. Please try again.\n"; 
     cin >> x; 
     if (x > 100 || x < 1) 
     { 
      cout << "Input invalid. Please input a number between 1 - 100.\n"; 
      cin >> x; 
     } 
    } 
    if (x == ranNum) 
     cout << "You guessed right!\n"; 
} 

我也改變while (x > ranNum || x < ranNum)while (x != ranNum)因爲我們只想要運行的循環,而x不等於ranNum

+0

正確,並且在嵌套條件中有一個類似的實例。 OP似乎期望縮進具有語義。 –

+0

感謝您的幫助。將括號放在正確的位置解決了我遇到的問題。 –

+0

@BillFisher沒問題。如果這裏或其他答案解決了這個問題,你可以考慮通過點擊複選標記來接受它。這表明答案解決了您的問題。 – NathanOliver

0

你請勿正確使用if對帳單:

{ 
    if (x > ranNum) 
     cout << "Your input was greater than the system's generated random number. Please try again.\n"; 
    cin >> x; 
    if (x > 100 || x < 1) 
     cout << "Input invalid. Please input a number between 1 - 100.\n"; 
    cin >> x; 
} 

Both 0上面的條件不會影響任何內容,但輸出消息是否被打印,並且周圍的花括號也是無用的。當空調的代碼包含比單個語句多,你需要將其包裝成花括號:

{ 
    if (x > ranNum) 
    { 
     cout << "Your input was greater than the system's generated random number. Please try again.\n"; 
     cin >> x; 
    } 
    if (x > 100 || x < 1) 
    { 
     cout << "Input invalid. Please input a number between 1 - 100.\n"; 
     cin >> x; 
    } 
} 

也有在重複的代碼讀取輸入並執行範圍檢查是沒有意義的。下面看起來更直接對我說:

int main() 
{ 
    int ranNum; 
    srand(time(0)); 

    ranNum = rand() % 100 + 1; 

    cout << "Please input your guess for a random number between 1 - 100.\n"; 
    cin >> x; 

    int x; 
    for (;;) // Endless loop 
    {     
     cin >> x; 

     if (x > 100 || x < 1) 
     { 
      cout << "Input invalid. Please input a number between 1 - 100.\n"; 
      continue; // Jump to next iteration of the loop 
     }            

     if (x == ranNum)        
     { 
      cout << "You guessed right!\n"; 
      break; // Leave the loop 
     } 

     if (x > ranNum) 
      cout << "Your input was greater than the system's generated random number. Please try again.\n"; 
     else 
      cout << "Your input was less than the system's generated random number. Please try again.\n"; 
    } 

    return 0; 
} 
0

您的代碼看起來是這樣的:

#include <iostream> 
#include <ctime>//A user stated that using this piece of code would make a true randomization process. 
#include <cstdlib> 
using namespace std; 

int main()//When inputing a number, sometimes the user has to input it more than once. 
{ 
    int x; 
int ranNum; 
srand(time(0)); 

ranNum = rand() % 100 + 1; 

cout << "Please input your guess for a random number between 1 - 100.\n"; 


while (x > ranNum || x < ranNum) 
{ 
    cin >> x; 
    if (x > ranNum && x<=100 & x>=1) cout << "Your input was greater than the system's generated random number. Please try again.\n"; 
    else if (x < ranNum && x<=100 & x>=1) cout << "Your input was smaller than the system's generated random number. Please try again.\n"; 
    else if (x>=100 || x<=1) cout << "Input invalid. Please input a number between 1 - 100.\n"; 

} 
cout << "You guessed right!\n"; 

    return 0; 
} 

在未來:

當您使用while循環,它包括if語句在裏面。使用正確。另外,嘗試nt來嵌套if語句,而不是使用邏輯運算符。

+0

在while循環條件下評估之前,您忘記了初始化x。 – NathanOliver

相關問題