C++ 中的错误"invalid conversion from 'const char*' to 'char' [-fpermissive]"



我的代码有问题。我正在尝试制作一个将非整数转换为二进制的程序。它在第21行返回一个错误,说";从"const char*"到"char"的转换无效[-fpermission]";。这意味着什么?我该如何解决?

#include <iostream>
#include <string>
using namespace std;
int main(){
string number;
cout<<"Insert number";
cin>>number;
int continue = 0, integer;
bool first = false;
while(continue >= 0){
if(primo == false){
integer = number[continue];
first = true;
continue++;
}
else{
char dot = "."; //line 21
char comma = ",";
char check = number[continue];
if((check == comma) or (check == dot)){
continue = -1;
}
else{
int encapsulation = number[continue]
integer = (integer*10)+encapsulation;
continue++;
}

}

}
cout<<integer;
return 0;
}

几个错误:

  1. continue是一个关键字。我将其重命名为cont

  2. 双引号"."用于字符串。Chars常量用单引号括起来,作为'.'

#include <iostream>
#include <string>
using namespace std;
int main(){
string number;
cout<<"Insert number";
cin>>number;
int cont = 0; 
int integer;
bool first = false;
bool primo = false;
while(cont >= 0){
if(primo == false){
integer = number[cont];
first = true;
cont++;
}
else{
char dot = '.'; //line 21
char comma = '.';
char check = number[cont];
if((check == comma) or (check == dot)){
cont = -1;
}
else{
int encapsulation = number[cont];
integer = (integer*10)+encapsulation;
cont++;
}

}

}
cout<<integer;
return 0;
}

Godbolt:https://godbolt.org/z/Tvz3MKxx7

最新更新