C++ 在C++;

C++ 在C++;,c++,char,integer,C++,Char,Integer,我试图使这个石头,布,剪刀游戏的工作,但我有困难通过“你已经进入线”,因为它终止后 我的教授说,我的问题是,当randNumGenerated的类型为int时,userChoice的类型为char。我尝试使用以下方法转换我的r、p和s值: 字符r='a'; int a=r 但编译器出现了“重新定义:不同的基本类型”错误。我不确定该怎么办,因为我的意图是将变量重新定义为int。我是不是完全错了?任何帮助都将不胜感激 int main() { char userChoice; in

我试图使这个石头,布,剪刀游戏的工作,但我有困难通过“你已经进入线”,因为它终止后

我的教授说,我的问题是,当randNumGenerated的类型为int时,userChoice的类型为char。我尝试使用以下方法转换我的r、p和s值: 字符r='a'; int a=r

但编译器出现了“重新定义:不同的基本类型”错误。我不确定该怎么办,因为我的意图是将变量重新定义为int。我是不是完全错了?任何帮助都将不胜感激

int main()
{

    char userChoice;
    int computer;
    int randNumGenerated = 0;
    int r = 0;
    int p = 0;
    int s = 0;
    unsigned seed;



    cout << "Chose either rock, paper, or scissors" << endl;
    cout << "Let r,p,and s represent rock,paper,and scissors respectively " << endl;   

    cin >> userChoice;

    cout << "You entered: " << userChoice << endl;
    cout << " " << endl;

    seed = time(0);
    srand(seed);
    randNumGenerated = rand() % 3;

    r == 0;
    p == 1;
    s == 2;

    


    if ((userChoice == 0 && randNumGenerated == 2) || (userChoice == 1 && randNumGenerated == 0) || (userChoice == 2 && randNumGenerated == 1))
    {
        cout << "You win!";
        
    }

    if ((userChoice == 0 && randNumGenerated == 1) || (userChoice == 1 && randNumGenerated == 2) || (userChoice == 2 && randNumGenerated == 0))
    {
        cout << "You lose!";

    }

    if ((userChoice == 0 && randNumGenerated == 0) || (userChoice == 1 && randNumGenerated == 1) || (userChoice == 2 && randNumGenerated == 2))
    {
        cout << "It's a draw!";

    }



    


    return 0;

}
intmain()
{
字符用户选择;
int计算机;
int randNumGenerated=0;
int r=0;
int p=0;
int s=0;
无标记种子;

cout这三行没有效果(字面意思,编译器应该警告您这一点):

您可能正在寻找以下内容:

int userNum = -1;
switch (userChoice) {
    case 'r':
        userNum = 0;
        break;
    case 'p':
        userNum = 1;
        break;
    case 's':
        userNum = 2;
        break;
}
但是,为什么还要使用整数呢?如果使用字符表示,if语句将更易于阅读:

char randomChosen = "rps"[randNumGenerated];
if ((userChoice == 'r' && randomChosen == 's') || ...) {
     std::cout >> "You win!\n";
}

只要看一下这一行,我就可以看出这是摇滚与剪刀的较量。在你基于数字的代码中,我必须回头看看转换表。

这三行没有效果(字面意思,编译器应该警告你这一点):

您可能正在寻找以下内容:

int userNum = -1;
switch (userChoice) {
    case 'r':
        userNum = 0;
        break;
    case 'p':
        userNum = 1;
        break;
    case 's':
        userNum = 2;
        break;
}
但是,为什么还要使用整数呢?如果使用字符表示,if语句将更易于阅读:

char randomChosen = "rps"[randNumGenerated];
if ((userChoice == 'r' && randomChosen == 's') || ...) {
     std::cout >> "You win!\n";
}
只要看一下这个,我就知道这是摇滚vs剪刀。在你基于数字的代码中,我必须回顾一下转换表