首页 > 解决方案 > 有人可以帮我解决与操作员不匹配的错误吗

问题描述

#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;
}

标签: c++

解决方案


对于 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;
}

推荐阅读