2016-09-28 51 views
-2

所以我正在用C++編寫一個小的Rock,Paper,Scissors遊戲結構,並且遇到了一些我不明白的錯誤。C++函數和奇怪的錯誤

首先是代碼期望一個';'在NumberToWord函數中,但它不應該是因爲它是一個函數。

另一個錯誤是它似乎不喜歡的其他錯誤之一。

也許我失去了一些東西,我不知道,但它應該是一個簡單的修復。

#include <iostream> 
#include <cstdlib> 
#include <iomanip> 
using namespace std; 

int main() 
{ 
    int seed = static_cast <int> (time(0)); //Sets the random seed 
    srand(seed); 

    int winCount = 0; 

    string numberToWord (int x) { 
     string outputChoice; 

     if (x == 0) { outputChoice = "Rock"; } 
     else if (x == 1) { outputChoice = "Paper"; } 
     else if (x == 2) { outputChoice = "Scissors"; } 

     return outputChoice; 
    } 

    while (winCount < 3) { 
     int computerChoice = rand() % 4; 
     int userChoice; 

     cout << userChoice << endl; 

     cout << "Please Enter 0 for Rock, 1 for Paper, or 2 for Scissors: "; //Asks for user input 
     cin >> userChoice; //Inputs user input to variable 

     if (userChoice == computerChoice) { 
      cout << "Compuer Choose: " << numberToWord(computerChoice) << endl; 
      cout << "You Choose: " << numberToWord(userChoice) << endl; 
      cout << "Draw!" << endl; 
     } 
     else if ((userChoice == 1) && (computerChoice == 2)) { //Rock v Paper 
      cout << "Compuer Choose: " << numberToWord(computerChoice) << endl; 
      cout << "You Choose: " << numberToWord(userChoice) << endl; 
      cout << "Compuer wins!" << endl; 
     } 
     else if ((userChoice == 1) && (computerChoice == 3)) { //Rock v Scissors 
      cout << "Compuer Choose: " << numberToWord(computerChoice) << endl; 
      cout << "You Choose: " << numberToWord(userChoice) << endl; 
      cout << "You win!" << endl; 
      winCount += 1; 
     } 
     else if ((userChoice == 2) && (computerChoice == 1)) { //Paper v Rock 
      cout << "Compuer Choose: " << numberToWord(computerChoice) << endl; 
      cout << "You Choose: " << numberToWord(userChoice) << endl; 
      cout << "You win!" << endl; 
      winCount += 1; 
     } 
     else if ((userChoice == 2) && (computerChoice == 3)) { //Paper v Scissors 
      cout << "Compuer Choose: " << numberToWord(computerChoice) << endl; 
      cout << "You Choose: " << numberToWord(userChoice) << endl; 
      cout << "Compuer wins!" << endl; 
     } 
     else if ((userChoice == 3) && (computerChoice == 1)) { //Scissors v Rock 
      cout << "Compuer Choose: " << numberToWord(computerChoice) << endl; 
      cout << "You Choose: " << numberToWord(userChoice) << endl; 
      cout << "Compuer wins!" << endl; 
     } 
     else if ((userChoice == 3) && (computerChoice == 2)) { //Scissors v Paper 
      cout << "Compuer Choose: " << numberToWord(computerChoice) << endl; 
      cout << "You Choose: " << numberToWord(userChoice) << endl; 
      cout << "You win!" << endl; 
      winCount += 1; 
     } 
    } 


    return 0; 
} 

感謝您的幫助!

Errors


第2部分

簡而言之程序不喜歡 '< <'。我在很多其他程序中使用這種方法來處理變量,但是這次我使用了一個字符串變量時會拋出一個錯誤。我查閱了C++字符串變量,看起來我正確地做了這件事,所以我不知道錯誤的原因。

參考文獻:

http://www.cplusplus.com/doc/tutorial/basic_io/

http://www.cplusplus.com/doc/tutorial/variables/

void displayOutput(int comp, int user, string winner) { 
    string compOutputChoice = ""; 
    string userOutputChoice = ""; 

    /* 
    if (comp == 0) { compOutputChoice = "Rock"; } 
    else if (comp == 1) { compOutputChoice = "Paper"; } 
    else if (comp == 2) { compOutputChoice = "Scissors"; } 

    if (user == 0) { userOutputChoice = "Rock"; } 
    else if (user == 1) { userOutputChoice = "Paper"; } 
    else if (user == 2) { userOutputChoice = "Scissors"; } 
    */ 

    cout << "Compuer Choose: " << compOutputChoice << endl; 
    cout << "You Choose: " << userOutputChoice << endl; 
    //cout << winner << endl; 

    return; 
} 

錯誤:

誤差(活性)沒有操作員 「< <」 32

誤差(一莫如)沒有操作員「< <」 33

錯誤C2679二進制「< <」:沒有操作員發現這需要類型的右邊的操作數‘的std :: string’(或沒有可接受的轉化率)32

錯誤C2679二進制「< <」:沒有操作員發現它接受一個右邊的操作數類型的‘的std :: string’(或沒有可接受的轉化率)33

+1

您應該在您的問題中逐字地發佈錯誤消息 - 如** text **,* not * image。 –

+4

你不能在'main()'函數體中定義另一個函數,除非它是一個lambda表達式。 –

+0

解決了一個問題,但是我仍然不能使用符合cout >>的函數來輸出它 – CTOverton

回答

1

功能string numberToWord (int x)嵌套在main函數內。這是無效的C++。

GCC編譯器確實支持嵌套函數作爲擴展,但它不是標準和其他編譯器(我知道)的一部分不接受它。只是不要那樣做。將函數移出main(或者,如果有意義的話,使其成爲lambda)。

1

問題很簡單。 numberToWord不能是main的內部函數。如果你使用的是較新的C++,請將它移到main之外或將其更改爲lambda。

auto numberToWord = [](int x) -> string { 
    string outputChoice; 

    if (x == 0) { outputChoice = "Rock"; } 
    else if (x == 1) { outputChoice = "Paper"; } 
    else if (x == 2) { outputChoice = "Scissors"; } 

    return outputChoice; 
}; 
+0

現在唯一的問題是我仍然不能使用: – CTOverton

+0

cout <<「Compuer選擇:」<< numberToWord(computerChoice)<< endl; – CTOverton

+0

@rext,你是什麼意思「仍然不能使用」。不適合你? –