Trouble understanding File input/output and .get()

Hey guys I've trouble understanding (inFile >> ch).get();. After I added this piece of code, it start working. I found the solution on the Internet but I really want to know what's happening right there.

User is supposed to enter a specific text file which contains some names and the amount of money they donated. Textfile is down below

Can someone explain for me? :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <cstdlib>		// exit() function
#include <string>

const int Contributors = 4;

struct donor
{
		std::string name;
		double money;
};
int main()
{
	using namespace std;

	
	cout << "Welcome to the Society for the Preservation of Rightful Influence" << endl;
	cout << "Which file is it?: ";
	
	char fil[50];			
	ifstream inFile;		
	cin >> fil;				
	inFile.open(fil);		
		
	if (!inFile.is_open())									
	{
		cout << "File not found, terminating program...\a";
		cin.get();
		cin.get();
		exit(EXIT_FAILURE);
	}

	cout << "File found!, Amount of donors/contributors are:  " << endl;

	donor *Mydonor = new donor [Contributors];			

	char ch;
	(inFile >> ch).get();		// <-- This
	cout << ch << endl;
	
	int i = 0;
	while (inFile.good())
	{
		getline(inFile, Mydonor[i].name);     
		cout << "Donor's name#" << i+1 << ": " << Mydonor[i].name<< endl;	
		(inFile >> Mydonor[i].money).get();   // <-- This
		cout << "Amount#" << i+1 << ": " << Mydonor[i].money<< endl;		
		i++;
	}

	if (inFile.eof())
		cout << "\nEnd of file reached!\n";
	
	inFile.close();
	delete [] Mydonor;

	cin.get();
	cin.get();

	return 0;


}


Thanks!

EDIT:
The text file contains:

4
Sam Stone
2000
Freida Flass
100500
Tammy Tubbs
5000
Rich Raptor
55000
Last edited on
The >> operator does two things. First, it extracts data from and input stream ( cin is such a stream ), and then it returns the stream.

The extraction first ignores all white space. It then reads data until a character is found that is not of the type it is looking for. That character is not extracted.

The stream is returned, which allows for chaining:
cin >> x >> endl;

The return also means you can do what is in your code, specifically call get() with the stream that is returned from cin >> num.

The reason for this is that the >> operator has left the new line character in the stream buffer, and this needs to be ignored.
closed account (Dy7SLyTq)
i dont understand why you would use it, but im assuming operator>> for ifstream returns an ifstream, giving access to the .get member
The extraction first ignores all white space. It then reads data until a character is found that is not of the type it is looking for. That character is not extracted.


So, is it a "new line" in the text document that's messing the whole thing up? And when I'm adding .get() it "takes care" of the "new line".

Correct me if I'm wrong...

i dont understand why you would use it, but im assuming operator>> for ifstream returns an ifstream, giving access to the .get member


If there are other ways to solve this, I'm up for suggestions! :]
So, is it a "new line" in the text document that's messing the whole thing up? And when I'm adding .get() it "takes care" of the "new line".


Basically.

The issue with the new line is the use of the function getline(). By default, getline() extracts characters up until ( and including ) a newline character. Since the >> operator doesn't extract a trailing newline, a call to getline() after >> will create an empty string.

The preferred method is to use ignore() after using a >> operator:
1
2
3
int x;
cin >> x;
cin.ignore( 80, '\n' );  // ignore until 80 characters have been ignored, or a newline has been extracted 
Alright, then I understand! Thank you!
you can still use getline(), and then thefile >> ws to "Eat up" leading white spaces.
Topic archived. No new replies allowed.