| | |
String maniputlation runtime error
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2006
Posts: 32
Reputation:
Solved Threads: 0
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
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 263
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).
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 263
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!
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!
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 263
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.
•
•
•
•
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.
The problem worsens when your program starts spanning multiple header and source files (multiple .h and .cpp files).
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- first cannot conver std::string to const char, now runtime error! (C++)
- runtime error#58 (Visual Basic 4 / 5 / 6)
- Runtime Error 424 object required (was: Please Help!) (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: How to execute this statement continuously?
- Next Thread: Is there any more Documenation that I need?
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






