• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

C++: Getline function woes

Status
Not open for further replies.

Macam

Banned
So I'm working on a basic C++ payroll program, but I'm having trouble getting the getline() function to work properly. It's supposed to read from a plain text file an employee's ID number, name, payrate, and the number of dependents and store them into 5 separate variables. So the information in the text file is formatted as follows:

012 James Baker 13.50 2 M
015 Marianne Verince 15.00 1 F
etc.

That last piece of data is the employee's gender, but that can be ignored easily enough so I'm not concerned with that. In any case, I'm using the following line of code to call the getline function (inputFile represents the text file being read in, declared via ifstream):

inputFile.getline(id, 3) ;

That should, according to what I know in any case, get the first three characters of the line (or until a newline character is reached) and store into the 'id' variable...which it does -- only it's storing in a character array and converting it, which is the problem I'm having. I've also tried static_casting it, but that doesn't seem to work either as I get the same warning regarding an undesired conversion.

So that's essentially it. A basic C++ problem...any help is tremendously appreciated and I'll be more than happy to clarify if anyone that can help needs it.
 

maharg

idspispopd
A) Don't use the member function getline(). Use the global one that puts it in a std::string. Raw string arrays are for idiots and people who really know what they're doing. Everyone else should use std::string until they decide to be one or the other. At the moment, you don't seem to know enough about them to fit into the latter category, so please don't fall into the former.
B) You are, as the person above pointed out, using the wrong function here. You want to use a formatted extractor and just put it into an int variable. int x; stream >> x; In fact, lets go whole hog:

Code:
int id;
std::string firstname;
std::string lastname;
double whatever;
int whateverelse;
char gender;

stream >> id >> firstname >> lastname >> whatever >> whateverelse >> gender;

Mind you there's no error detection or correction there, but frankly, error detection with iostream is hell and I don't feel like explaining it.
C) Again, don't use char arrays unless you know wtf you're doing.
 

Macam

Banned
I forgot to add that I'm supposed to use C-strings (not the C++ string class) to represent strings in my program -- from what I gather that may have an effect on the suggested help, correct me if I'm wrong.

And thanks again, still getting my feet wet with C++ unfortunately.
 
Macam said:
I forgot to add that I'm supposed to use C-strings (not the C++ string class) to represent strings in my program -- from what I gather that may have an effect on the suggested help, correct me if I'm wrong.

And thanks again, still getting my feet wet with C++ unfortunately.

Then you can declare firstname as a dynamically allocated character array.

char *firstname = new char[];

A null character is appended when using the formatted extractor so firstname will be a string.

inputfile>>firstname;

edit: Hopefully you DO know what the fuck you're doing when it comes to character arrays. Otherwise, I'm with Maharg. Best not to fuck with them unless you have no other option.
 

maharg

idspispopd
Unfortunately for you, your teacher is forcing you into the idiot camp. Not your fault, mind you, but theirs.

Good luck.
 

Macam

Banned
maharg said:
Unfortunately for you, your teacher is forcing you into the idiot camp. Not your fault, mind you, but theirs.

Good luck.

Music to my hears =/ Thanks to the both of you, I'll see if I can't hammer this out tonight.

Given the abundance of C++ resources out there, I don't suppose you two, or anyone else, have any recommended reading material or preferred web sites/books/etc to get a good foundation going? If I can learn better ways to do things, I'd sooner learn them now than find out later.
 

maharg

idspispopd
Get Accelerated C++, which teaches the language the right way around. It's pretty short and cheap too, as C++ books go.

Do NOT use Thinking in C++. It's not worth what you pay for it (free as in cheap, not free as in good).
 
Status
Not open for further replies.
Top Bottom