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.
#include<iostream>
#include<fstream>
#include<iomanip>
usingnamespace 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;
}
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.
As you got your conditionals pretty close, this is what I used.
1 2 3 4 5 6
if ( type < 50 )
index = 0;
elseif ( 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.
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.
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.
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.
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".