2014-01-18 61 views
-2

我是一名初學者,我無法找到我在這個岩石剪刀程序中做錯了什麼。有人可以幫忙嗎?以下是我的代碼:C++中的岩石紙剪刀程序

我創建了兩個函數one userchoice(),它接受來自用戶的輸入並返回相應的字符串。第二個是compchoice(),隨機數從1到3生成,並且返回對應的字符串,然後我調用這個函數並將該值保存在一個while循環中比較的變量中,以查看用戶輸入是否與計算機匹配。

using namespace std; 

string compchoice(); 
string userchoice(); 
int main() { 
    //cout<<"computer picks a choice here"<<compchoice()<<endl; 
    //cout<<"pick your choice "<<userchoice()<<endl; 
    string comp, user; 
    comp = compchoice(); 
    user = userchoice(); 
    while (userchoice() != compchoice()) { 
     if (userchoice() != compchoice()) { 
     cout << " go again \n\t"; 
     comp = compchoice(); 
     user = userchoice(); 
     } else { 
     cout << "congratulations\t computer choice was " << comp << 
       " user choice is " << user; 
     //comp = compchoice(); 
     //user = userchoice(); 
     } 
    } 
} 
string compchoice() { 
    srand(time(0)); 
    int ch; 
    string choiceStr; 
    ch = rand() % 3 + 1; 
    switch (ch) { 
     case 1: 
     choiceStr = "rock"; 
     break; 
     case 2: 
     choiceStr = "paper"; 
     break; 
     case 3: 
     choiceStr = "scissors"; 
     break; 
     default: 
     cout << "computer menu.... existing"; 
     //break; 
    } 
    return choiceStr; 
} 
string userchoice() { 
    string choiceStr; 
    int choice; 
    cout << " 1. rock \n"; 
    cout << " 2. paper \n"; 
    cout << " 3. scissors \n"; 
    cout << " user menu make selection : \n"; 
    cin >> choice; 
    cout << endl; 
    switch (choice) { 
     case 1: 
     choiceStr = "rock"; 
     break; 
     case 2: 
     choiceStr = "paper"; 
     break; 
     case 3: 
     choiceStr = "scissors"; 
     break; 
     default: 
     choiceStr = "enter only 1, 2, or 3"; 
     //break; 
    } 
    return choiceStr; 
} 
+1

有無數個'石頭紙scissor'上做題(我猜它必須是一個共同的功課問題)。你看過他們中的任何一個嗎? – Barmar

+4

有什麼問題? – esel

+1

你的'#include'在哪裏?你有什麼問題,輸入?輸出? – zero298

回答

1

你的程序不能編譯,因爲你錯過了添加這些包括:

#include <string> 
#include <iostream> 
#include <time.h> 

編輯: 你也應該避免使用全namespace std。這太寬泛了。使用特定using!而非:

using std::string; 
using std::cout; 
using std::cin; 
using std::endl;