A bug I cannot fix. Input value differs from output value. Please help!

Hey. I'm trying to answer a test practice question and I am not able to find a fix to a bug for hours now. I enter 16 values into the program but the first row and starting from the second and down to the very last of the row, the numbers are changed into the last row first three numbers. Try out the code and it will give you a better understanding of what I am trying to say. I want the output to be the same as the input, but it is not.

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
#include <iostream>
#include <string>
#include <sstream>
#include <conio.h>
using namespace std;

int numbers[3][3];

int main () {
	string in;
	int count = 0;
	for (int i = 0; i <= 3; i++) {
		cout << "[" << i << "]" << "[" << count << "] ";
		getline(cin, in);
		stringstream(in) >> numbers[i][count];
		if (i == 3) {
			cout << "\n";
			count += 1;
			if (count <= 3) {
				i = -1;
			}
		}
	}
	cout << "\n";
	count = 0;
	for (int i = 0; i <= 3; i++) {
		cout << "[" << i << "]" << "[" << count << "] = " << numbers[i][count] << "\n";
		if (i == 3) {
			cout << "\n";
			count += 1;
			if (count <= 3) {
				i = -1;
			}
		}
	}
	cout << "\n";
	system("pause");
	return 0;
}
Last edited on
You cannot access the fourth element of a 3-element array. Your array is too small on both axes, and you are running off the edge. Change line 7 to int numbers[4][4];
or I think you can change
 
for (int i = 0; i <= 3; ++i)

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

it also appears to me that you know
(++variable-name) is an increment
I wonder why you use
count += 1
...
Last edited on
Thank you! I changed to int numbers[4][4]; and ++count; I thought arrays counted from 0 which they do but [] is suppose to contain # of elements which I did not know. And I used count += 1 out of habit.
Last edited on
Ripper53 wrote:

And I used count += 1 out of habit.


I see...
Topic archived. No new replies allowed.