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
#include <iostream>
#include <string>
#include<fstream>
usingnamespace 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.
}
}
#include <iostream>
#include <string>
#include<fstream>
#include <cstdlib>
usingnamespace 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;
}
#include <iostream>
#include <string>
#include<fstream>
usingnamespace 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
}