C++ 有人能帮我解决操作员不匹配的错误吗 #包括 #包括 #包括 使用名称空间std; int main() { 字符串str=“”,str1=“”,str2=“”; 字符串math=“10”; cout
对于字符串,没有定义运算符“*”。因为您想用int等数字进行乘法。在这里,您可以找到一种方法来实现:C++ 有人能帮我解决操作员不匹配的错误吗 #包括 #包括 #包括 使用名称空间std; int main() { 字符串str=“”,str1=“”,str2=“”; 字符串math=“10”; cout,c++,C++,对于字符串,没有定义运算符“*”。因为您想用int等数字进行乘法。在这里,您可以找到一种方法来实现: #include <iostream> #include<string> #include<sstream> using namespace std; int main() { string str="", str1 = "", str2 = "" ; string math="10"; cout << "Enter a
#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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串str1=“”,str2=“”;
整数数学=10;
难道你期望做什么?std::string
s是不可乘法的。stringstream是一种将数字转换为字符串的简单方法。-
istringstream也是一种用空格解析数据的简单方法,但你没有在本可以的情况下使用它。所以我将字母转换为数字t当我想将它们相乘时,你没有任何数字。你有看起来像数字的字符串,但它们不是数字。要获得数字,你需要将字符串转换成数字,然后将这些值存储在具有整数或浮点数据类型(int
,double
等)的变量中即使你有数字,math*str2;
也能完成什么?乘法的结果存储在哪里?
#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;
}