0

I'm trying to build a function that goes through a while or for-loop and finds where the space is, outputs everything before the space, and then erases everything before the space including the space, and then repeats this again.

Any help is much appreciated.

int sentence()
{
    string enteredSentence="";

    getline(cin,enteredSentence);
    string sentenceString(enteredSentence);

    int sentenceLength=enteredSentence.size();
    cout<<"size of sentence"<<sentenceLength<<endl;
    int stringSize=sentenceString.size();
    while(stringSize>0)
    {
        int spaceLoc = enteredSentence.find(" ");
        cout<<spaceLoc;

        cout<<sentenceString.substr(0,spaceLoc)<<endl;
        sentenceString.substr(0,spaceLoc);
        cout<<"string before string eraced"<<sentenceString<<endl;

        sentenceString.erase (0,spaceLoc);
        cout<<"string after string eraced"<<sentenceString<<endl;

        stringSize=sentenceString.size();
        cout<<"string size is"<<stringSize<<endl;
    }
Pang
  • 9,564
  • 146
  • 81
  • 122
ike
  • 45
  • 6
  • What is your problem specifically? – Ediac Jul 04 '15 at 04:37
  • the problem is the when "enteredSentence.find(" ");" is ran it is returning the same value when it looks for a space. can the ,find() only find one time. How do I make give out a accurate number each time. The first number is accurate however the numbers after that are the same – ike Jul 04 '15 at 04:46
  • here is the output notice its a 3 everytime – ike Jul 04 '15 at 04:50
  • Pizza is my favorite food size of sentence25 5Pizza string before string eracedPizza is my favorite food string after string eraced is my favorite food string size is20 5 is m string before string eraced is my favorite food string after string eracedy favorite food string size is15 5y fav string before string eracedy favorite food string after string eracedorite food string size is10 5orite etc... – ike Jul 04 '15 at 04:50
  • @ike check my answer :) – fsacer Jul 04 '15 at 04:50
  • @ike pls edit your post instead, also like others said check if the answers below fulfill your requirements – Ediac Jul 04 '15 at 04:51

3 Answers3

1

I'm not 100% sure that I understand what you want to achieve. But I can help you with find:

It has a second parameter that specifies from where on in the string the search will start:

size_t pos = 0;
while ((pos = str.find(' ', pos)) != std::string::npos) {
  std::cout << "Found a space at " << pos << std::endl;
  ++pos;
}

Reference

With more information on what you actually want your code to do (show example input plus wanted output) I can help you clear the rest of your code.
Currently your description suggests that you want to output the entire string, but in pieces (separated by spaces).

Your code makes a (needless?) copy of your input, generates substrings only to throw them away and doesn't return an int as said in the function declaration.

If you want to tokenize your input then this question has some answers for you.

Community
  • 1
  • 1
Daniel Jour
  • 15,896
  • 2
  • 36
  • 63
  • You need to increment `pos` on subsequent iterations, or you will just keep finding the same space character each time, because you are starting at the same position after the first result is found. The specified position is included in the search. – Remy Lebeau Jul 04 '15 at 04:47
1

This is how I fixed your code:

#include <iostream>

using namespace std;

int main()
{
    string enteredSentence="";

    getline(cin,enteredSentence);
    string sentenceString(enteredSentence);

    int sentenceLength = enteredSentence.size();
    cout<<"size of sentence:"<<sentenceLength<<endl;
    string::size_type stringSize = sentenceString.size();
    while(stringSize > 0)
    {
        int spaceLoc = sentenceString.find(" "); //there was incorrect var
        cout<<spaceLoc<<endl;

        if(spaceLoc == string::npos){
            cout<<"last part:"<<sentenceString<<endl;
            break;
        }//check if there are no spaces left

        cout<<sentenceString.substr(0,spaceLoc)<<endl;
        //the substr line here was redundant
        cout<<"string before string erased:"<<sentenceString<<endl;

        sentenceString.erase(0, spaceLoc + 1);//also delete the space
        cout<<"string after string erased:"<<sentenceString<<endl;

        stringSize=sentenceString.size();
        cout<<"string size:"<<stringSize<<endl<<endl;

    }
    return 0;
}
fsacer
  • 1,382
  • 1
  • 15
  • 23
1

You could use a stringstream.

#include <sstream>
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
    string enteredSentence; // It's initialized to "" by default, by the way
    getline(cin,enteredSentence);
    cout<<"size of sentence: "<<enteredSentence.length()<<endl;
    istringstream str_in(enteredSentence);
    string word;
    while(str_in >> word) {
        // Do stuff with word
        // I believe str_in.str() will also give you the portion that hasn't yet been processed.
    }
    return 0;
}
celticminstrel
  • 1,637
  • 13
  • 21