Bowling

Hey, I'm trying to make a bowling scorer and I can't figure out what's going wrong. I am inputting from a file containing these numbers:

9 0
3 7
6 1
3 7
8 1
5 5
0 10
8 0
7 3
8 2 8


and I am supposed to output something like this:

Welcome to Bowling Scorer!
Please enter the name of a score file: example2.txt
9-0 3-7 6-1 3-7 8-1 5-5 0-10 8-0 7-3 8-2-8
9 25 32 50 59 69 87 95 113 131


The first three lines are fine but the last line is outputting:
17 27 34 44 53 63 73 81 91 101


As you can see, the math here is all correct, except it is somehow getting 17 from 9-0.

If you can help me out that would be great! Here is the code:

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
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

const int NUMSCORES=21;
const int NUMFRAMES=10;
int score[NUMSCORES];
ifstream scores;
void scorer(int score[NUMSCORES], int NUMSCORES);
void ScoreGame(int score[], int NUMSCORES);

int main()
{
	char filename[20];
	
	cout << "Welcome to Bowling Scorer!" << endl
		<< "Please enter the name of a score file: ";
	cin >> filename;
	scores.open(filename);
	if(scores.fail()){
		perror(filename);
		exit(1);
	}
	scorer(score, NUMSCORES);
	cout << endl;
	ScoreGame(score, NUMSCORES);
	return 0;
}

void scorer(int score[], int NUMSCORES){
	int i=0;
	for(i=0;i<NUMSCORES-1;i++){
		scores >> score[i];
		if(i%2==0){
			cout << score[i] << "-";
		}
		else{
			cout << score[i];
			if(i<NUMSCORES-2) cout << "\t";
		}
	}
	cout << "-" << score[i];
}

void ScoreGame(int score[], int NUMSCORES){
	int j=0, comp=score[j];
	for(j=0;j<NUMSCORES-1;j++){
		scores >> score[j];
		comp+=score[j];
		if(j%2!=0)
		cout << comp << "\t";
	}
}
1) It doesn't look to me like you are reading enough into your score array in 'scorer':
for(i=0;i<NUMSCORES-1;i++){ <- why the -1? Won't that make you drop the last score?

2) You already read the score from the file in 'scorer', then read them from the file AGAIN in ScoreGame. This combined with problem #1 is probably resulting in your score array getting corrupted.

3) You aren't calculating the bonus for strikes or spares (though your sample data has no strikes... but it does have a few spares)


#1 and 2 are easily solved by moving the file reading code OUT of scorer and ScoreGame and putting it in its own function.

#3 requires you to rethink how you're calculating the score.
Last edited on
Well on #3, I'm not required to factor in strikes or spares.

And if I take off the -1 in scorer I don't get the output I want. Scorer is outputting correctly as it is.

And for #2 I'm just following the directions. I was told to iterate through score in both functions.
And I'm not sure what you mean about moving the file reading code out of scorer and ScoreGame and putting it in its own function. I'm sure its simple but I'm new at this and I can barely grasp what I'm doing. Do you think you could copy my code and make the necessary changes to it?
Topic archived. No new replies allowed.