Help with homework-Structures & String Processing

The structure that will be used in both of the programs will represent one team. It has fields for the school name, team name, score for game 1, score for game 2, and score for game 3.

1
2
3
4
5
6
7
8
9
struct teamInfo
{
char schoolName[20];
char teamName[20];
int game1Score;
int game2Score;
int game3Score;
};

The file consists of a set of multi-line records for the teams that participated in the tournament. Each record represents one team and each line for a record represents a piece of information for the team. The lines for each player are: the school name, the team name, the score for game 1, the score for game 2, and the score for game 3.

Takes a file called teams.txt that looks similar like

DELAWARE ST.
BLUE HENS
187
162
171
With more information that follows

Output

The output will be a file called binary_teams. It will contain a structure for each of the teams in the teams.txt file.

Suggested Logic for main()

Create a teamInfo variable, C-Style string, input file stream, and output file stream.

Open the file teams.txt for input and verify that it opened correctly

Open the file binary_teams for *binary* output and verify that it opened correctly

Use getline to read the school name from the input file into the C-Style string

While there are input records in the input file

Put the school name into the appropriate field of the teamInfo variable

Use getline to read the team name from the input file into the C-Style string
Put the team name into the appropriate field of the teamInfo variable

Use getline to read the score for game 1 from the input file into the C-Style string
Convert the C-style string to an integer value
Put the integer version of the score for game 1 into the appropriate field of the teamInfo variable

Use getline to read the score for game 2 from the input file into the C-Style string
Convert the C-style string to an integer value
Put the integer version of the score for game 2 into the appropriate field of the teamInfo variable

Use getline to read the score for game 3 from the input file into the C-Style string
Convert the C-style string to an integer value
Put the integer version of the score for game 3 into the appropriate field of the teamInfo variable

Write the teamInfo variable to the output file using the write command

Use getline to read the next school name from the input file into the C-Style string
Endwhile

Close the input and output files


I have no idea how to make write this, due to being sick and missing a class or two.
Last edited on
Sorry to hear you were sick. Here's a tutorial to help you back on your tracks:

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

Feel free to post specific questions about anything you don't understand there, and please make sure you post your code and any error messages you are receiving.

I also remind you to use code tags whenever you post code. Like this:

[code]
1
2
//Code here gets formatted.
//It also receives line numbers. 
[/code]

Finally, please make sure you post code that compiles whenever possible, unless you actually need help to make the program compile. If you don't have a compiler at hand, use www.ideone.com.
okay i fixed the code part in the thread.. i just have no idea how to get this part of the hw done
Show the code you have so far. Surely you have some more written, right?
i have the second part that reads what the first program grabs and stores. I just dont remember how to make this part work


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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <iomanip>
#include <fstream>

#define MAXTEAMS 48

using namespace std;

struct teamInfo
{
char schoolName[20];
char teamName[20];
int game1Score;
int game2Score;
int game3Score;
};


void calcStats(teamInfo[], int, double, double, double, int, int);
void printArray(teamInfo[], int);
int buildArray(teamInfo[]);

int main()
{
teamInfo teamAr[MAXTEAMS];
int teamcount, game1, game2, game3, totscore, loclow, lochigh;
double avgGame1, avgGame2, avgGame3;

teamcount = buildArray(teamAr);

printArray( teamAr, teamcount);

calcStats( teamAr, teamcount, avgGame1, avgGame2, avgGame3, lochigh, loclow);

cout << "Tiddly-Winks Tournament Statistics\n";
cout << "----------------------------------";
cout << "Average Game 1 Score\t" << avgGame1 << endl;
cout << "Average Game 2 Score\t" << avgGame2 << endl;
cout << "Average Game 3 Score\t\n" << avgGame3 << endl;
cout << "Highest Scoring Team\t" << teamAr[lochigh].teamName << endl;
cout << "Lowest Scoring Team\t" << teamAr[loclow].teamName << endl;

}

/********************************************************************
This function is used to calculate various stats from the tournament. It
the array, the count of teams, the averages of the games, and the location
of the highest and lowest scores as its arguments
*********************************************************************/

void calcStats (teamInfo teamAr[], int teamcount, double &avgGame1, double &avgGame2,
double &avgGame3, int &lochigh, int &loclow)
{
int i;
double sum1 = 0.00, sum2 = 0.00, sum3 = 0.00;

int temphigh,templow, total;

for (i == 0; i < teamcount; i++)
{
sum1 = sum1 + teamAr[i].game1Score;
sum2 = sum2 + teamAr[i].game2Score;
sum3 = sum3 + teamAr[i].game3Score;

total = teamAr[i].game1Score + teamAr[i].game2Score + teamAr[i].game3Score;

if ( total > temphigh)
{
temphigh = total;
lochigh = i;
}

if ( total < templow)
{
templow = total;
loclow = i;
}

}

avgGame1 = sum1 / teamcount;
avgGame2 = sum2 / teamcount;
avgGame3 = sum3 / teamcount;

}

/********************************************************************
This function totals up the score of all three games, and also displays all
of the information gathered in the tournament by printing the array. It has
the teaminfo array and number of teams as its arguments.
********************************************************************/

void printArray (teamInfo teamAr[], int teamcount)
{
int i, total, sub = 0;
cout << "\t\tBig 9 Conference Tiddly-Winks Tournament\n\n";
cout << "School Name\t" << "Team Name\t" << "Game 1 " << "Game 2 ";
cout << "Game 3 " << "Total Score\n";
cout << "--------------------------------------------------------\n\n";
for (i == 0; i < teamcount; i++)
{
total = teamAr[i].game1Score + teamAr[i].game2Score + teamAr[i].game3Score;

cout << teamAr[i].schoolName << setw(9) << teamAr[i].teamName << setw( 7);
cout << teamAr[i].game1Score << setw(4) << teamAr[i].game2Score <<setw(4);
cout << teamAr[i].game3Score << setw(4) << total << endl;
sub ++;
if (sub%4 == 0)
cout << endl;
}
}

/*********************************************************************
This function builds the array. It gets the information from the other
program and makes an array with structures as the types. It has the teaminfo
team array as its argument, and returns the number of valid teams.
**********************************************************************/

int buildArray(teamInfo teamAr[])
{
ifstream inFile;
int i;
teamInfo aTeam;

inFile.open ( "binary_teams" );

if ( inFile.fail() )
{
cout << "input file did not open";
exit(0);
}

i = 0;

inFile.read( (char *) &aTeam, sizeof(aTeam) );

while (inFile)
{
teamAr[i] = aTeam;

i++;

inFile.read( (char *) &aTeam, sizeof(aTeam) );
}

return i;
}
Last edited on
1. You are NOT using code code tags. See how in my example the code gets colored and each line receives a number? Your post did not get that, meaning you didn't use code tags as shown.
2. The buildArray() function is using "binary_teams" as input, but that's the output file name.
3. You are not using std::getline() as you were told by the suggested logic. You are instead using std::ifstream::read(). Since the lines are of variable width, using this approach complicates things. Use std::getline() to read one line at a time.
4. You cannot read from the text file and into a variable of type struct teamInfo. You can only do that if the file was in binary format (the binary file called "binary_teams", but at this point you should be reading the text file, not the binary file).
Last edited on
the program above had different rules, the first part had to take the teams txt and convert it and save it under binary_teams. Then the program above would open it and read it and output it to the screen
I see. Well, in that case your code post has no relevance. Start by writing the appropriate main() function for this part. Your homework statement pretty much sums up what you need to do.
Topic archived. No new replies allowed.