Solved

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

Posted on 2011-02-14
4
322 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 34

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

729 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