-1

I am writing a program that allows the user to enter a sentence which then gets stored in a string and then the program will remove any full stops in the sentence and then create a list of each individual word in the string before outputting that list to the console.

the program is working perfectly as long as there is only 1 full stop in the sentence but if there are any more than that it throws this exception:

Unhandled exception at at 0x7696B727 in Project6.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0022F8B8.

and then if I continue to run it it throws:

Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

any suggestions? (and before anyone asks, i know you usually only have 1 full stop in a sentence but i need to do it with more than 1 as part of testing.

here is the code:

 #include <iostream>
 #include <string>

 using namespace std

 string sSentence; // sets up a string variable called sSentence

 int i = 0;

 void RemoveFullStop()
 {
    while(sSentence.find (".") != string::npos) // the program runs this loop until it cannot            find any more full stops in the string
{
        i = sSentence.find("." , i); // find the next full stop in the string and get the      character count of the space and place it in the variable i
        sSentence.replace(i,1,""); // remove the full stop
    }
}

void ListWords()
{
    while(sSentence.find (" ") != string::npos) // the program runs this loop until it cannot find any more spaces in the string
    {
        i = sSentence.find(" " , i); // find the next space in the string and get the character count of the space and place it in the variable i

        // cout << i << endl; // output the contents of iWordSpace to the console (used for debugging - no longer used)

        sSentence.replace(i,1,"\n");

        // cout << sSentence << endl; // output the contents of iWordSpace to the console (used for debugging - no longer used)

        }
    }

   int main()
   {
        getline(cin, sSentence); // get user input and store it in sSentence (using the getline     function so the .find operation works correctly)

        RemoveFullStop(); // calls the RemoveFullStop void function that removes all full stops from the string

        ListWords(); // calls the ListWords void function that splits the string into a list of words

        cout << endl; // formatting line
        cout << "The words that were in the sentence were:" << endl;
        cout << endl; // formatting line
        cout << sSentence << endl;
        cout << endl; // formatting line

    system("pause");

    return 0;
}
  • 6
    well, have you stepped through the code in the debugger? – OldProgrammer Jan 13 '15 at 19:21
  • 3
    @OldProgrammer We should have a number of one-click standard comments here :-P ... – πάντα ῥεῖ Jan 13 '15 at 19:23
  • 1
    You could print out the values of `sSentence` and `i` as your while() loop proceeds. This should give you some insight as to what is happening. – jwd Jan 13 '15 at 19:23
  • http://stackoverflow.com/questions/5891610/how-to-remove-characters-from-a-string – indiv Jan 13 '15 at 19:23
  • I gave you an upvote for clearly stating your problem. Probably people are downvoting you because it doesn't look like you have made many attempts to investigate the problem yourself. If you have, please add some notes about what you have tried and what the results were. – jwd Jan 13 '15 at 19:27
  • Hint: What value does `i` have when `ListWords` is entered? – Wintermute Jan 13 '15 at 19:44
  • I had output the value of sSentence and i with system pauses after every line of code through the while loop and i didnt see anything that i did not expect. I didnt however realise that the value of i was not resetting after the first void function was called so when the program entered the next void function, the value of i went out of range because it wasnt being reset. after looking at your solutions this morning then having a look through it myself I understand the problem completly so thank you – Kieran Milbourne Jan 14 '15 at 14:38

2 Answers2

0

The problem is that you keep re-using i, in both RemoveFullStop and ListWords.

i only ever increases, and so eventually it can get past the end of the string.

You really shouldn't need a global i variable at all, to do this task.

jwd
  • 10,837
  • 3
  • 43
  • 67
0

The reason this happens is that when sSentence.find(" " , i) in ListWords runs, i's value is not 0 because it was already defined in RemoveFullStop(). To fix this, first remove int i = 0;, then add it to RemoveFullStop() and ListWords()
Also, while this is just a style thing and won't effect your codes ability to run, I wouldn't call this variable i as i,j,k usually imply counters. Call this variable something more appropriately descriptive.

Here is the relevant code as it should be.

 using namespace std
 string sSentence; 

void RemoveFullStop()
{
    int charPlace = 0;
    while(sSentence.find (".") != string::npos) 
    {
        charPlace = sSentence.find("." , charPlace); 
        sSentence.replace(charPlace,1,""); 
    }
}

void ListWords()
{
    int charPlace = 0;
    while(sSentence.find (" ") != string::npos) 
    {
        charPlace = sSentence.find(" " , charPlace);
        sSentence.replace(charPlace,1,"\n");
    }
}
JakeP
  • 327
  • 3
  • 14