-
April 15th, 2013, 07:25 PM
#1
Can't read in a text file to load a struc
Hi,
I am doing an exercise that reads in a text file and loads the data into a struct. My problem is it doesn't read anything from the text file. I think it's the way I'm loading the data. Oh, it does read in the first record that tells the program how many contributor records to create, but nothing after that. Here it is:
Code:
//
#include <iostream>
#include <fstream>
#include <cstdlib>
const int strsize = 30;
const int SIZE = 60;
struct contributions {
char fullname[strsize]; //name
double donation; //donation
};
int main()
{
using namespace std;
char filename[SIZE];
ifstream inFile; // object for handling file input
cout << "Enter name of data file: ";
cin.getline(filename, SIZE);
inFile.open(filename); // associate inFile with a file
if (!inFile.is_open()) // failed to open file
{
cout << "Could not open the file " << filename << endl;
cout << "Program terminating.\n";
// cin.get(); // keep window open
exit(EXIT_FAILURE);
}
int howmany = 0;
// cout << "What's the number of contributions?: ";
inFile >> howmany;
int count = 0;
contributions * ps = new contributions[howmany];
while (count < howmany)
{
inFile.get(ps[count].fullname, strsize);
inFile.get();
inFile >> ps[count].donation;
inFile.get();
cout << "fullname: " << ps[count].fullname << "\n";
cout << "donation: " << ps[count].donation << "\n";
count++;
}
if (inFile.eof())
cout << "End of file reached.\n";
else if (inFile.fail())
cout << "Input terminated by data mismatch.\n";
else
cout << "Input terminated for unknown reason.\n";
if (count == 0)
cout << "No data processed.\n";
else
{
cout << "Items read: " << count << endl;
//cout << "Sum: " << sum << endl;
//cout << "Average: " << sum / count << endl;
}
inFile.close();
cout << "Please enter one of the following choices:\n";
cout << "a. Patrons\t" << "b. Grand Patrons\n";
cout << "q. quit\n";
cout << "Enter choice ";
int numdon;
char choice;
cin >> choice;
while (choice != 'Q' && choice != 'q')
{
switch(choice)
{
case 'a':
case 'A':
count = 0;
numdon = 0;
cout << "\tPatrons \n";
while (count < howmany)
{
if (ps[count].donation < 10000)
{
cout << ps[count].fullname << "\t" << ps[count].donation << "\n";
numdon++;
}
count++;
}
if (numdon == 0)
cout << "None\a\n";
break;
case 'b':
case 'B':
count = 0;
numdon = 0;
cout << "\tGrand Patrons \n";
while (count < howmany)
{
if (ps[count].donation >= 10000)
{
cout << ps[count].fullname << "\t" << ps[count].donation << "\n";
numdon++;
}
count++;
}
if (numdon == 0)
cout << "None\a\n";
break;
default : cout << "Enter a valid choice.\n";
}
cout << "Next choice: ";
cin >> choice;
}
cin.get();
cin.get();
return 0;
}
Here is the record input:
Data file:
4
Suz Stuz
55000
Froco Frock
5000
Yandi Yuck
100500
Dippy Dip
120000
9 records - each struct has two members; full name and donation.
First record should be number of contributors
Thanks in advance
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|