Solved

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

Posted on 2011-02-14
4
312 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 32

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now