pgmerLA
asked on
c++ getting an runtine error: subscript out of range
I use MS VS2008
it's a 2 step program. It supposed to abbreviate states. If they are 2 letters, then first letter of first word, and second letter of second word. if one word, then the first 2 letters of the state.
We have to do at least 3 states.
it works for the state, then it gets me a runtime error.
help please.
proj3.3.txt
it's a 2 step program. It supposed to abbreviate states. If they are 2 letters, then first letter of first word, and second letter of second word. if one word, then the first 2 letters of the state.
We have to do at least 3 states.
it works for the state, then it gets me a runtime error.
help please.
proj3.3.txt
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry, that should have read 'CR/LF'.
You could try this as well
cout<< "Continue(y/n)?";
cin>> c;
cin.clear(); cin.ignore(INT_MAX,'\n'); //----------------> Try this
statements like
sentence[0] = toupper(sentence[0]);
or
sentence[1] = toupper(sentence[m+1]);
can lead to index access out of string boundaries if the string has not the required length. for example if it is empty or ends with a space char the statements would 'subscript out of range'
so always check the length before accessing a string element by index.
int l = sentence.length();
m = sentence.find(' ');
if (m == -1 && l > 1) //no space present and length >= 2
{sentence[0] = toupper(sentence[0]);
sentence[1] = toupper(sentence[1]);
cout<< sentence[0]<<sentence[1]<< endl;}
else if (l > m+1)
{sentence[0] =toupper(sentence[0]);
sentence[1] = toupper(sentence[m+1]);
cout<<sentence[0]<<sentenc e[1]<<endl ;}
the code is not good by such statements (i don't understand what it should do???) but safe.
Sara
sentence[0] = toupper(sentence[0]);
or
sentence[1] = toupper(sentence[m+1]);
can lead to index access out of string boundaries if the string has not the required length. for example if it is empty or ends with a space char the statements would 'subscript out of range'
so always check the length before accessing a string element by index.
int l = sentence.length();
m = sentence.find(' ');
if (m == -1 && l > 1) //no space present and length >= 2
{sentence[0] = toupper(sentence[0]);
sentence[1] = toupper(sentence[1]);
cout<< sentence[0]<<sentence[1]<<
else if (l > m+1)
{sentence[0] =toupper(sentence[0]);
sentence[1] = toupper(sentence[m+1]);
cout<<sentence[0]<<sentenc
the code is not good by such statements (i don't understand what it should do???) but safe.
Sara