using typedef and using function call

I am having a hard time understanding the typedef, and using it in function calls. Also how to use atoi with a function call inside its parameter? Thanks for your help


typedef char Line[LINE_SIZE] //typedef Line
//Prototypes
void stringBuild(fstream &inputFile, Line& oneLine, Line& oneString, int &pos);
char readFile(ifstream& inFile, Team teams[LEAGUE_SIZE], Line& oneline );
//main
int main( const int argc, const char* argv[], Line &)
{
Team teams[LEAGUE_SIZE];
LeagueTotals leagueStats;

Line oneLine;
Line oneString;
//Function call
stringBuild(); //what parameters do I put in?
readFile(filename, teams[LEAGUE_SIZE), oneline);
//How do I do this?
}

strcpy(teams[i].name, atoi(stringBuild()));// I think it's one of these, orclose
teams[i].wins = atoi(stringBuild();) ;
Any help would be appreciated!!

A typedef is simply a way of giving an existing thing another name.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

typedef int number;

int main()
  {
  number n = 42;
  cout << "A number: " << n << ".\n";
  return 0;
  }


Playing with fixed-size arrays and parameter types is typically a no-no. However, in your case, you shouldn't have much problem.
1
2
3
ifstream f( "teamdata.txt" );
int n = 0;
stringBuild( f, oneLine, oneString, n );


I think, however, that you are making some mistakes in how you read and write your league data. I didn't get a chance to repond to your last post the other day (or was it earlier today?), but you should make some functions that read and write a single team's data from and to file. For example, given the file:
Name Wins Losses Ties Points Points/Game
Badgers 0 0 0 0 0.0
Beavers 11 2 2 24 1.6
Bulldogs 2 11 5 9 0.5
Gophers 7 3 1 15 1.4
Mavericks 8 12 6 22 0.8
Seawolves 15 15 4 34 1.0
Totals 43 43 18 104 1.0


You first need data types that match what you are doing.
1
2
3
4
5
6
7
8
9
struct TeamStats
  {
  string name;
  int    wins;
  int    losses;
  int    ties;
  int    points;
  double points_per_game;
  };
Next, you need a function to read a team's stats from input:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <sstream>
#include <string>

istream& operator >> ( istream& ins, TeamStats& result )
  {
  // This is the whole record for our team (a single line of the input):
  string s;
  getline( ins, s );

  // Now let's get the pieces from that single line
  istringstream ss( s );
  ss >> result.name
     >> wins
     >> losses
     >> ties
     >> points
     >> points_per_game;

  // If something went wrong, report it
  if (ss.fail()) ins.setstate( ios::failbit );

  return ins;
  }
Notice how this code catches errors.
You will also want a function to write a team's stats to file.
ostream& operator << ( ostream& outs, const TeamStats& stats );
I'll leave the details of that one to you.

You will also want a function that reads a list of TeamStats from file, and likewise to file. The difference will be that you'll have to handle the first line of the file specially, since it isn't techinically team statistics, just column names.

Hope this helps.
Thanks for the last post, that really helped!

Here is my code updated. I think I figured out the typedef, but I am having a hard time with readFile Function.

If anyone could take a look at my code and help me out, it would be appreciated.


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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
[code]

#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <cstring>
#include <cstdlib>
//#include "inputFile.h"

using namespace std;

/********************************************************************
*** FUNCTION <name of function> ***
*********************************************************************
*** DESCRIPTION : <detailed english description of the function> ***
*** INPUT ARGS : <list of all input argument names> ***
*** OUTPUT ARGS : <list of all output argument names> ***
*** IN/OUT ARGS : <list of all input/output argument names> ***
*** RETURN : <return type and return value name> ***
********************************************************************/
const int LINE_SIZE = 80;
const int LEAGUE_SIZE = 12;
	
typedef char Line[LINE_SIZE];  //typedef Line
	
struct Team
{
	char Line;
	int wins;
	int losses;
	int ties;
	int totalPoints;
	int PPG;

}; //Team Structure
struct LeagueTotals
{
	int totalWins;
	int totalLosses;
	int totalTies;
	int totalPoints;
	int totalPPG;
}; //LeagueTotals Structure



//************************************************************//
//               Function Prototypes                          //
//************************************************************//
void introduction();
void stringBuild(fstream &inputFile, Line& oneLine, Line& oneString, int &pos);
char readFile(Team teams[], int LEAGUE_SIZE, Line& filename ) ; 
void teamStatistics(Team teams[LEAGUE_SIZE], LeagueTotals LeagueStats, Line& filename);
void teamSort(Team teams[LEAGUE_SIZE], LeagueTotals LeagueStats, Line& filename);

