Member Avatar for dmmckelv

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

#include <iostream>
#include <string>
using namespace 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

Recommended Answers

All 6 Replies

> 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.

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).

commented: Very helpful! +1
Member Avatar for dmmckelv

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?

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!

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.

commented: Correct, and good advice. -joeprogrammer +5

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).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.