for loop Array issue.

I'm confused why I'm getting Errors for my for loop

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
 //==========================================================
//
// Title:      Lab01
// Course:     CSC 1101
// Lab Number: 1
// Author:     Kevin Russell
// Date:       1/14/2020
// Description:
//   A C++ console application that calculates total and adverage snowfall
// 
//
//==========================================================
#include <conio.h> // For function getch()
#include <cstdlib> // For several general-purpose functions
#include <fstream> // For file handling
#include <iomanip> // For formatted output
#include <iostream> // For cin, cout, and system
#include <string> // For string data type
using namespace std; // So "std::cout" may be abbreviated to "cout"
const int ArryRow = 2;
const int ArryCol = 5;
void enterScores(int scores[ArryRow][ArryCol],int games)
{
	int WarriorScore;
	int LakersScore;
	cout << "Welcome to Ping Pong Pals" << endl;
	cout << "-----------------------------" << endl << endl;
	for (size_t i = 0; i < games; i++)
	{
		cout << "Game " << i+1 << endl;
		cout << "Enter Warriors score: ";
		cin >> WarriorScore;
		cout << "Enter Lakers score: ";
		cin >> LakersScore;
		while (WarriorScore == LakersScore || (WarriorScore != 11 && LakersScore != 11) || WarriorScore > 11 || WarriorScore < 0 || LakersScore>11 || LakersScore < 0)
		{
			cout << "ERROR: one of the following conditions occurred:" << endl;
			cout << "-The score was tied." << endl;
			cout << "-At least one score was outside the rand 0-11." << endl;
			cout << "-Neither score was 11." << endl << endl;
			cout << "Enter Warriors score: ";
			cin >> WarriorScore;
			cout << "Enter Lakers score: ";
			cin >> LakersScore;
		}
		cout << endl;
		scores[1][i] = { WarriorScore };
		scores[2][i] = { LakersScore };
		
		
	}
	printScores(scores, 6);
}
void printScores(int scores[ArryRow][ArryCol], int games)
{
	
	for (size_t i = 0; i < ArryRow; i++)
	{
		for (size_t j = 0; j<ArryCol; j++)
		{
			cout << scores[i][j] << " ";
		}
		cout << endl;
	}
}
int main()
{
	int scores[ArryRow][ArryCol];
	enterScores(scores, 5);
	
}



I'm confused why I'm getting Errors for my for loop

What exactly are the errors? Compile-time errors? Run-time errors?

The more details you give us the more we are able to help you.
Run-Time Check Failure #2 - Stack around the variable 'scores' was corrupted.

and the output would look like this:

-858993460 -858993460 -858993460 -858993460 -858993460
11 11 11 11 11

sorry for being vague idk what to really give
Last edited on
1
2
		scores[1][i] = { WarriorScore };
		scores[2][i] = { LakersScore };

Array indices start at 0, not 1.
Hello gus2427,

To start with you could use some blank lines in your code.

The first error that came up deals with the end of the "enterScores" function. You are calling the "printScores" function before it has been compiled so the compiler does not know about it yet. You should move the "printScores" function to main.

A function should do only one thing.

After you correct what Ganado has told you next is printScores(scores, 6); at the end of "enterScores". If the array only has room for 5 columns then why are you sending the function 6. In the "print" function you will be trying to read past the end of the array. This may not be a runtime error, but you will be printing garbage because it is outside of the array.

Just to give yo an idea about blank lines:
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
void printScores(int scores[ArryRow][ArryCol], int games)
{
	for (int i = 0; i < ArryRow; i++)
	{
		for (int j = 0; j < ArryCol; j++)
		{
			cout << std::setw(3) << scores[i][j];
		}

		cout << '\n';
	}

	cout << endl;
}

int main()
{
	int scores[ArryRow][ArryCol];

	enterScores(scores, 5);

	printScores(scores, 5);

	return 0;  // <--- Not required, but makes a good break point.
}


One last point. You are sending an "int" for the size of the number of "games", but in the "print" function you never use it nor need it.

In your for loops you define the loop iterator as a "size_t", or "unsigned int", then compare the size_t to an int. this is a type mismatch and should be a compiler warning.The "size_t" should be an "int"

Andy
Good catch, I saw the 5 in the call from main, but not the 6 in the inner call.
@ Ganado,

Thanks. Now and then I get lucky. I caught it when I tried running the program.

Andy

Edit: typo.
Last edited on
Topic archived. No new replies allowed.