从char *到int的转换无效。 C ++错误;

时间:2016-04-20 11:45:55

标签: c++ do-while

我在这里感到很困惑。为什么我会收到此错误?

Running /home/ubuntu/workspace/main.cpp
/home/ubuntu/workspace/main.cpp: In function ‘int main()’:
/home/ubuntu/workspace/main.cpp:22:19: error: invalid conversion from ‘const char*’ to ‘int’ [-fpermissive]
              case "-" : 
                   ^
/home/ubuntu/workspace/main.cpp:22:19: error: ‘(int)((long int)"-")’ is not a constant expression
/home/ubuntu/workspace/main.cpp:31:19: error: invalid conversion from ‘const char*’ to ‘int’ [-fpermissive]
              case "*" : 
                   ^
/home/ubuntu/workspace/main.cpp:31:19: error: ‘(int)((long int)"*")’ is not a constant expression
/home/ubuntu/workspace/main.cpp:40:19: error: invalid conversion from ‘const char*’ to ‘int’ [-fpermissive]
              case "/" : 
                   ^
/home/ubuntu/workspace/main.cpp:40:19: error: ‘(int)((long int)"/")’ is not a constant expression
/home/ubuntu/workspace/main.cpp:50:14: error: no match for ‘operator>>’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘const char [41]’)
         cout >> "Want to continue? Enter Y or y OR N or n" << endl;
              ^
/home/ubuntu/workspace/main.cpp:50:14: note: candidates are:
In file included from /usr/include/c++/4.8/string:53:0,
                 from /usr/include/c++/4.8/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8/bits/ios_base.h:41,
                 from /usr/include/c++/4.8/ios:42,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from /home/ubuntu/workspace/main.cpp:1:
/usr/include/c++/4.8/bits/basic_string.tcc:996:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&)
     operator>>(basic_istream<_CharT, _Traits>& __in,
     ^
/usr/include/c++/4.8/bits/basic_string.tcc:996:5: note:   template argument deduction/substitution failed:
/home/ubuntu/workspace/main.cpp:50:17: note:   ‘std::ostream {aka std::basic_ostream<char>}’ is not derived from ‘std::basic_istream<_CharT, _Traits>’
         cout >> "Want to continue? Enter Y or y OR N or n" << endl;
                 ^
In file included from /usr/include/c++/4.8/istream:882:0,
                 from /usr/include/c++/4.8/iostream:40,
                 from /home/ubuntu/workspace/main.cpp:1:
/usr/include/c++/4.8/bits/istream.tcc:955:5: note: template<class _CharT2, class _Traits2> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT2*)
     operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s)
     ^
/usr/include/c++/4.8/bits/istream.tcc:955:5: note:   template argument deduction/substitution failed:
/home/ubuntu/workspace/main.cpp:50:17: note:   ‘std::ostream {aka std::basic_ostream<char>}’ is not derived from ‘std::basic_istream<_CharT, _Traits>’
         cout >> "Want to continue? Enter Y or y OR N or n" << endl;
                 ^
In file included from /usr/include/c++/4.8/istream:882:0,
                 from /usr/include/c++/4.8/iostream:40,
                 from /home/ubuntu/workspace/main.cpp:1:
/usr/include/c++/4.8/bits/istream.tcc:923:5: note: template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&)
     operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c)
     ^
/usr/include/c++/4.8/bits/istream.tcc:923:5: note:   template argument deduction/substitution failed:
/home/ubuntu/workspace/main.cpp:50:17: note:   ‘std::ostream {aka std::basic_ostream<char>}’ is not derived from ‘std::basic_istream<_CharT, _Traits>’
         cout >> "Want to continue? Enter Y or y OR N or n" << endl;
                 ^
In file included from /usr/include/c++/4.8/iostream:40:0,
                 from /home/ubuntu/workspace/main.cpp:1:
/usr/include/c++/4.8/istream:727:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char&)
     operator>>(basic_istream<char, _Traits>& __in, unsigned char& __c)
     ^
/usr/include/c++/4.8/istream:727:5: note:   template argument deduction/substitution failed:
/home/ubuntu/workspace/main.cpp:50:17: note:   ‘std::ostream {aka std::basic_ostream<char>}’ is not derived from ‘std::basic_istream<char, _Traits>’
         cout >> "Want to continue? Enter Y or y OR N or n" << endl;
                 ^
In file included from /usr/include/c++/4.8/iostream:40:0,
                 from /home/ubuntu/workspace/main.cpp:1:
/usr/include/c++/4.8/istream:732:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char&)
     operator>>(basic_istream<char, _Traits>& __in, signed char& __c)
     ^
/usr/include/c++/4.8/istream:732:5: note:   template argument deduction/substitution failed:
/home/ubuntu/workspace/main.cpp:50:17: note:   ‘std::ostream {aka std::basic_ostream<char>}’ is not derived from ‘std::basic_istream<char, _Traits>’
         cout >> "Want to continue? Enter Y or y OR N or n" << endl;
                 ^
In file included from /usr/include/c++/4.8/iostream:40:0,
                 from /home/ubuntu/workspace/main.cpp:1:
/usr/include/c++/4.8/istream:774:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char*)
     operator>>(basic_istream<char, _Traits>& __in, unsigned char* __s)
     ^
/usr/include/c++/4.8/istream:774:5: note:   template argument deduction/substitution failed:
/home/ubuntu/workspace/main.cpp:50:17: note:   ‘std::ostream {aka std::basic_ostream<char>}’ is not derived from ‘std::basic_istream<char, _Traits>’
         cout >> "Want to continue? Enter Y or y OR N or n" << endl;
                 ^
In file included from /usr/include/c++/4.8/iostream:40:0,
                 from /home/ubuntu/workspace/main.cpp:1:
