#include <iostream>
using namespace std;
int main()
{
string message;
cout << "Ahlan ya user ya habibi." <<endl;
cout <<"what do you like to do today?" <<endl;
cout <<"please enter the message:" <<endl;
getline(cin,message);
for(int i=0;i<message.size();i++)
{
if(string(message[i]==32))
{
cout<<char(message[i]);
}
else if(string( message[i])>=110)
{
int x = int(message[i])-13;
cout<<char(x);
}
else
{
int x = string (message[i])+13;
cout<<char(x);
}
}
return 0;
}
e:我的programe quiz main.cpp | 20 |错误:呼叫'std :: __ cxx11 :: basic_string&lt; char> :: basic_string(char&amp;('|
e: my programe quiz main.cpp | 20 |错误:从'char'到'const char*'[-fpermissive] |
的转换无效e:我的programe quiz main.cpp | 27 |错误:呼叫'std :: __ __ cxx11 :: basic_string&lt; char> :: basic_string(char&amp;('|
e:我的programe quiz main.cpp | 27 |错误:从'char'到'const char*'[-fpermissive] |
的转换无效
std::string::operator[]
返回char&
参考。您正在尝试以单个char
值作为输入来构造临时std::string
对象,但是std::string
没有任何仅将单个char
作为输入的构造函数。这就是为什么您会遇到错误的原因。
即使您可以从单个char
构造std::string
,也无法将std::string
与整数进行比较。
您根本不需要所有这些string()
(和char()
(铸件(顺便说一句,您的第一个string()
铸件无论如何都会畸形(。char
是数字类型。您可以将char
值直接与整数进行比较,并将/减去和整数直接添加到char
值以产生新的char
值。
而是尝试一下:
#include <iostream>
using namespace std;
int main()
{
string message;
cout << "Ahlan ya user ya habibi." << endl;
cout << "what do you like to do today?" << endl;
cout << "please enter the message:" << endl;
getline(cin, message);
for(int i = 0; i < message.size(); i++)
{
if (message[i] == 32)
{
cout << message[i];
}
else if (message[i] >= 110)
{
char x = message[i] - 13;
cout << x;
}
else
{
char x = message[i] + 13;
cout << x;
}
}
return 0;
}
实时演示