PDA

View Full Version : Need Help Please...Won't Read File


splackavellie69
12-20-2006, 05:40 PM
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

struct dormgames
{
string firstname;
string lastname;
string dorm;
string sport1;
string sport2;
float money;
};

float footballmoney;
float tennismoney;
float soccermoney;
float basketballmoney;

float btfootballmoney;
float bttennismoney;
float btsoccermoney;
float btbasketballmoney;

float dgfootballmoney;
float dgtennismoney;
float dgsoccermoney;
float dgbasketballmoney;

float ftfootballmoney;
float fttennismoney;
float ftsoccermoney;
float ftbasketballmoney;

float tufootballmoney;
float tutennismoney;
float tusoccermoney;
float tubasketballmoney;

float opfootballmoney;
float optennismoney;
float opsoccermoney;
float opbasketballmoney;

float lnfootballmoney;
float lntennismoney;
float lnsoccermoney;
float lnbasketballmoney;

int footballstu;
int tennisstu;
int soccerstu;
int basketballstu;

int btfootballstu;
int bttennisstu;
int btsoccerstu;
int btbasketballstu;

int dgfootballstu;
int dgtennisstu;
int dgsoccerstu;
int dgbasketballstu;

int ftfootballstu;
int fttennisstu;
int ftsoccerstu;
int ftbasketballstu;

int tufootballstu;
int tutennisstu;
int tusoccerstu;
int tubasketballstu;

int opfootballstu;
int optennisstu;
int opsoccerstu;
int opbasketballstu;

int lnfootballstu;
int lntennisstu;
int lnsoccerstu;
int lnbasketballstu;

float bttotal1;
float dgtotal1;
float fttotal1;
float tutotal1;
float optotal1;
float lntotal1;

int bttotal2;
int dgtotal2;
int fttotal2;
int tutotal2;
int optotal2;
int lntotal2;

ifstream infile;


