views:

54

answers:

4

This seems to be giving me a bit of trouble. This method is supposed to generate a random number and assigns it to a char. getline grabs the entire string from the text file and assigns it to foods. y has the purpose of holding the place of where it finds c in the foods string. It will then use that int to erase from the string and print out whats left.

I keep getting a "Program has requested to shutdown due to a runtime error in an unusual way" and it locks up. Thanks in advance.

   void feedRandomFood()
   {
       int y = 0;
       int x = rand() % food.size() + 1; //assigns x a random number between 1 and food.size MAX
       char c = '0' + x; //converts int to char for delimiter char.
       ifstream inFile;
       inFile.open("OatmealFood.txt", ios::in);
       string foods = "";
       getline(inFile, foods); 
       inFile.close();
       y = foods.find(c); 
       foods.erase(y); //erase characters up to the char found 
       cout << foods;
   }
A: 

Try:

Note that foods.erase(y) will erase the characters from 'f' forward. If you want to erase the characters up to 'f', then see this example:

Here's a simple example of how to erase characters:

  string x = "abcdefghijk";
  // find the first occurrence of 'f' in the string
  int loc = x.find('f');

  // erase all the characters up to and including the f
  while(loc >= 0) {
    x.erase(x.begin()+loc);
    --loc;
  }
  cout<<x<<endl;

Output of program:

---------- Capture Output ----------
> "c:\windows\system32\cmd.exe" /c c:\temp\temp.exe
ghijk

> Terminated with exit code 0.

So for your example, you need something like this:

while(y >= 0) {
    foods.erase(foods.begin() + y);
    --y;
}

EDIT You can also eliminate the while loop and just call the overloaded erase, like this:

  string x = "abcdefghijk";
  int loc = x.find('f');
  if (loc >= 0) {
    x.erase(x.begin(),x.begin()+loc+1);
    cout<<x<<endl;
  }

Output of program:

---------- Capture Output ----------
> "c:\windows\system32\cmd.exe" /c c:\temp\temp.exe
ghijk

> Terminated with exit code 0.
dcp
AH! Forgot about using while loops, i just learned about how to use string manipulations though, so I was doing .erase();
Nick Gibson
+2  A: 

What if the find method fails to find c in the string foods ? It returns npos and when you use that in erase your program blows.

So you need to add this check before you do an erase:

y = foods.find(c);
if( y != string::npos) {
    foods.erase(y); 
}

Also you should always ensure that the file open succeeded before you try and read a line from it.

inFile.open("OatmealFood.txt", ios::in);
if(!inFile.is_open()) {
  // open failed..take necessary steps.
}
codaddict
The value of `npos` can never be `-1`, for the simple reason that `npos` is unsigned. Instead, `npos´ has an extremely large value (probably 2^32 -1)
Bart van Ingen Schenau
@Bart: http://www.cplusplus.com/reference/string/string/npos/
codaddict
@codaddict: Note the type: `size_t` is unsigned.
Bart van Ingen Schenau
A: 

You need to place a check between y and the call to erase to make sure the substring was found:

y = foods.find(c);
if( y != string::npos ){
    foods.erase(y); //erase characters up to the char found 
    cout << foods;
}
wheaties
+1  A: 

I can't comment on the above solution from dcp (not enough posts yet), by why wouldn't you use the other erase methods available? Why do you need a while loop?

you can simply call:

foods.erase(0, loc);

(can you not?)

Nim