小编His*_*ery的帖子

在C++中将Char转换为Int的问题

本网站上的第一个问题,希望我以可接受的方式发布代码.我正在努力使这个摇滚,纸张,剪刀游戏工作,但我很难通过"你已进入线",因为它在那之后终止.

我的教授说我的问题是当randNumGenerated是int类型时,userChoice是char类型.我尝试使用以下代码转换我的r,p和s值:char 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 …
Run Code Online (Sandbox Code Playgroud)

c++ int char

0
推荐指数
1
解决办法
88
查看次数

标签 统计

c++ ×1

char ×1

int ×1