error:binary'>> :没有操作符,找到类型为'const char [1]的右手操作数。第一次输入后程序崩溃 [英] error : binary '>>' : no operator found which takes a right-hand operand of type 'const char [1] And program crashes after taking first input

查看:130
本文介绍了error:binary'>> :没有操作符,找到类型为'const char [1]的右手操作数。第一次输入后程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的程序,它需要2个数字,反转它们并打印它们的反转和。我有2个问题

This is a simple program which takes 2 numbers, reverses them and prints their reversed sum. I have 2 problems


  1. 如果我继续使用cin >>,它会产生错误binary >>:

  2. 如果我使用scanf_s()而不是cin >>它在第一个数字后崩溃。

  1. If I keep using "cin >> " it gives error "binary >> :no operator found which take a right hand operand of type 'const char[1](or there is no acceptable version )" .
  2. If I use "scanf_s()" instead of "cin >> " then it crashes after taking the first number.

int calculate_sum(string num){
stack<char> mystack;
int sum = 0;
for (int i = 0; i < num.length(); i++){
    mystack.push(i);
}

while (!mystack.empty()){
    char c; int n;
    c = mystack.top();
    mystack.pop();
    n= (int)c;
    sum = sum + n;
}

int main(){
    cout << "Enter testcases:" << endl;
    int testcase=0;
    cin >> testcase;
    while (testcase--){
        string num1, num2;
        int rev_sum1, rev_sum2, final_sum;
        int sum = 0;
        cin >> num1 >> "" >> num2 ;
        //scanf_s("%s %s", num1, num2);
        rev_sum1 = calculate_sum(num1);
        rev_sum2 = calculate_sum(num2);
        final_sum = rev_sum1 + rev_sum2;
        cout << final_sum << endl;
    }
    return sum;
}  



推荐答案

问题出在这一行:

cin >> num1 >> "" >> num2 ;

您不能将输入中获得的值存储到。我相信你想要的东西像:

You cannot store the value got from the input into "". I believe you wanted something like:

cin >> num1 >> num2 ;

这篇关于error:binary'&gt;&gt; :没有操作符,找到类型为'const char [1]的右手操作数。第一次输入后程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