/usr/include/c++/4.8/istream:779:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char*)
     operator>>(basic_istream<char, _Traits>& __in, signed char* __s)
     ^
/usr/include/c++/4.8/istream:779:5: note:   template argument deduction/substitution failed:
/home/ubuntu/workspace/main.cpp:50:17: note:   ‘std::ostream {aka std::basic_ostream<char>}’ is not derived from ‘std::basic_istream<char, _Traits>’
         cout >> "Want to continue? Enter Y or y OR N or n" << endl;
                 ^
In file included from /usr/include/c++/4.8/iostream:40:0,
                 from /home/ubuntu/workspace/main.cpp:1:
/usr/include/c++/4.8/istream:872:5: note: template<class _CharT, class _Traits, class _Tp> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&&, _Tp&)
     operator>>(basic_istream<_CharT, _Traits>&& __is, _Tp& __x)
     ^
/usr/include/c++/4.8/istream:872:5: note:   template argument deduction/substitution failed:
/home/ubuntu/workspace/main.cpp:50:17: note:   ‘std::ostream {aka std::basic_ostream<char>}’ is not derived from ‘std::basic_istream<_CharT, _Traits>’
         cout >> "Want to continue? Enter Y or y OR N or n" << endl;
                 ^

对于这个C ++代码:

#include <iostream>

using namespace std;

int main(){
    char option,continueCheck;
        int a,b,res;
    cout << "********** Welcome to the operation game ********* " <<endl;
    do{
        cout << "Please type (+,-,/,*) to add your numbers " <<endl;
        cin >> option;
        switch(option){
            case '+' : 
               cout << "Please type in the first number" <<endl;
               cin >> a;
               cout << "Please type in the second number" <<endl;
               cin >> b;
               res = a+b;
               printf("%d + %d = %d \n" , a,b,res);
               break;

             case "-" : 
               cout << "Please type in the first number" <<endl;
               cin >> a;
               cout << "Please type in the second number" <<endl;
               cin >> b;
               res = a-b;
               printf("%d + %d = %d \n" , a,b,res);
               break;

             case "*" : 
               cout << "Please type in the first number" <<endl;
               cin >> a;
               cout << "Please type in the second number" <<endl;
               cin >> b;
               res = a*b;
               printf("%d + %d = %d \n" , a,b,res);
               break;

             case "/" : 
               cout << "Please type in the first number" <<endl;
               cin >> a;
               cout << "Please type in the second number" <<endl;
               cin >> b;
               res = a/b;
               printf("%d + %d = %d \n" , a,b,res);
               break;   
        }

        cout >> "Want to continue? Enter Y or y OR N or n" << endl;
        cin >> continueCheck;

    }while(continueCheck = 'y');
}

我的意思是我显然没有在任何地方将char转换为int。我甚至都没有学会如何做到这一点。就像我在这里感到困惑一样。它认为'+'int还是其他什么?

3 个答案:

答案 0 :(得分:3)

在您的情况下,您使用的双引号对应const char*。您想使用单引号,即char s。

它抱怨从const char*转换为int的原因是char被提升为int类型。

但是你的程序还有很多其他问题。

  • 所有printf打印的内容都相同(75%的时间都不正确)。

  • 您需要使用双等号来检查while中的相等性。

  • 您在>>的末尾使用了提取运算符std::cout - 应为<<

这是一个(更多)正确的版本:

#include <iostream>

using namespace std;

int main(){
    char option,continueCheck;
        int a,b,res;
    cout << "********** Welcome to the operation game ********* " <<endl;
    do{
        cout << "Please type (+,-,/,*) to add your numbers " <<endl;
        cin >> option;
        switch(option){
            case '+' : 
               cout << "Please type in the first number" <<endl;
               cin >> a;
               cout << "Please type in the second number" <<endl;
               cin >> b;
               res = a+b;
               printf("%d + %d = %d \n" , a,b,res);
               break;

             case '-' : 
               cout << "Please type in the first number" <<endl;
               cin >> a;
               cout << "Please type in the second number" <<endl;
               cin >> b;
               res = a-b;
               printf("%d - %d = %d \n" , a,b,res);
               break;

             case '*' : 
               cout << "Please type in the first number" <<endl;
               cin >> a;
               cout << "Please type in the second number" <<endl;
               cin >> b;
               res = a*b;
               printf("%d * %d = %d \n" , a,b,res);
               break;

             case '/' : 
               cout << "Please type in the first number" <<endl;
               cin >> a;
               cout << "Please type in the second number" <<endl;
               cin >> b;
               res = a/b;
               printf("%d / %d = %d \n" , a,b,res);
               break;   
        }

        cout << "Want to continue? Enter Y or y OR N or n" << endl;
        cin >> continueCheck;

    }while(continueCheck == 'y');
}

在行动here中查看。

答案 1 :(得分:1)

'-'"-"之间存在实质性差异:

  • 前者是类型为char且值为45(ASCII值为 - )
  • 的单个整数
  • 后者是一个字符 string 。字符串的内容是一个2元素数组{45,0},其中45是-的ASCII代码,0表示字符串的结尾。字符串作为一个整体的值是指向存储它的内存的指针。

有效地,您的代码尝试执行的操作是将option的值与指向内存的指针进行比较。修复方法是更改​​引号。

您的代码中还有其他错误,最突出的是

while(continueCheck = 'y')

将值'y' 分配给continueCheck,如果指定的值为非零值(始终为true),则继续循环。您很可能希望 continueChecky进行比较,这是通过运算符==完成的,而不是=

答案 2 :(得分:0)

可能是打字错误!? 第22行:编译器在这个位置需要一个整数,但这是一个字符串,一个指针。 第13行:在此输入:' - '

相关问题