Pregunta

I'm coding a little console application in C++ and in it I take a string from the user:

cin >> themainstring;
int si = 0;
while (themainstring[si] != '+' || themainstring[si] != '-' ||
        themainstring[si] != '*') {
    if (themainstring[si] == '+' || themainstring[si] == '-' ||
            themainstring[si] == '*') {
        lmnopt = themainstring[si];
        break; // while
    }
    si++;
}
int strlenthestring = themainstring.size();
lmnop1 = themainstring.substr(0, si);
lmnop2 = themainstring.substr(si + 1, strlenthestring);

So for example, when I give this input:

ilove+programming

I want to try and cut the string when I see +, - and *. which works fine.

However I want my code to do the same when I input:

ilove + programming (white spaces after and before arithmetical operator)

I have messed around with the WS but I couldn't understand the logic.

Actually the main problem of mine is about C++'s space logic. Why it thinks the space will explode the string input?

¿Fue útil?

Solución

I'm not sure I've understood this question completely correctly, however I thought I'd pitch in with some help.

First off, when it comes to looking through strings, C++ has a great set of functions as standard that does that. Point your browser to: Basic String Library.

This contains all the functions you can carry out on a string in C++.

Secondly, something else you need to be aware of, is that you are using std::cin to read user input from the keyboard. By default, cin ignores white spaces, so for example, the following code:

string inputString;
cin >> intputString;
cout << "Input String is: " << inputString << endl;

and let's assume you entered Hello World in as your user input, the program would only output "Hello"

So what you need to do is to use getline. Which allows for whitespaces in your user inputs. And you use it as follows:

std::getline(cin, inputString);

So to give an example where it all gels together:

#include <iostream>
#include <sstream> // for istringstream
#include <string>

using namespace std;

int main(int argc, char * argv[])
{
    string inputString;
    cout << "Please Enter String: ";
    getline(cin, inputString);

    cout << "\n" << endl;
    cout << "InputString is: " << inputString << endl;

    // So you can do something like this
    string searchTerm("+");

    // Find first of is an operating you can carry out
    // on a string so you don't have to use loops.
    cout << "Position: " <<  inputString.find_first_of(searchTerm) << endl;
    int pos = inputString.find_first_of(searchTerm);
    string part1 = inputString.substr(0, pos);
    string part2 = inputString.substr(pos + 1, inputString.length());
    cout << "Position of + is " << pos << endl;
    cout << "part 1 is: " << part1 << endl;
    cout << "part 2 is: " << part2 << endl;
}

Now I know I've only done this with the + sign, but it should serve as a starting point to getting to where you want to be.

Hope all this helps.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top