Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to link to a static lib in Visual Studio C++ 2010 Express/ Console applications 2 92
PDF library for Delphi 2 107
C++ question 3 62
Safe conversion? 4 58
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

896 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

14 Experts available now in Live!

Get 1:1 Help Now