所以我正在用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;
}
感謝您的幫助!
第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
您應該在您的問題中逐字地發佈錯誤消息 - 如** text **,* not * image。 –
你不能在'main()'函數體中定義另一個函數,除非它是一個lambda表達式。 –
解決了一個問題,但是我仍然不能使用符合cout >>的函數來輸出它 – CTOverton