c++ - 有人可以帮我解决与操作员不匹配的错误吗
问题描述
#include <iostream>
#include<string>
#include<sstream>
using namespace std;
int main()
{
string str="", str1 = "", str2 = "" ;
string math="10";
cout << "Enter a string:\n";
//Get input string. Use newline as terminator and store the string in str1
getline(cin,str1,'\n');
//limit str1 to 100 characters
str1 = str1.substr(0,100);
//Identify if somethings wrong on the input
if(str1=="")
{
cout << "You entered nothing you silly nod!: " << endl;
return 0;
}
for(int i=0;i <str1.length();i++)
{
stringstream ss;
string temp = "";
if(isalpha(str1[i]) || str[i] == ' ')
{
//break up the string with spaces in the same places. This trim the '-' off the end
before adding the space.
if(str1[i] == ' ')
{
str2 = str2.substr(0, str2.length() -1) + str[i];
}
else
{
//convert the char to int and substract the offset
//stringstream is an easy way to convert numbers to strings.
ss << (int)(tolower(str1[i])-86);
ss >> temp;
str2 += temp + "-";
}
}
}
//Trim the '-' off the final string
math * str2;
str2= str2.substr(0, str2.length() -1);
cout << "Your generated number: " << endl;
cout << str2 << endl <<endl;
system("pause");
return 0;
}
解决方案
对于 String 没有定义运算符“ * ”。由于您想将数字相乘,请使用例如 int。在这里,您可以找到一种方法:
#include <iostream>
#include<string>
#include<sstream>
#include<vector>
using namespace std;
int main()
{
string str1 = "", str2 = "";
int math = 10;
cout << "Enter a string:\n";
//Get input string. Use newline as terminator and store the string in str1
getline(cin, str1, '\n');
//limit str1 to 100 characters
str1 = str1.substr(0, 100);
//Identify if somethings wrong on the input
if (str1 == "")
{
cout << "You entered nothing you silly nod!: " << endl;
return 0;
}
vector<int> intergers;
for (int i = 0; i < str1.length(); i++)
{
if (isalpha(str1[i]) || str1[i] == ' ')
{
//break up the string with spaces in the same places. This trim the '-' off the end before adding the space.
if (str1[i] == ' ')
{
str2 = str2.substr(0, str2.length() - 1) + str1[i];
}
else
{
//convert the char to int and substract the offset
intergers.push_back(tolower(str1[i]) - 86);
str2 += to_string(tolower(str1[i]) - 86) + "-";
}
}
}
for (auto val : intergers)
{
cout << math << " * " << val << " = " << math * val << endl;
}
//Trim the '-' off the final string
str2 = str2.substr(0, str2.length() - 1);
cout << "Your generated number: " << endl;
cout << str2 << endl << endl;
system("pause");
return 0;
}
推荐阅读
- wordpress - Wordpress 根文件夹内的错误 404 MyBB 文件夹
- docker - Docker-Compose:更新后无法访问服务
- python - 我在哪里可以找到在自然图像上训练的预训练变分自动编码器?
- python - 使用Pyhton for循环,我怎样才能每次都从字符串中取出一个字母而不重复
- azure-active-directory - 使用 azuread-openidconnect 的 express js 的 Passport 中间件
- python - python - 如何使用复杂的构造函数从python中的jsons创建对象?
- git - .gitignore 模式递归地忽略所有文件,但不是文件夹,也不是其他 .gitignore 文件
- angular7 - 如何使用angular7显示谷歌弹出确认对话框?
- python - 如何创建无密码的 FTP 连接?
- ios - Swift 5 CAKeyframeAnimation - 应用程序移动到免费后恢复图层动画