String maniputlation runtime error

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2006
Posts: 32
Reputation: dmmckelv is an unknown quantity at this point 
Solved Threads: 0
dmmckelv dmmckelv is offline Offline
Light Poster

String maniputlation runtime error

 
0
  #1
Jan 17th, 2007
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

Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: String maniputlation runtime error

 
0
  #2
Jan 17th, 2007
> 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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: String maniputlation runtime error

 
1
  #3
Jan 17th, 2007
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).
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 32
Reputation: dmmckelv is an unknown quantity at this point 
Solved Threads: 0
dmmckelv dmmckelv is offline Offline
Light Poster

Re: String maniputlation runtime error

 
0
  #4
Jan 17th, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: String maniputlation runtime error

 
0
  #5
Jan 17th, 2007
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!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: String maniputlation runtime error

 
1
  #6
Jan 17th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,610
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: String maniputlation runtime error

 
0
  #7
Jan 18th, 2007
Originally Posted by Lerner View Post
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).
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC