c ++中的岩纸剪刀程序

时间:2014-01-18 06:38:58

标签: c++

我是初学者,我无法在这个摇滚剪刀程序中找到我做错的事。请有人帮忙吗。下面是我的代码:

我创建了两个函数one userchoice(),它接受来自用户的输入并返回相应的字符串。第二个是compchoice(),从1到3生成随机数并返回相应的字符串,然后我调用fucntions并将值保存在我在while循环中比较的变量中,以查看用户输入是否与计算机匹配。 / p>

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 个答案:

答案 0 :(得分:1)

您的程序无法编译,因为您错过了添加这些内容包括:

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

修改的: 您还应该避免使用整个namespace std。这太宽泛了。请改用特定的using

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