Help with my code please!

Create a new .cpp program that reads the data from this file using a while-loop. Call your program yourname_count_play.cpp, and save it to your folder. Your program will use a counting loop to (a) determine what type of player a data row indicates, and (b) determine the average weight of those player types and all the players listed in the text file.

As the program reads the data from the file, it must determine if the player is a lineman, a back, or other. (You will use an if...else if...else block to do this.) To determine the type of player a data entry represents, assume that all numbers between 1 and 49 are backs, between 50 and 79 are linemen, and between 80 and 99 are other types. Use counters to keep track of the number of each type of player (using the ++ operator), and use other variables to determine the total weight of each type of player (use the += compound operator). When you reach the end of the file, stop the while loop (use a counting option for this program, and stop after reading the 20 lines in the file). When all the data has been read, calculate the average weight of each group of player types, and then the average weight of the entire team. Print the results to both the screen and to a file named yourname_avg_wt.txt, with appropriate labels.

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
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
int main()
{
    int item_number;
    int counter = 0;
    int i = 0;
    int maxPlayers = 20;
    int lineman = 0;
    int back = 0;
    int other = 0;
    int averageWeight;

    
    double amount, unit_price, total_cost, total_sales;
    ifstream datain ( "footballstats.txt");
    
    if (counter <= 49)
    back ++;

   
	 else(counter >= 50 < 79)
    lineback ++;

    if (counter >= 80 && =< 99)
    other ++;
    

system("pause");
return 0;
}


I'm completely lost.

Text from the text file: http://pastebin.com/DaFfWaaV
Last edited on
First, you'll need something to read the information into.

1
2
3
4
5
int identifier = 0;

...

datain >> identifier;

The line datain >> identifier; would retrieve the next integer value from the file (ignoring whitespace)

There are a few ways to do this in a loop. Some tutorials will tell you to use while(!datain.eof()), but many on here will advise against that. I typically use while(datain >> identifier), which will extract it from the file and then enter the loop. As long as it can successfully extract from the file, the condition will evaluate to TRUE. When it can no longer extract from the file, the condition evaluates to false and the loop no longer executes. This assumes the file is formatted properly.
Last edited on
Your results should be;

 9 backs @ 193
 9 lines   @ 253
 2 other  @ 207

As you got your conditionals pretty close, this is what I used.

1
2
3
4
5
6
	if ( type < 50 )
	    index = 0;
	else if ( type > 79 )
	    index = 2;
	else
	    index = 1;


Then I do something like groupWeight [index] += weight;.

What you need to do is;

1: Open data file
2: Was that operation successful
3: Read in type & weight until end of file.
4: Update your totals based on those two amounts.
5: close file
6: Display totals and average for player groups.

1
2
3
4
5
6
7
8
9
10
11
12
Data.open ("Stats.txt");
	
if ( Data.is_open () ) {
    // Initialize some variables here
    while ( !Data.eof () ) {
        // Read file and do computation here
	}		
    Data.close ();
	// Display results
    }
    else
        cout << "Data Failure" << endl;


You won't have anymore questions with this much help.
I kept working with it for the past 2 hours and this is what I have come up with.

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
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
int main()
{
    int item_number;
    int counter = 0;
    int i = 0;
    int maxPlayers = 20;
    int lineback = 0;
    int back = 0;
    int other = 0;
    int averageWeight;
	int lineback_avg, back_avg, other_avg;
	int x;
    
    ifstream datain ( "footballstats.txt");
    
 
	while(datain >> counter >> x)
{
	datain >> counter;
	
	
	if (counter <= 49) 
	{
	back ++;
	averageWeight += x;
  	back_avg += x;
	}

	else if (counter >= 50 && counter <= 79)
	{
	lineback ++;
	averageWeight += x;
  	lineback_avg += x;
    }

	else if (counter >= 80 && counter <= 99)
	{
	other ++;
	averageWeight += x;
  	other_avg += x;
    }
    


}	
    back_avg = back_avg/back;
    lineback_avg = lineback_avg/lineback;
    other_avg = other_avg/other;
    averageWeight = averageWeight/maxPlayers;
    
    ofstream dataout ("yourname_avg_wt.txt");
    system("pause");
	return 0;
}
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
	while(datain >> counter >> x)
{
	datain >> counter;
	
	
	if (counter <= 49) 
	{
	back ++;
	averageWeight += x;
  	back_avg += x;
	}

	else if (counter >= 50 && counter <= 79)
	{
	lineback ++;
	averageWeight += x;
  	lineback_avg += x;
    }

	else if (counter >= 80 && counter <= 99)
	{
	other ++;
	averageWeight += x;
  	other_avg += x;
    }
    


}	
   


You don't need to read it in again, since you did that earlier in while(datain >> counter >> x). At the end of that, both "counter" and "x" will have the updated value. You can use them normally. Whenever you loop finishes, those values will be read in and updated. Try printing the values to the screen at the beginning of the loop and you should see that they updated.

