Student statistics. How to read the middle intial of a student

Pages: 12
So chervil, in the reference page it says I can get a string until a new line is found. Why can't I just use getline(infile,name) without a delimiter? New name will be on a new line?
Chervil, Your solution provided did work; however, I was looking over some articles and found another solution.

1
2
3
4
string name=" ";

getline(infile,name);


I use the same method provided by the articles, but mines does not seem to work. Can you tell me what's wrong with this code or why possibly this code wont work? Should be my last question :D
fahmankhan75 wrote:
I use the same method provided by the articles, but mines does not seem to work.

You need to read and understand the description of the function provided, not just examine the example code and guess what is happening.


fahmankan75 wrote:
Why can't I just use getline(infile,name) without a delimiter? New name will be on a new line?

Your name is not the only data on a line, therefore if you use getline without a delimiter you will get the full line of data, not just the name. (Just as the function description spells out.)
Last edited on
Why can't I just use getline(infile,name) without a delimiter?

If you do, then a default delimiter is used.

This getline(infile,name);

has the same effect as getline(infile,name, '\n');

So really, you cannot avoid using a delimiter. The whole intention behind getline() is that it continues reading characters into the string until it finds a delimiter (or reaches the end of the stream).

The point is, in the context of the data in your file, both the tab '\t' and newline'\n' characters are present. You choose which one to use depending on what you are trying to achieve.
Before I start bugging you guys :D just one tiny concern please :) suppose on text file, this is the name on line 1 with scores. I use this function getline(infile, name,"\n") the "n" means newline. The string reads all the data in that line until new line is found. So, should it not read fahman David khan? Because new line is not happening at that time.

1
2
3

Fahman David khan 95 34 64



Won't bug you Fter this but I do appreciate for the help :D
Before I start bugging you guys :D just one tiny concern please :) suppose on text file, this is the name on line 1 with scores. I use this function getline(infile, name,"\n") the "n" means newline.


std::getline takes a char argument as the delimiter, not a pointer to char (std::getline(infile, name, '\n'))

If the format of the file is as you indicate in the post above, getline will return an empty string, because the first line consists of only a newline. Assuming you do another getline the string extracted would be "Fahman David khan 95 34 64"

It should be noted that if you are using the extraction operator (>>) to read the numbers from the file, those operations will leave any trailing whitespace in the input buffer (including newlines) which will be encountered by a subsequent getline with the default '\n' delimiter which will, therefore, only extract an empty string. One can avoid that by using infile >> std::ws prior to using getline.

Topic archived. No new replies allowed.
Pages: 12