2D Array Puzzle

Good evening,

I am working on my final project for my C++ class and have run into a snag with my program. There seems to be a problem with my array and const int but I'm not sure why.

Here is the full program:

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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//Jeremy Johnson
//May 1, 2019
//Final Project

#include "pch.h"
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>

using namespace std;

const int COLS = 3;
const int PLAYERS_COL = 3;

void showChart(string[], int[][COLS]);
void showPlayersTotal(int[][COLS]);
void showTetrisScore(string[], int[][COLS]);
void showPlayerPacman(string[], int[][COLS]);

int main()
{
	int scores, nuPlayers = 3;
	double totalAmount = 0, avgAmount, totalAverage, totalComplex = 0;

	string player;
	ofstream outputFile;
	outputFile.open("players.txt"); //Record input onto text file
	cout << "Enter the number of players:  "; 
	cin >> nuPlayers;

	for (int i = 0; i < 3; i++)
	{

		cout << "\nEnter the player's name: ";
		cin >> player;
		outputFile << player << " ";
		outputFile << endl;
	}
	outputFile.close();

	int scores;
	ofstream outputFile;
	outputFile.open("scores.txt"); 
	
	for (int j = 0; j < scores; j++)
	{
		cout << "\nEnter the score amount: "; 
		cin >> scores;
		outputFile << scores << " ";

	}
	outputFile.close();

	string players[PLAYERS_COL];
	ifstream inputPlayers;

	inputPlayers.open("players.txt");*****
	for (int i = 0; i < PLAYERS_COL; i++)
		inputPlayers >> player[i];
	inputPlayers.close();

	int scores[PLAYERS_COL][COLS];
	ifstream inputScores;

	inputScores.open("scores.txt");
	for (int m = 0; m < COLS; m++)
	{
		for (int c = 0; c < PLAYERS_COL; c++)
		{
			inputScores >> scores[m][c];
		}
	}
	inputScores.close();

	int selection;

		
	do {

		cout << "1. Display data" << endl;
		cout << "2. Display total number of recreation and non-recreational visitors" << endl;
		cout << "3. Display total tent and RV campers by month" << endl;
		cout << "4. Display total of recreational visitors for a certain month" << endl;
		cout << "Enter any other number to exit" << endl;
		cin >> selection;
		cout << endl;

		//Switch for options
		switch (selection)
		{
		case 1:
			showChart(players, scores);
			break;
		case 2:
			showPlayersTotal(scores);
			break;
		case 3:
			showPlayersAverage(players, scores);
			break;
		case 4:
			showPlayerScore(players, scores);
			break;
		default:
			system("pause");
		}


	} while (selection >= 1 && selection <= 4);

	system("pause");
	return 0;
}

	//To show the data
	void showChart(string scores[], int players[][COLS])
	{

		cout << left << setw(10) << "Player" << right << setw(12) << "Pac-Man" << setw(12) << "Galaga" << setw(12) << "Tetris" << endl;
		for (int m = 0; m < PLAYERS_COL; m++)
		{
			cout << left << setw(10) << players[m] << right;
			for (int c = 0; c < COLS; c++)
			{
				cout << setw(12) << scores[m][c];
			}
			cout << endl;
		}
		cout << endl;
	}

	
	void showPlayersTotal(int scores[][COLS])
	{
		int totalScores = 0;
		for (int m = 0; m < PLAYERS_COL; m++)
			totalScores += scores[m][0] + scores[m][1];

		cout << "The total amount for all the scores combined is: " << totalScores << endl << endl;
	}

	
	void showTetrisScore(string players[], int scores[][COLS])
	{
		int total;
		cout << "Total Tetris Score" << endl;
		for (int m = 0; m < PLAYERS_COL; m++)
		{
			total = scores[m][3];
			cout << setw(12) << players[m] << setw(12) << total << endl;
		}
		cout << endl;
	}

	
	void showPlayerPacman(string players[], int scores[][COLS])
	{
		string search;
		cout << "Enter the player you want the total Pac-Man score: ";
		cin >> search;
		int index = -1;

		for (int m = 0; m < PLAYERS_COL && index == -1; m++)
		{
			if (players[m] == search)
				index = m;
		}

		if (index == -1)
		{
			cout << search << " is not a valid month" << endl;
		}
		else
		{
			cout << "For " << players[index] << " they scored " << scores[index][0] << " " << endl;
		}
		cout << endl;

	}



And here is the section my error starts:

1
2
3
4
5
6
7
8
9
inputScores.open("scores.txt");
	for (int m = 0; m < COLS; m++)
	{
		for (int c = 0; c < PLAYERS_COL; c++)
		{
			inputScores >> scores[m][c];
		}
	}
	inputScores.close();



Advice would be appreciated.
Last edited on
First bit of advice, format your code for readability.
http://www.cplusplus.com/articles/jEywvCM9/
Also I suggest you submit a stripped down version of the program. Often when
making a condensed version, you will discover the cause of the problem.


int[][COLS];


What's the deal with this statement ?
You have not specified a size for the first part of the 2 dimensional array ?
Last edited on
There seems to be a problem with my array and const int but I'm not sure why.
Can you be more specific? What is the invalid output? What should it be? What input creates the invalid output?
You appear to be declaring scores as three separate things on lines 23, 42 and 63
Lines 23, 42, & 63: You've defined scores 3 times with 2 different types.
Line 46: Scores is used before it's initialized.
Line 99: showPlayersAverage() is not defined.
Topic archived. No new replies allowed.