int main( const int argc, const char* argv[], Line &filename) // general solution algorithm
{
	Team teams[LEAGUE_SIZE];
	LeagueTotals leagueStats;
	
	Line oneLine;
	Line oneString;
	//Line filename;
	
	ifstream inFile;
	char ch;
	int pos = 0;
	
	introduction();
	cout << "Reading data from the file.\n\n";
	inFile.open("ushl.txt");

		
	cout << "Reading File" << endl << endl;
    readFile(teams[], &LEAGUE_SIZE, &filename ) ;
	
	stringBuild(inFile, &oneLine, &oneString, &pos);
    teamStatistics(teams[LEAGUE_SIZE], LeagueStats, &filename);
    teamSort(teams[LEAGUE_SIZE], LeagueStats, &filename);
	

return 0;
}
/********************************************************************
*** FUNCTION <Introduction> ***
*********************************************************************
*** DESCRIPTION : <This function will display the process of program> ***
*** INPUT ARGS : <No arguments> ***
*** OUTPUT ARGS : <No arguments> ***
*** IN/OUT ARGS : <No arguments> ***
*** RETURN : <void return type returning no values> ***
********************************************************************/
void introduction()
{
     cout << "This program will use a data file to process league statistics"
          << " for the Western Collegiate Hockey Association using an array of"
          << " structure variables and display the results sorted into"
          << " alphabetical order.";
}

/********************************************************************
*** FUNCTION <readFile> ***
*********************************************************************
*** DESCRIPTION : <This function will open the file inputed from
                : the user and copy the string inside StringBuild
                : which is being called inside this function.> ***
*** INPUT ARGS : <Line& oneLine, Team teams[LEAGUE_SIZE]> ***
*** OUTPUT ARGS : <> ***
*** IN/OUT ARGS : <stringBuild function, teams> ***
*** RETURN : <return type and return value name> ***
********************************************************************/
char readFile(Team teams[], int& LEAGUE_SIZE, Line& filename ) 
{
    ifstream inFile;
	char ch;
	int pos = 0;
	
	cout << "Please enter the file name";
	cin >> fileName;
	
	inFile.open(filename);
	if(!inFile)
	{
		cerr << "Error on retreving file" << endl
			<< "Please press any key to continue";
		cin.get(ch);
    }
	else
	{
	//strncpy(filename, argv[1], strlen(argv[1]) + 1 ); //verify string length?
	
	while (!inFile.getline(oneLine, LINE_SIZE, '\n').eof() )
	{
		stringBuild(oneLine, oneString, pos);
		strcpy(teams[i].name, oneString);
		
		stringBuild(oneLine, oneString, pos);
		strcpy(teams[i].wins, oneString);
		
		stringBuild(oneLine, oneString, pos);
		strcpy(teams[i].losses, oneString);
		
		stringBuild(oneLine, oneString, pos);
		strcpy(teams[i].ties, oneString);
		i++;
    }//end of while loop
    
    LEAGUE_SIZE = i;
    inFile.close();
    }//end of else statement
	return;
}//end of readFile
/********************************************************************
*** FUNCTION <stringBuild> ***
*********************************************************************
*** DESCRIPTION : <This function will check each position for a character>
                : <and return each string to readFile function> ***
*** INPUT ARGS : <inputFile, oneLine, oneString, pos> ***
*** OUTPUT ARGS : <pos, oneString,> ***
*** IN/OUT ARGS : <pos, oneString> ***
*** RETURN : <void return type returning pos and oneString value> ***
********************************************************************/
void stringBuild(fstream &inputFile, Line &oneLine, Line &oneString, int &pos)
{
	
	int loc = 0;
	
	while(oneLine[pos] == " ")
		oneLine[++pos];
		
	while(oneLine[pos] != " " && oneLine[pos] != '\0')
	{
		oneString[loc] = oneLine[pos];
		pos++;
		loc++;
	}  //while
	
		oneString[loc] == '/0';
		
		pos++;
		
		return(oneString);
}
/********************************************************************
*** FUNCTION <name of function> ***
*********************************************************************
*** DESCRIPTION : <detailed english description of the function> ***
*** INPUT ARGS : <list of all input argument names> ***
*** OUTPUT ARGS : <list of all output argument names> ***
*** IN/OUT ARGS : <list of all input/output argument names> ***
*** RETURN : <return type and return value name> ***
********************************************************************/
void teamStatistics(Teams teams[LEAGUE_SIZE], LeagueTotals LeagueStats, Line &filename)
{
	int totalWins = 0;
	int totalLosses = 0;
	int totalTies = 0;
	int totalPoints = 0;
	int totalPPG = 0;
//for(int team =0; team < LEAGUE_SIZE; ++team)
//	{
		wins * 2 = points;
		ties += points;
		pointsPerGame = points / numGames;
//	}
}
/********************************************************************
*** FUNCTION <name of function> ***
*********************************************************************
*** DESCRIPTION : <detailed english description of the function> ***
*** INPUT ARGS : <list of all input argument names> ***
*** OUTPUT ARGS : <list of all output argument names> ***
*** IN/OUT ARGS : <list of all input/output argument names> ***
*** RETURN : <return type and return value name> ***
********************************************************************/
void teamSort(Team teams[LEAGUE_SIZE], LeagueTotals LeagueStats)
{
bool swap;
int temp;

do
{
	swap = false;
	for(int count = 0; count < (size -1); count++)
	{
		if(array[count] > array[count + 1]
		{
			temp = array[count];
			array[count] = array[count + 1];
			array[count + 1] = temp;
			swap = true;
		}//ends if statement
	}//ends for loop
}while(swap);
}//ends TeamSort

[code]

Last edited on
Topic archived. No new replies allowed.