?
Solved

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

Posted on 2011-02-14
4
Medium Priority
?
325 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 2000 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 35

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

Introducing Priority Question

Increase expert visibility of your issues by participating in Priority Question, our latest feature for Premium and Team Account holders. Adjust the priority of your question to get emergent issues in front of subject-matter experts for help when you need it most.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

770 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