Solved

c++ getting an runtine error: subscript out of range

Posted on 2011-02-14
4
316 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:pgmerLA
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 34891970
Seems to be an issue about CR/LC and streaming in a single char, you end up with an empty 'sentence' the loop is run the 2nd time, thus the error - try the following, it works:
#include <iostream>
#include<string>
#include <cctype>

using namespace std;



int main(){
	//declare variable
	string sentence;
	int m;
	string c="y";
	
	while( c == "y" || c == "Y")
	{cout<< "Enter the name of a state: ";
	getline(cin, sentence);
	cout<< sentence <<" or ";
	m = sentence.find(' ');
	if (m == -1) //no space present
		{sentence[0] = toupper(sentence[0]);
		sentence[1] = toupper(sentence[1]);
		cout<< sentence[0]<<sentence[1]<<endl;}
	else 
		{sentence[0] =toupper(sentence[0]);
		sentence[1] = toupper(sentence[m+1]);
		cout<<sentence[0]<<sentence[1]<<endl;}

	cout<< "Continue(y/n)?";
	getline(cin,c);
	}
	//terminate the program
	return 0;
}

Open in new window

0
 
LVL 86

Expert Comment

by:jkr
ID: 34891974
Sorry, that should have read 'CR/LF'.
0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 34894877
You could try this as well
cout<< "Continue(y/n)?";
	cin>> c;
	cin.clear(); cin.ignore(INT_MAX,'\n'); //----------------> Try this

Open in new window

0
 
LVL 33

Expert Comment

by:sarabande
ID: 34894904
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]<<sentence[1]<<endl;}

the code is not good by such statements (i don't understand what it should do???) but safe.  

Sara
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question