Hi, I have a C++ programming assignment and am new to programming. My assignment is to read from an input file into an array of structs but for some reason all im reading in is garbage could really use some advice. If anybody could help thank you.
Heres my code so far:
#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
//Struct for Athletes
struct Athletes
{
int athletenum;
char sport[5];
double score[5];
};
void openfiles(ifstream & ,ofstream & );
void getathletes(ifstream &, Athletes scrlist[100]);
double total(double total, Athletes scrlist[100]);
void printcontestants(Athletes scrlist [100], ofstream &, double, double, int, char );
int main()
{
//Declares an array of type struct
Athletes scrlist[100];
double total1=0, total2=0;
int winner=0;
int contestant=0;
ifstream infile;
ofstream outfile;
openfiles(infile, outfile);
outfile << "Kenley O'Brien" << endl;
outfile << "CMPS 2 Program 1" << endl;
if(infile.fail())
cout << " error opening file " << endl;
getathletes(infile, scrlist);
printcontestants(scrlist, outfile, total1, total2, winner, contestant);
outfile << "Athlete of the Year: " << contestant << "With a total score of: " << winner << endl;
outfile << " Computer Science Rocks!!!!!!!" << endl;
infile.close();
outfile.close();
system("pause");
return 0;
}
//function to open files
void openfiles(ifstream & infile, ofstream & outfile)
{
char infilename[20];
char outfilename[20];
cout << "Please enter a file name for use: " << endl;
cin >> infilename;
cout << "Please enter a file for the output: " << endl;
cin >> outfilename;
infile.open(infilename);
outfile.open(outfilename);
}
//funciton to read in from file into array of structs
void getathletes(ifstream & infile, Athletes scrlist[100])
{
int i=0;
infile>>scrlist[i].athletenum;
while(scrlist[i].athletenum)
{
infile >> scrlist[i].athletenum;
for(int j=0; j<5; j++)
{
infile >> scrlist[i].sport[j];
infile >> scrlist[i].score[j];
}
i++;
}
}
//prints out athlete data
//for some reason I could not get this function to work
void printcontestants(Athletes scrlist[100], ofstream & outfile, double total1, double total2, int winner, char contestant)
{
for(int i=0; i<100; i++)
{
outfile << scrlist[i].athletenum;
for(int j=0; j<5; j++)
{
if(scrlist[i].sport[j]=='D')
outfile << "Diving; ";
else if(scrlist[i].sport[j] == 'G')
outfile << "Gymnastics: ";
else if(scrlist[i].sport[j] == 'B')
outfile <<"Boxing: ";
else if(scrlist[i].sport[j] == 'S')
outfile <<"Skating: ";
else if(scrlist[i].sport[j] == 'P')
outfile <<"Posing: ";
outfile << setw(10) << scrlist[i].score[j] << endl;
total1 = total(total1, scrlist);
outfile << "Total: " <<setw(15) << total << endl;
if(total2<total1)
{
total2=winner;
contestant=scrlist[i].athletenum;
}
}
}
outfile << "Athlete of the Year: " << contestant << "With a total score of: " << winner << endl;
outfile << " Computer Science Rocks!!!!!!!" << endl;
}
//gets total scores
//This is one of two functions that I could not get to work
//it is something wrong with my parameters
double total(double total1, Athletes scrlist[100])
{
for(int i=0; i<100; i++)
{
for (int j=0; j<5; j++)
{
total1 =0;
total1 = total1 + scrlist[i].score[j];
}
}
return total1;
}