Using "eof" is generally not considered best practice, but will suffice for what you are doing.
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
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
int main()
{
    int item_number;
    int counter = 0;
    int i = 0;
    int maxPlayers = 20;
    int lineback = 0;
    int back = 0;
    int other = 0;
    int averageWeight;
	int lineback_avg, back_avg, other_avg;
	int x;
	int category;
	int amount;
    
    ifstream datain ( "footballstats.txt");
	while(datain >> counter >> x)
{	
	if (counter <= 49) 
	{
	back ++;
	averageWeight += x;
  	back_avg += x;
	}

	else if (counter >= 50 && counter <= 79)
	{
	lineback ++;
	averageWeight += x;
  	lineback_avg += x;
    }

	else if (counter >= 80 && counter <= 99)
	{
	other ++;
	averageWeight += x;
  	other_avg += x;
    }

}	
    back_avg = back_avg/back;
    lineback_avg = lineback_avg/lineback;
    other_avg = other_avg/other;
    averageWeight = averageWeight/maxPlayers;
    
    cout<<setw(10) <<"Back Average"<< setw(10) <<back_avg<<endl;
    cout<<setw(10) <<"Lineback Average"<<setw(10)<<lineback_avg<<endl;
    cout<<setw(10) <<"Others Average"<<setw(10)<<other_avg<<endl;
    cout<<setw(10) <<"Average Wieght"<<setw(10)<<averageWeight<<endl;
    
    
    ofstream dataout ("yourname_avg_wt.txt");
    system("pause");
	return 0;
}


I have too many int ( ) , anyway to shorten them up? Also I keep getting the wrong average for "Back average."
Too many integers? For computers these days, that usually isn't a problem. We could declare 1000000 integers and still be fine (but don't do that unless you have to).

As to the reason why you're getting the wrong average, your issue could be here:

int lineback_avg, back_avg, other_avg;

You should get in the habit of initializing your variables.

Try this:

int lineback_avg = 0, back_avg = 0, other_avg = 0;

When you declare a variable, it is put into memory, but the memory isn't cleared - it will have whatever value it had previously, which could be from another program. By initializing your variable, you are clearing the memory to make sure your variable doesn't get the value of anything but what you intend it to.

The compiler I use (Visual Studio) will actually fail to compile if you don't initialize your variables before you use them. Try to get in the habit of doing that.

It doesn't look like you're using item_number, i, category, or amount, so you could remove those from your code if you want to use less integers.
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
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;

int main() {

	int counter = 0;
	int maxPlayers = 20;
	int lineback = 0;
	int back = 0;
	int other = 0;
	int averageWeight=0;
	int lineback_avg = 0;
	int back_avg = 0;
	int other_avg =  0;
	int x;

 	ifstream datain("footballstats.txt");


 	while ((datain >> counter >> x) && x <= 20)
  {
 	if (counter <= 49)
  {
   		back++;
   		averageWeight += x;
  		back_avg += x;
  }

   else if (counter >= 50 && counter <= 79)
  {
  		lineback++;
   		averageWeight += x;
 		lineback_avg += x;
  }
	else if (counter >= 80 && counter <= 99)
  {
		other++;
		averageWeight += x;
		other_avg += x;
  }

  x++;

 }
 datain.close();
 back_avg = back_avg / back;
 lineback_avg = lineback_avg / lineback;
 other_avg = other_avg / other;
 averageWeight = averageWeight / maxPlayers;
 cout << setw(10) << "Category" << setw(10) << "Average" << endl;
 cout << setw(10) << "Back" << setw(10) << back_avg << endl;
 cout << setw(10) << "Lineback" << setw(10) << lineback_avg << endl;
 cout << setw(10) << "Other" << setw(10) << other_avg << endl;
 cout << setw(10) << "All" << setw(10) << averageWeight << endl;
 ofstream out("output.txt"); //change "output.txt" to whatever the text file is called
 if (out.is_open())
 {
  out<< setw(10) << "Category" << setw(10) << "Average" << endl;
  out<< setw(10) << "Back" << setw(10) << back_avg << endl;
  out << setw(10) << "Lineback" << setw(10) << lineback_avg << endl;
  out << setw(10) << "Other" << setw(10) << other_avg << endl;
  out << setw(10) << "All" << setw(10) << averageWeight << endl;
 }
 out.close();
 system ("PAUSE");
 return 0;
 
}


My code crashes everytime I go to cmd. I'm not sure how to put columns.
Took me a couple of seconds to spot the error this time, but without using GDB, I might not even have caught it. What you should do, is design your application top down. What that simply means is start with something like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
#include<fstream>
#include<iomanip>

using namespace std;

int main() {
	fstream Data;	
	
	cout << "\n\n\tFootball statistics" << endl;
	Data.open ("footballstats.txt");
	
	if ( Data.is_open ) {
		cout << "\tProcessing" << endl;
		
		Data.close ();
	}
	else
		cout << "\tFailed to open text file" << endl;
	
	return 0;
}

What you should see is;

	Football statistics
	Processing
or

	Football statistics
	Failed to open text file
if the file does not exist. In the second case I changed the name to ball.txt.

Next thing we can do is add this @ line 15
1
2
3
4
5
6
		while ( !Data.eof () ) {
			int PlayerType, Weight;
			
			Data >> PlayerType >> Weight;
			cout << " -> " << PlayerType << ", " << Weight << endl;
		}

and the resulting output is this.


	Football statistics
	Processing
 -> 25, 195
 -> 35, 220
 -> 81, 190
 -> 57, 275
 -> 37, 185
 -> 98, 225
 -> 67, 270
 -> 53, 215
 -> 47, 215
 -> 72, 270
 -> 58, 220
 -> 23, 185
 -> 24, 190
 -> 55, 215
 -> 5, 180
 -> 61, 270
 -> 18, 185
 -> 78, 270
 -> 51, 275
 -> 12, 190

Now we know our program is reading the data properly into PlayerType & Weight. If you like, comment out line 5 of previous example and then continue developing inside that while loop.

One thing I do, is after 3 levels of nesting I'll start building functions, that way it is a lot easier to read program.

Look at line 22 of your example. Do you see the mistake with "x".
Topic archived. No new replies allowed.