Reading Text File

I'm having trouble reading a text file and putting the information on that text file into the console output. I THOUGHT my way about the program was correct..but I can't seem to figure out what is wrong or happening.

The first item in the file should be the number of contributors, and the rest of the file should consist of pairs of lines, with the first line of each pair being a contributor’s name and the second line being a contribution. That is, the file should look like this:
4
Sam Stone
2000
Freida Flass
100500
Tammy Tubbs
5000
Rich Raptor
55000
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
  #include <iostream>
#include <string>
#include<fstream>
using namespace std;

struct society
{
    int contribution;
string contributor;
};

int main()
{
society *contributee = new society[4];
int amount;
int money;
string name;
ifstream fin;
fin.open("fish.txt");
fin>>amount; //read the amount of contributors
cout<<amount<<endl;    //display the amount of contributors
    for(int i =0; i<4;i++)
    {
        fin>>name; //read the name of the contributor
        contributee[i].contributor=name; //store the name into variable
        cout<<contributee[i].contributor<<endl; //print out the name that was stored
        fin>>money;  //get the amount of money
        contributee[i].contribution=money; //store the amount of money in a variable.
        cout<<contributee[i].contribution<<endl; // print the amount of money out.
    }

}


My output is this http://gyazo.com/5b9c7a3376e54633468456bf27b6afb5

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
#include <iostream>
#include <string>
#include<fstream>
#include <cstdlib>
using namespace std;

struct society
{
	int contribution;
	string contributor;
};

int main()
{
	society *contributee = new society[4];
	int amount;
	int money;
	string name;
	ifstream fin;
	fin.open("fish.txt");
	if (!fin)
	{
		cout << "input file failed to open";
		cin.ignore();
		exit(0);
	}
	fin >> amount; //read the amount of contributors
	fin.ignore(1000, '\n');

	cout << amount << endl;    //display the amount of contributors
	for (int i = 0; i < amount; i++)
	{
		getline(fin, name); //read the name of the contributor
		contributee[i].contributor = name; //store the name into variable
		cout << contributee[i].contributor << endl; //print out the name that was stored
		fin >> money; //get the amount of money
		fin.ignore(1000, '\n');
		contributee[i].contribution = money; //store the amount of money in a variable.
		cout << contributee[i].contribution << endl; // print the amount of money out.
	}

	cin.ignore();
	return 0;
}
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
#include <iostream>
#include <string>
#include<fstream>
using namespace std;

struct society
{
    int contribution;
    string contributor;
};

int main()
{
    society *contributee = new society[4];
    int amount;
    int money;
    string name;
    ifstream fin;
    fin.open("fish.txt");
    fin>>amount; //read the amount of contributors
    cout<<amount<<endl;    //display the amount of contributors

    for(int i =0; i<amount;i++) //if you are manually going to check 4, there is no point reading in amount.
    {
          getline(fin, name); //getline is used for strings
          contributee[i].contributor=name; //store the name into variable
          cout<<contributee[i].contributor<<endl; //print out the name that was stored
          fin>>money;  //get the amount of money
          fin.ignore(10, '\n'); //Discards endline character
          contributee[i].contribution=money; //store the amount of money in a variable.
          cout<<contributee[i].contribution<<endl; // print the amount of money out.
    }
    return 0; //You ALWAYS need to return something if it isn't a void function
}
Topic archived. No new replies allowed.