944,132 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1482
  • C++ RSS
Jan 17th, 2007
0

String maniputlation runtime error

Expand Post »
I am getting a runtime error somewhere in the inputData function, I can't find it. Can anyone help? Thanks

#include<iostream>
#include<string>
usingnamespace std;
void inputData (string &); //function prototype passing by reference
void outputData (string); //function prototype passing by value
int main()
{
string myString; //instantiates a string called myString
cout << "Enter a name format: (Last, First Middle) : ";
cin >> myString;
inputData( myString ); //calls inputData function passes myString by reference
outputData( myString ); //calls function outputData passes myString by value
//myString's data was changed by inputData function
return 0;
} // end main
// function inputData receives myString by reference then rearranges name using find(),
// substr(), length(), insert(), erase().
void inputData(string &aString)
{
int length = aString.length();
string lastName = aString.substr(0, aString.find(',') -1);

string firstName = aString.substr(aString.find_first_of(' ') +1 , aString.find_last_of(' ') -1 );

int firstLength = firstName.length();
string middleName = aString.substr(aString.find_last_of(' '), length );

int middleLength = middleName.length();
string middleInitial = middleName.erase( 1, middleLength - 2 );
lastName.insert(0, " ");
middleInitial.insert( 0, " ");
aString = firstName + middleInitial + lastName;
cout << aString;
}//end function inputData
//function outputData receives myString by value and outputs it to command line
void outputData(string bString)
{
cout << bString;
} //end function outputData

Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
dmmckelv is offline Offline
33 posts
since Nov 2006
Jan 17th, 2007
0

Re: String maniputlation runtime error

> usingnamespace std;
It seems that whatever you used to format the code, it's come through on the board as a big mess.

If you can still edit your post, I would suggest you do so, and use "post preview" to make sure we're going to see what you're seeing in your code editor.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jan 17th, 2007
1

Re: String maniputlation runtime error

Tracking down runtime errors can be a hassle. There is no right way to do it. If you can get somebody else to do it for you, good luck. Learing how to do it yourself, however, is probably better in the long run. I would either learn how to use a debugging program or I would liberally sprinkle output statements throughout the program to determine the validity/correctness of the variable I'm watching. When something goes haywire I won't get the output I expect at the time I expect or the program will crash before the an output occurs or whatever. In any event, I've successfully narrowed down the problem to a given line or two that I can analyze or, if I still can't figure it out, I can ask a more specific question with more specific information about the problem at a forum such as this, and I don't have to think about the rest of the program (for a while at least).
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Jan 17th, 2007
0

Re: String maniputlation runtime error

After taking Lerner's advice I have found that myString is not accepting spaces. If I type in a name... Doe, John Hanes, I expect that entire name to be in the string but when I check this all that is included is "Doe,". Any ideas?
Reputation Points: 10
Solved Threads: 0
Light Poster
dmmckelv is offline Offline
33 posts
since Nov 2006
Jan 17th, 2007
0

Re: String maniputlation runtime error

Well done! You've learned one valuable lesson today already. Here's another lesson you should take to heart.

The >> operator will stop input into a given variable when it encounters a whitespace character, which is the space character most often, but could be a tab character or whatever after the first nonwhitespace char in the input stream (that is, >> ignores leading whitespace, but it stops input on nonleading whitespace). In order to input a string that contains one or more whitespace characters you need to use getline() or gets() or fgets(). Given that you are using the STL string class I would suggest the version of getline() that goes something like this:

getline(streamName, variableName, terminatingChar);

where streamName is the input stream you want to use, cin in this case, variableName is the name of the STL string variable you want to use, myString in this case, and terminatingChar is the char used to terminate input with if EOF isn't found first (the terminating char will default to the newline char, so if that's okay with you, and it probably is in this case, then you don't need to send getline() the third parameter. Also, you should be aware there is another form of getline() for C style strings if you should ever need to use that type of string or if you're reading code and see getline() using a different syntax.)

So, instead of:

cin >> myString;

try

getline(cin, myString);

and see what happens.

Good luck!
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Jan 17th, 2007
1

Re: String maniputlation runtime error

And, if you're up for it, here's another piece of unsolicted advice that would be good to learn, at least IMO. NEVER EVER write a full program (or at least nothing more than a simple "Hello World" program) without checking both compile and runtime errors along the way. I recommend never writing more than a few lines or at most a small function without checking and double checking. The time you take early on saves a whole lot of grief tracking down runtime errors later on. Once you reach the level of experience that someone like Salem, or some of the other posters, has, then you can stretch it out, but I can (almost) gaurantee you that even the most experienced programmers don't write anything complicated all at once, either.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Jan 18th, 2007
0

Re: String maniputlation runtime error

Click to Expand / Collapse  Quote originally posted by Lerner ...
NEVER EVER write a full program (or at least nothing more than a simple "Hello World" program) without checking both compile and runtime errors along the way.
Bingo, good advice. Writing a code with 100+ LOC without checking for compile time errors and bugs is suicidal.

The problem worsens when your program starts spanning multiple header and source files (multiple .h and .cpp files).
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How to execute this statement continuously?
Next Thread in C++ Forum Timeline: Is there any more Documenation that I need?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC