backyard bowling problem

Hello everyone! this is my first semester in a c++ class and our teacher is having us write our own code. so for mine i decided to write a program to keep score of a backyard bowling game. We are using microsoft visual studio. so far the problems i am having is setting the top line of my array to the frame 1-5. then underneath i am collecting user input for the array. such as..
1 2 3 4 5
5 10 7 8 3
1. when i run the program, it does not output my 2d array correctly.
2. the program will not return the correct values to the array after user input.
we have to use functions and arrays. i hope that is enough information for some help. thanks everyone!

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
  #include <iostream> // input and output
#include <iomanip> //set percision
#include <string> // allows strings
#include <fstream> // allows use of files to read and write
using namespace std;

const int ROWS = 2;
const int COLS = 5;


void Framebuild(int[][COLS], int, int);
int GetPlayerScore(int[][COLS], int, int, int, int, int);
void printScore(int[][COLS], int);





int main()
{
	int scoreboard[ROWS][COLS];
	int frameScore = 0;
	int shot1 = 0;
	int shot2 = 0;
	int frame = 1;


	cout << "            BACKYARD BOWLING" << endl;
	cout << " RULES: 5 frames per game" << endl;
	cout << "        Max Score of 50 points" << endl;
	cout << "        2 Shots per frame if all 10 pins not knocked down" << endl;
	Framebuild(scoreboard, ROWS, frame);
	int playscore = GetPlayerScore(scoreboard, ROWS, frameScore, shot1, shot2, frame);
	printScore(scoreboard, ROWS);











	system("pause");
	return 0;


}


void Framebuild(int scoreboard[][COLS], int ROWS, int frame)
{

		for (int i = 0; i < ROWS; i++)
		{
			for (int j = 0; j < COLS; j++)
			{
				scoreboard[i][j] = 0;
			}
		}
	
}

int GetPlayerScore(int scoreboard[][COLS], int ROWS, int frameScore, int shot1, int shot2, int frame)
{	
		for (int i = 1; i < 2; i++)
		{
			for (int j = 0; j < COLS; j++)
			{
				while (frame < 6)
				{
					cout << "Enter Pins knocked down FRAME " << frame << ">";
					cin >> shot1;
					if (shot1 == 10)
					{

					}
					else
					{
						cout << "Enter Pins knocked down on second shot FRAME " << frame << ">";
						cin >> shot2;
					}
					frameScore = shot1 + shot2;
					scoreboard[i][j] = frameScore;
					frame = frame + 1;
				}
				return scoreboard[1][j];
			}
		}
}

void printScore(int scoreboard[][COLS], int ROWS)
{
	for (int i = 0; i < ROWS; i++)
	{
		for (int j = 0; j < COLS; j++)
		{
			cout << scoreboard[i][j] << " ";
		}
	}
	cout << endl;
}
Line 66: Why are framescore, shot1, shot2, frame being passed as arguments by value? They're initialized to 0 in main and passed by value. The variables in main are never updated. Shouldn't these be local variables, although you probably want to pass frame by reference.

Line 68: What's the purpose of this loop? It's only going to execute once. You might as well not have it. Did you mean to iterate through ROWS?

Line 70: Why the for loop here on j? Isn't j the same as the frame number?

Line 89: You're going to return unconditionally after the first iteration through the loop at line 70.

Line 33: You have no loop here to iterate through the frames.

Here's a simpler version of GetPlayerScore() that gets one score at a time.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int GetPlayerScore(int scoreboard[][COLS], int player, int frame)
{	int shot1, shot2 = 0, framescore;

    cout << "Enter Pins knocked down by player " << player << " in FRAME " << frame << ">";
	cin >> shot1;
	if (shot1 == 10)
	{}
	else
	{   cout << "Enter Pins knocked down on second shot FRAME " << frame << ">";
		cin >> shot2;
	}
	frameScore = shot1 + shot2;
	scoreboard[player][frame] = frameScore;
	return scoreboard[player][frame];	
}


Last edited on
Thanks for the reply!
i made changes to the program so it now saves the values but whenever i tried to mess around with line 66 i always received an error about the terms needing values. in the example you sent me, you have a player selection but my program doesnt ask for that. i added a loop to the main and it now saves the values correctly.
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
#include <iostream> // input and output
#include <iomanip> //set percision
#include <string> // allows strings
#include <fstream> // allows use of files to read and write
using namespace std;

const int ROWS = 2;
const int COLS = 5;


void Framebuild(int[][COLS], int, int);
int GetPlayerScore(int[][COLS], int, int, int, int, int);
void printScore(int[][COLS], int);





int main()
{
	int scoreboard[ROWS][COLS];
	int frameScore = 0;
	int shot1 = 0;
	int shot2 = 0;
	int frame = 1;


	cout << "            BACKYARD BOWLING" << endl;
	cout << " RULES: 5 frames per game" << endl;
	cout << "        Max Score of 50 points" << endl;
	cout << "        2 Shots per frame if all 10 pins not knocked down" << endl;
	Framebuild(scoreboard, ROWS, frame);
		for (int y = 0; y < COLS; y++)
		{
			int playscore = GetPlayerScore(scoreboard, ROWS, frameScore, shot1, shot2, frame);
			scoreboard[1][y] = playscore;
			frame++;
		}
	
	printScore(scoreboard, ROWS);











	system("pause");
	return 0;


}


void Framebuild(int scoreboard[][COLS], int ROWS, int frame)
{

	for (int j = 0; j < COLS; j++)
	{
		scoreboard[0][j] = frame;
		frame++;
	}
	
}

int GetPlayerScore(int scoreboard[][COLS], int ROWS, int frameScore, int shot1, int shot2, int frame)
{	
	{	
		cout << "Enter Pins knocked down FRAME " << frame << ">";
		cin >> shot1;
		if (shot1 == 10)
		{}
		else
		{
			cout << "Enter Pins knocked down on second shot FRAME " << frame << ">";
			cin >> shot2;
		}
		frameScore = shot1 + shot2;								
		return frameScore;
	}
		
}

void printScore(int scoreboard[][COLS], int ROWS)
{
	for (int i = 0; i < ROWS; i++)
	{
		for (int j = 0; j < COLS; j++)
		{
			cout << right << setw(3) << scoreboard[i][j] << "";
		}
              cout << endl;
	}
}
Last edited on
and now i'm lost. i forgot i had to write the values of the array to a file. i looked at some examples for passing files from functions to an array but, im getting an error when i call my framebuild function. it wont let me use the outputfile in it. any help?
thank you.
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
#include <iostream> // input and output
#include <iomanip> //set percision
#include <string> // allows strings
#include <fstream> // allows use of files to read and write
using namespace std;

const int ROWS = 2;
const int COLS = 5;


void Framebuild(int[][COLS], int, int, ofstream);
int GetPlayerScore(int[][COLS], int, int, int, int, int);
void printScore(int[][COLS], int);
int totalscore(int[][COLS]);




int main()
{
	ofstream outputFile;
	outputFile.open("bowling.txt");


	int scoreboard[ROWS][COLS];
	int frameScore = 0;
	int shot1 = 0;
	int shot2 = 0;
	int frame = 1;
	string playname;


	cout << "            BACKYARD BOWLING" << endl;
	cout << " RULES: 5 frames per game" << endl;
	cout << "        Max Score of 50 points" << endl;
	cout << "        2 Shots per frame if all 10 pins not knocked down" << endl;

	cout << "Please enter Players Name: ";
	cin >> playname;
	cout << endl;
	Framebuild(scoreboard, ROWS, frame, outputFile);
		for (int y = 0; y < COLS; y++)
		{
			int playscore = GetPlayerScore(scoreboard, ROWS, frameScore, shot1, shot2, frame);
			scoreboard[1][y] = playscore;
			outputFile << scoreboard[1][y] << " ";
			frame++;
		}
	printScore(scoreboard, ROWS);
	int thescore = totalscore(scoreboard);
	cout << "Thanks for playing " << playname << "! Your final score was " << thescore << " out of 50!" << endl;









	system("pause");
	return 0;


}


void Framebuild(int scoreboard[][COLS], int ROWS, int frame, ofstream outputFile)
{

	for (int j = 0; j < COLS; j++)
	{
		scoreboard[0][j] = frame;
		outputFile << scoreboard[0][j] << " ";
		frame++;
	}
	
}

int GetPlayerScore(int scoreboard[][COLS], int ROWS, int frameScore, int shot1, int shot2, int frame)
{	
	{	
		cout << "Enter Pins knocked down FRAME " << frame << ">";
		cin >> shot1;
		if (shot1 == 10)
		{}
		else
		{
			cout << "Enter Pins knocked down on second shot FRAME " << frame << ">";
			cin >> shot2;
		}
		frameScore = shot1 + shot2;								
		return frameScore;
	}
		
}

void printScore(int scoreboard[][COLS], int ROWS)
{
	for (int i = 0; i < ROWS; i++)
	{
		for (int j = 0; j < COLS; j++)
		{
			cout << right << setw(3) << scoreboard[i][j] << "";
		}
		cout << endl;
	}
}

int totalscore(int scoreboard[][COLS])
{
	int totscore = 0;
	for (int j = 0; j < COLS; j++)
	{
		totscore = totscore + scoreboard[1][j];
	}
	return totscore;
}
Is your program supposed to support more than one player, or is only intended for a single player? If for a single player, why is ROWS 2?

Lines 27-28: Why are shot1 and shot2 defined here? They're not used in main. As I said before, they should be local variables within GetPlayerScore(). You need to remove them from both line 12 and line 60.

Line 45: You're storing the score into row[1]. I'm not understanding why you have 2 rows.

Line 73: You're only initializing a single row of scoreboard, which by the way you initializing to all 1's (frame).


Topic archived. No new replies allowed.