int main()
{

dormgames records[25];
int x = 0;

infile.open("sports.dat");

while (infile)
{
infile >> records[x].firstname >> records[x].lastname >> records[x].dorm >> records[x].sport1 >> records[x].sport2 >> records[x].money;

x++;
}



for (x = 0; x < 25; x++)
if(records[x].sport2 == "football" || records[x].sport2 == "football")
{
footballmoney += records[x].money / 2;
}

else if(records[x].sport2 == "tennis" || records[x].sport2 == "tennis")
{
tennismoney += records[x].money / 2;
}

else if(records[x].sport2 == "soccer" || records[x].sport2 == "soccer")
{
soccermoney += records[x].money / 2;
}

else if(records[x].sport2 == "basketball" || records[x].sport2 ==
"basketball")
{
basketballmoney += records[x].money / 2;
}

for (x = 0; x < 25; x++)
if (records[x].dorm == "BT")
{
if(records[x].sport2 == "football" || records[x].sport2 == "football")
{
btfootballmoney += records[x].money / 2;
}

else if(records[x].sport2 == "tennis" || records[x].sport2 == "tennis")
{
bttennismoney += records[x].money / 2;
}

else if(records[x].sport2 == "soccer" || records[x].sport2 == "soccer")
{
btsoccermoney += records[x].money / 2;
}

else if(records[x].sport2 == "basketball" || records[x].sport2 ==
"basketball")
{
btbasketballmoney += records[x].money / 2;
}
}
for (x = 0; x < 25; x++)
if (records[x].dorm == "DG")
{
if(records[x].sport2 == "football" || records[x].sport2 == "football")
{
dgfootballmoney += records[x].money / 2;
}

else if(records[x].sport2 == "tennis" || records[x].sport2 == "tennis")
{
dgtennismoney += records[x].money / 2;
}

else if(records[x].sport2 == "soccer" || records[x].sport2 == "soccer")
{
dgsoccermoney += records[x].money / 2;
}

else if(records[x].sport2 == "basketball" || records[x].sport2 ==
"basketball")
{
dgbasketballmoney += records[x].money / 2;
}
}
for (x = 0; x < 25; x++)
if (records[x].dorm == "FT")
{
if(records[x].sport2 == "football" || records[x].sport2 == "football")
{
ftfootballmoney += records[x].money / 2;
}

else if(records[x].sport2 == "tennis" || records[x].sport2 == "tennis")
{
fttennismoney += records[x].money / 2;
}

else if(records[x].sport2 == "soccer" || records[x].sport2 == "soccer")
{
ftsoccermoney += records[x].money / 2;
}

else if(records[x].sport2 == "basketball" || records[x].sport2 ==
"basketball")
{
ftbasketballmoney += records[x].money / 2;
}
}
for (x = 0; x < 25; x++)
if (records[x].dorm == "TU")
{
if(records[x].sport2 == "football" || records[x].sport2 == "football")
{
tufootballmoney += records[x].money / 2;
}

else if(records[x].sport2 == "tennis" || records[x].sport2 == "tennis")
{
tutennismoney += records[x].money / 2;
}

else if(records[x].sport2 == "soccer" || records[x].sport2 == "soccer")
{
tusoccermoney += records[x].money / 2;
}

else if(records[x].sport2 == "basketball" || records[x].sport2 ==
"basketball")
{
tubasketballmoney += records[x].money / 2;
}
}
for (x = 0; x < 25; x++)
if (records[x].dorm == "OP")
{
if(records[x].sport2 == "football" || records[x].sport2 == "football")
{
opfootballmoney += records[x].money / 2;
}

else if(records[x].sport2 == "tennis" || records[x].sport2 == "tennis")
{
optennismoney += records[x].money / 2;
}

else if(records[x].sport2 == "soccer" || records[x].sport2 == "soccer")
{
opsoccermoney += records[x].money / 2;
}

else if(records[x].sport2 == "basketball" || records[x].sport2 ==
"basketball")
{
opbasketballmoney += records[x].money / 2;
}
}
for (x = 0; x < 25; x++)
if (records[x].dorm == "LN")
{
if(records[x].sport2 == "football" || records[x].sport2 == "football")
{
lnfootballmoney += records[x].money / 2;
}

else if(records[x].sport2 == "tennis" || records[x].sport2 == "tennis")
{
lntennismoney += records[x].money / 2;
}

else if(records[x].sport2 == "soccer" || records[x].sport2 == "soccer")
{
lnsoccermoney += records[x].money / 2;
}

else if(records[x].sport2 == "basketball" || records[x].sport2 ==
"basketball")
{
lnbasketballmoney += records[x].money / 2;
}
}
for (x = 0; x < 25; x++)

if(records[x].sport2 == "football" || records[x].sport2 == "football")
{
footballstu += 1;
}

else if(records[x].sport2 == "tennis" || records[x].sport2 == "tennis")
{
tennisstu += 1;
}

else if(records[x].sport2 == "soccer" || records[x].sport2 == "soccer")
{
soccerstu += 1;
}

else if(records[x].sport2 == "basketball" || records[x].sport2 ==
"basketball")
{
basketballstu+= 1;
}

for (x = 0; x < 25; x++)
if(records[x].dorm == "BT")
{
if(records[x].sport2 == "football" || records[x].sport2 == "football")
{
footballstu += 1;
}

else if(records[x].sport2 == "tennis" || records[x].sport2 == "tennis")
{
tennisstu += 1;
}

else if(records[x].sport2 == "soccer" || records[x].sport2 == "soccer")
{
soccerstu += 1;
}

else if(records[x].sport2 == "basketball" || records[x].sport2 ==
"basketball")
{
basketballstu += 1;
}
}
for (x = 0; x < 25; x++)
if(records[x].dorm == "DG")
{
if(records[x].sport2 == "football" || records[x].sport2 == "football")
{
dgfootballstu += 1;
}

else if(records[x].sport2 == "tennis" || records[x].sport2 == "tennis")
{
dgtennisstu += 1;
}

else if(records[x].sport2 == "soccer" || records[x].sport2 == "soccer")
{
dgsoccerstu += 1;
}

else if(records[x].sport2 == "basketball" || records[x].sport2 ==
"basketball")
{
dgbasketballstu += 1;
}
}
for (x = 0; x < 25; x++)
if(records[x].dorm == "FT")
{
if(records[x].sport2 == "football" || records[x].sport2 == "football")
{
ftfootballstu += 1;
}

else if(records[x].sport2 == "tennis" || records[x].sport2 == "tennis")
{
fttennisstu += 1;
}

else if(records[x].sport2 == "soccer" || records[x].sport2 == "soccer")
{
ftsoccerstu += 1;
}

else if(records[x].sport2 == "basketball" || records[x].sport2 ==
"basketball")
{
ftbasketballstu += 1;
}
}
for (x = 0; x < 25; x++)
if(records[x].dorm == "TU")
{
if(records[x].sport2 == "football" || records[x].sport2 == "football")
{
tufootballstu += 1;
}

else if(records[x].sport2 == "tennis" || records[x].sport2 == "tennis")
{
tutennisstu += 1;
}

else if(records[x].sport2 == "soccer" || records[x].sport2 == "soccer")
{
tusoccerstu += 1;
}

else if(records[x].sport2 == "basketball" || records[x].sport2 ==
"basketball")
{
tubasketballstu += 1;
}
}
for (x = 0; x < 25; x++)
if(records[x].dorm == "OP")

if(records[x].sport2 == "football" || records[x].sport2 == "football")
{
opfootballstu += 1;
}

else if(records[x].sport2 == "tennis" || records[x].sport2 == "tennis")
{
optennisstu += 1;
}

else if(records[x].sport2 == "soccer" || records[x].sport2 == "soccer")
{
opsoccerstu += 1;
}

else if(records[x].sport2 == "basketball" || records[x].sport2 ==
"basketball")
{
opbasketballstu += 1;
}

for (x = 0; x < 25; x++)
if(records[x].dorm == "LN")
{
if(records[x].sport2 == "football" || records[x].sport2 == "football")
{
lnfootballstu += 1;
}

else if(records[x].sport2 == "tennis" || records[x].sport2 == "tennis")
{
lntennisstu += 1;
}

else if(records[x].sport2 == "soccer" || records[x].sport2 == "soccer")
{
lnsoccerstu += 1;
}

else if(records[x].sport2 == "basketball" || records[x].sport2 ==
"basketball")
{
lnbasketballstu += 1;
}
}

bttotal1 = btfootballmoney + bttennismoney + btsoccermoney + btbasketballmoney;

dgtotal1 = dgfootballmoney + dgtennismoney + dgsoccermoney + dgbasketballmoney;

fttotal1 = ftfootballmoney + fttennismoney + ftsoccermoney + ftbasketballmoney;

tutotal1 = tufootballmoney + tutennismoney + tusoccermoney + tubasketballmoney;

optotal1 = opfootballmoney + optennismoney + opsoccermoney + opbasketballmoney;

lntotal1 = lnfootballmoney + lntennismoney + lnsoccermoney + lnbasketballmoney;


bttotal2 = btfootballstu + bttennisstu + btsoccerstu + btbasketballstu;

dgtotal2 = dgfootballstu + dgtennisstu + dgsoccerstu + dgbasketballstu;

fttotal2 = ftfootballstu + fttennisstu + ftsoccerstu + ftbasketballstu;

tutotal2 = tufootballstu + tutennisstu + tusoccerstu + tubasketballstu;

optotal2 = opfootballstu + optennisstu + opsoccerstu + opbasketballstu;

lntotal2 = lnfootballstu + lntennisstu + lnsoccerstu + lnbasketballstu;

cout << fixed << setprecision(2);

cout << "1. What is the total amount of money spent on each sport?" << endl;
cout << "Question #1 Answer: football: $" << footballmoney << endl;
cout << " tennis : $" << tennismoney << endl;
cout << " soccer: $" << soccermoney << endl;
cout << " basketball: $" << basketballmoney << endl << endl;

cout << "2. What is the total amount of money spent at each dormitory?" << endl;
cout << "Question #2 Answer: BT:" << endl;
cout << " total: $" << bttotal1 << endl;

cout << " DG:" << endl;
cout << " total: $" << dgtotal1 << endl;

cout << " FT:" << endl;
cout << " total: $" << fttotal1 <<endl;

cout << " TU:" << endl;
cout << " total: $" << tutotal1 << endl;

cout << " OP:" << endl;
cout << " total: $" << optotal1 << endl;

cout << " LN:" << endl;
cout << " total: $" << lntotal1 << endl;

cout << "3. What is the total amount of money spent on each sport at each dormitoy?" << endl;
cout << "Question #3 Answer: BT:" << endl;
cout << " football: $" << btfootballmoney << endl;
cout << " tennis : $" << bttennismoney << endl;
cout << " soccer: $" << btsoccermoney << endl;
cout << " basketball: $" << btbasketballmoney << endl << endl;

cout << " DG:" << endl;
cout << " football: $" << dgfootballmoney << endl;
cout << " tennis : $" << dgtennismoney << endl;
cout << " soccer: $" << dgsoccermoney << endl;
cout << " basketball: $" << dgbasketballmoney << endl << endl;

cout << " FT:" << endl;
cout << " football: $" << ftfootballmoney << endl;
cout << " tennis : $" << fttennismoney << endl;
cout << " soccer: $" << ftsoccermoney << endl;
cout << " basketball: $" << ftbasketballmoney << endl << endl;

cout << " TU:" <<endl;
cout << " football: $" << tufootballmoney << endl;
cout << " tennis : $" << tutennismoney << endl;
cout << " soccer: $" << tusoccermoney << endl;
cout << " basketball: $" << tubasketballmoney << endl << endl;

cout << " OP:" << endl;
cout << " football: $" << opfootballmoney << endl;
cout << " tennis : $" << optennismoney << endl;
cout << " soccer: $" << opsoccermoney << endl;
cout << " basketball: $" << opbasketballmoney << endl << endl;

cout << " LN:" << endl;
cout << " football: $" << lnfootballmoney << endl;
cout << " tennis : $" << lntennismoney << endl;
cout << " soccer: $" << lnsoccermoney << endl;
cout << " basketball: $" << lnbasketballmoney << endl << endl << endl;


cout << "4. What is the total number of students participating in each of the sports?" << endl;
cout << "Question #4 Answer: football: $" << footballstu << endl;
cout << " tennis : $" << tennisstu << endl;
cout << " soccer: $" << soccerstu << endl;
cout << " basketball: $" << basketballstu << endl << endl << endl;

cout << "5. What is the total number of students participating at each of the dormitories?" << endl;
cout << "Question #5 Answer: BT:" << endl;
cout << " total: $" << bttotal2 << endl << endl;

cout << " DG:" << endl;
cout << " total: $" << dgtotal2 << endl << endl;

cout << " FT:" << endl;
cout << " total: $" << fttotal2 << endl << endl;

cout << " TU:" << endl;
cout << " total: $" << tutotal2 << endl << endl;

cout << " OP:" << endl;
cout << " total: $" << optotal2 << endl << endl;

cout << " LN:" << endl;
cout << " total: $" << lntotal2 << endl << endl<< endl;

cout << "6. What is the total number of students participating in each of the sports at each of the dormitories?" << endl;
cout << "Question #6 Answer: BT:" << endl;
cout << " football: $" << btfootballstu << endl;
cout << " tennis : $" << bttennisstu << endl;
cout << " soccer: $" << btsoccerstu << endl;
cout << " basketball: $" << btbasketballstu << endl << endl;

cout << " DG:" << endl;
cout << " football: $" << dgfootballstu << endl;
cout << " tennis : $" << dgtennisstu << endl;
cout << " soccer: $" << dgsoccerstu << endl;
cout << " basketball: $" << dgbasketballstu << endl << endl;

cout << " FT:" << endl;
cout << " football: $" << ftfootballstu << endl;
cout << " tennis : $" << fttennisstu << endl;
cout << " soccer: $" << ftsoccerstu << endl;
cout << " basketball: $" << ftbasketballstu << endl << endl;

cout << " TU:" <<endl;
cout << " football: $" << tufootballstu << endl;
cout << " tennis : $" << tutennisstu << endl;
cout << " soccer: $" << tusoccerstu << endl;
cout << " basketball: $" << tubasketballstu << endl << endl;

cout << " OP:" << endl;
cout << " football: $" << opfootballstu << endl;
cout << " tennis : $" << optennisstu << endl;
cout << " soccer: $" << opsoccerstu << endl;
cout << " basketball: $" << opbasketballstu << endl << endl;

cout << " LN:" << endl;
cout << " football: $" << lnfootballstu << endl;
cout << " tennis : $" << lntennisstu << endl;
cout << " soccer: $" << lnsoccerstu << endl;
cout << " basketball: $" << lnbasketballstu << endl << endl << endl;


return 0;
}

this is the code i am using for an assignment i cant get the file to read into the array can somebody please take a look at it and tell me what i am doing wrong???

nikkiH
12-20-2006, 08:02 PM
That's not quite how you read a file.
Check out this sample code.

http://www.cplusplus.com/doc/tutorial/files.html


// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}

else cout << "Unable to open file";

return 0;
}