error: string subscript out of range!!

Can someone please help me with this coding? After I press debug it executes the program but then an error message pops stating "string subscript out of range." I've tried everything i could think of but still got no solution.

#include <iostream>
#include <string>
using namespace std;
string substr (string str);
bool isVowel (char c);

int main ()
{
string str, str1;
cout << "Please enter string: ";
cin >> str;
str1 = substr(str);
cout << str1;
system ("pause");
return 0;
}

string substr(string str)
{
int i=0, temp;
bool check;
while (i < str.length())
{
check =isVowel(str[i]);
if (check)
{
temp = i;
while (i < str.length())
{
str[i] = str[i + 1];
i++;
}
i=temp;
}
else
i++;
}
return str;
}

bool isVowel (char c)
{
if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u' || c=='A' || c=='E' || c=='I' || c=='O' || c=='U')
return true;
else
return false;
}
Look at:

1
2
3
4
5
while (i < str.length())
{
str[i] = str[i + 1];
i++;
}


if I have a string "Foobar", what is its length, as returned by string::length()? 6. So if i=5, i < length() right? So in that case, you are setting:
str[5] = str[6]. Is str[6] a valid subscript?
Last edited on
i tried various different ways according to your explanation but i still cant manage to get it correctly. Can you please just tell me exactly what to change?
You didn't answer my question though...
i dont know which number will be a valid subscript bcuz i dont know exactly what to change to check it. the program is suppose to take a word for, example "there" and remove the vowels so it becomes "thr".. i understand exactly what you want me to do but i just dont know where to make the changes
C++ strings begin at subscript '0', so in string strCat = "cat"; strCat[0] is 'c', strCat[1] is 'a', etc. The length of the string is 3, but the last valid subscript is strCat[2]. What does strCat[3] give you? This is somewhat undefined. So what happens when you do str[5] = str[6] on a string with length 6? Try to amend your logic so you stay within the bounds of the subscript.
i dont understand what you mean by str[5] = str[6]. Your trying to say what happens when you change from 5 to 6? I tried str[5] = str[i+1] and it still gave me the same error even when i changed the 5 to 6 and i used a string with 5, 6, or 7 letters in a word. I understand what you mean by a valid subscript because in the word "there" str[4] would be the last valid subscript but again i dont know exactly where to make the changes to see what happens when i do str[5] = str[6] on a string with length 6.
You should also consider taking advantage of the toupper or tolower functions.

1
2
3
bool isVowel (char c)
{
if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u' || c=='A' || c=='E' || c=='I' || c=='O' || c=='U')



can be rewitten as

1
2
3
4
bool isVowel (char c)
{
c = toupper(c);
if (c=='A' || c=='E' || c=='I' || c=='O' || c=='U')




If you are still having trouble with the index try printing out each value of i along against the value of str.length().

Also remember that the last letter in a string of size 6 if actually indexed as 5.
Last edited on
Topic archived. No new replies allowed.