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

从char*到int的转换无效。C++错误;,c++,do-while,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 "-"

我在这里感到困惑。为什么我会犯这个错误

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');
}

我的意思是我显然没有把字符转换成int。我甚至都没学会怎么做。好像我在这里很困惑。它认为“+”是一个int还是什么?

在您的例子中,您使用的是双引号,对应于const char*。您希望使用单引号,即字符

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

不过,您的程序还有许多其他问题

您所有的printf都打印相同的内容,但75%的时间不正确

您需要使用双等于来检查while中的相等性


您在std::cout的末尾使用了一个extraction操作符>>,在您的例子中,应该是,您使用的是双引号,它对应于const char*。您希望使用单引号,即字符

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

不过,您的程序还有许多其他问题

您所有的printf都打印相同的内容,但75%的时间不正确

您需要使用双等于来检查while中的相等性

您在std::cout的末尾使用了提取运算符>>,应该可能是键入错误!? 第22行:编译器在这个位置需要一个整数,但这是一个字符串,一个指针。 第13行:按如下方式键入:“-”

可能是键入错误!? 第22行:编译器在这个位置需要一个整数,但这是一个字符串,一个指针。
第13行:按如下方式键入:“-”

在“-”和-”之间存在子空间差异:

前者是char类型的单个整数,值为45 ASCII值- 后者是一个字符串。字符串的内容是一个2元素数组{45,0},其中45是-的ASCII码,0表示字符串的结尾。字符串的值作为一个整体是指向存储它的内存的指针。 实际上,代码试图做的是将option的值与指向内存的指针进行比较。解决方法是更改引号

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

while(continueCheck = 'y')

它将值“y”指定给continueCheck,如果指定的值为非零(始终为真),则继续循环。您很可能希望将continueCheck与y进行比较,y是通过运算符==,而不是=。

在“-”和-”之间存在子空间差异:

前者是char类型的单个整数,值为45 ASCII值- 后者是一个字符串。字符串的内容是一个2元素数组{45,0},其中45是-的ASCII码,0表示字符串的结尾。字符串的值作为一个整体是指向存储它的内存的指针。 实际上,代码试图做的是将option的值与指向内存的指针进行比较。解决方法是更改引号

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

while(continueCheck = 'y')

它将值“y”指定给continueCheck,如果指定的值为非零(始终为真),则继续循环。您很可能希望将continueCheck与y进行比较,y是通过运算符==,而不是=。

case-→ 案例“-”等。非常感谢@Biffen!案例-→ 案例“-”等。非常感谢@Biffen!whilecontinueCheck='y';这也是错误的。由于OP写了“+”但是-等等,我认为OP只需要更加关注他/她的代码。我的意思是,我显然没有在任何地方将char转换为int。是的,OP试图,但没有意识到。因为这正是凯斯所期望的。@LightnessRacesinOrbit刚刚发布了一个仓促的回答。我们将列举程序中的其他问题,但这是最紧迫的问题不是批评,只是聊天。虽然FGITW=不好,但现在您提到了:P@LightnessRacesinOrbit为了完整起见,我添加了我能找到的所有逻辑问题。没有提到风格或最佳实践:whilecontinueCheck='y';这也是错误的。由于OP写了“+”但是-等等,我认为OP只需要更加关注他/她的代码。我的意思是,我显然没有在任何地方将char转换为int。是的,OP试图,但没有意识到。因为这正是凯斯所期望的。@LightnessRacesinOrbit刚刚发布了一个仓促的回答。我们将列举程序中的其他问题,但这是最紧迫的问题不是批评,只是聊天。虽然FGITW=不好,但现在您提到了:P@LightnessRacesinOrbit为了完整起见,我添加了我能找到的所有逻辑问题。没有提到风格或最佳实践:这应该是一个评论。问题是:我为什么会犯这个错误?答案应该是肯定的,而不是可能的。这应该是一个评论。问题是:我为什么会犯这个错误?答案应该是肯定的,而不是可能的。