Problem in C++ about delimited strings assigned to diff arrays

I am a newbie in c++.Now i have a problem.With my source code..What should i do..This is my problem...

This program tries to retrieve datas from a notepad, named employee.txt,consisting of employees data,
Such as:idnumber,employee name,salary grade.
Ex. A1001,John Davidson,1
A1002,Andrew Jackson,2

I use getline to delimit the string...

Now my problem is how am i going to assign the delimited string into different arrays??
Ex. A1001 and A1002 is a assigned to an array named idnumber.
John Davidson and Andrew Jackson is assigned to an array named emplname..

Re: Problem in C++ about delimited strings assigned to diff arrays

Posted 22 March 2012 - 10:51 PM

Hello Fellas...I have here a problem in C++...
This code is running but still i am having trouble with replacing underscore to space..

I have here an array named empname[5]={"John_Cris_Davidson","Thomas_Earl_Thompson"}..
I used replace to change the underscore into space..But the result is not what i have expected..
The empname values are replaced with spaces instead of replacing just the underscore...

Re: Problem in C++ about delimited strings assigned to diff arrays

Posted 23 March 2012 - 05:56 AM

Since you are just replacing a single character with a single character you have several options.

One option would be to iterate through the string character by character looking for the character that you want to remove. When you find that character replace it and continue to the end of the string.

The next option would be to use the std::string.find() function to search the string for the character you want to replace, and if it is in the string replace the character. Then repeat the find/replace until you fail to find any more of your characters.

Edit: Also since this is basically the same problem as your last post I am going to merge the two topics. Please don't open a new topic for what is basically the same problem.

Re: Problem in C++ about delimited strings assigned to diff arrays

Posted 25 March 2012 - 07:04 AM

No that is not correct. If you insist on using find and replace you need to carefully read the documentation for these functions.

First what is the return type of find()? What does find() return on failure? It does not return an int. It returns a size_t, which is an unsigned type. This is an important difference, because find returns the value std::string::npos on failure. This value is not zero. It is a very large number, the largest number that a size_t can hold which in many cases is larger than what a signed int can hold. The only time find() returns zero is when it finds the search character in the first character in the string. So you will probably have an endless loop with your code.

Now lets move on to your replace call. First, what happens when you don't "find" your character? What is the value of c? As I stated in the previous paragraph it returns std::string::npos. So because c is equal to std::string::npos your program will throw an exception and crash, because c is larger than the size of your string. You need to insure that c is less than the size of your string before calling the replace function. Also you say you want to replace the '_' character why are you replacing the character with a '_' character? Wouldn't the space character ' ' be a better choice?

You would be better off just iterating through the string and looking at each individual character and if it is the value you are looking for replace it with your replacement variable.