New to c++, program issue

Trouble getting my program to display the tournament winner with the winner's average score. My instructor told me I was calculating the average AFTER I compare it to the highest so far. I tried moving around the calculation for average score/avgScore and the if statement determining the highest average and the tournament winner. Can someone tell me where to place my statements?
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
  
// Program 6

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main() {

	double avgScore, totalAvg, highestAvg, playerScore, overallAvg ;
	int totalGames, perfectGames, winner ;
	int totalScore ;  // sumof one players score
	const int fw = 20;
	ifstream prog6bowlinginfo ;
	int playerid, numberOfGames;
	//Full path of the file
	//prog6bowlinginfo.open("C:\\Temp\\pgm6Bowl.txt");
	
	prog6bowlinginfo.open("pgm6Bowl.txt");
	prog6bowlinginfo >> playerid;
	// print headings 
	cout << "BG Bowling Tournament Report \n" << "Report prepared by: Patrick \n \n" ;
	cout << "Number" << setw(fw) << "Games" << setw(fw) << "Average \n" ;
	cout << "------------------------------------------------------- \n"; 
	totalGames = 0;
	perfectGames = 0;
	avgScore = 0;
	highestAvg = 0;
	winner = 0;
	totalAvg = 0;
	while (playerid != -1){
		//cout << playerid << endl;
		prog6bowlinginfo >> numberOfGames;
		// add to totalgames
		totalScore = 0;
		for (int g=1; g <= numberOfGames; g++) {
			prog6bowlinginfo >> playerScore;
			//cout <<playerScore <<endl;
			totalScore += playerScore;
			
			if (playerScore == 300)
				perfectGames += 1;
			avgScore = (double)totalScore / numberOfGames;
		}
	totalGames += numberOfGames;
	totalAvg += avgScore ;
	cout << setprecision(2) << fixed ;
	//avgScore = (double)totalScore / numberOfGames;
	cout << playerid << setw(fw) << numberOfGames << setw(fw) << avgScore << "\n" ;
	if (avgScore > avgScore){
				highestAvg = avgScore;
			    winner = playerid;
	}
	overallAvg = totalAvg / 8;
	prog6bowlinginfo >> playerid; // getnext id

	}
	cout << "Total Games " << totalGames << setw(fw+10) << " Overall Average " << overallAvg << "\n" ;
	cout << "Tournament winner " << winner << setw(fw) << " with average " << highestAvg << "\n";
	cout << "Total Perfect games " << perfectGames << "\n" ;
	prog6bowlinginfo.close();
	return 0;
}
1
2
if (avgScore > avgScore){
...

That if statement will always be false, you're comparing something to itself. Maybe you meant to compare
if (avgScore > highestAvg)? I would suggest breaking the program up into specific functions, it makes it much easier for someone else to see what's going on.
Last edited on
Ok thanks, I got it to show something other then 0. However, now its simply displaying the last score as the highest avg and the winner as -1. (The winner should be a number like 2107 or 1193 - the playerid and the highest average should be 223.60)
Topic archived. No new replies allowed.