I have question, why when I don't put the "inFile.ignore()" line, the program doesnt work. What is the thing that the line ignore?

while(1){
		inFile.getline(name,50);
		inFile>>ass1>>ass2>>ass3;
		inFile.ignore();
		if(inFile.eof()) break;
		cout<<name<<" "<<ass1<<" "<<ass2<<" "<<ass3<<endl;
	}

here is the content of the input file :

akmal
3 2 1
akmal hisyam
1 2 3

Recommended Answers

All 4 Replies

When you don't pass any arguments to ignore, it reads and discards the next character. In your case, that would be the newline character ('\n'). There's a sticky that explains the issues involved (translation: I don't want to write it all again).

The extraction operator breaks the input when it detects a whitespace character. At the end of each input line there is a newline, which is a whitespace character. The newline character is not extracted and is left in the input stream as a "dangling" character. This occurs most often after numeric inputs because a numeric variable can't hold the '\n' char.

The ignore extracts it so that it doesn't mess up your subsequent input operations.

EDIT:
Ack! I didn't know Narue was here...

thanks for replying and sorry for not searching throughly before making post :D

The extraction operator breaks the input when it detects a whitespace character. At the end of each input line there is a newline, which is a whitespace character. The newline character is not extracted and is left in the input stream as a "dangling" character. This occurs most often after numeric inputs because a numeric variable can't hold the '\n' char.

The ignore extracts it so that it doesn't mess up your subsequent input operations.

EDIT:
Ack! I didn't know Narue was here...

thanks a lot for explaining!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.