Read and input 2D arrays

Hi everyone, I'm a C++ beginner currently making a program which uses a 2D array and I need some help :) I know how to work 1D arrays but struggling with this.

Basically in the code below I want to allocate a1, b1, c1, a2, b2, c2 in a 3x2 array so that it fills in like this:


a1 a2
b1 b2
c1 c2

Likewise with ax1, ay1, bx2, by2 in a 2x2 array as:

ax1 bx2
ay1 by2

I'm stuck after the first prompt output for the a1 value, I've applied the counter but I have no clue what to do next in order to get it to place the input value of a1 into the particular element shown above.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <math.h>
using namespace std;

int main()

{
    double a1,b1, c1, a2, b2, c2, dis[3][2];

    double ax1, ay1, bx2, by2, car[2][2]; 
    
    cout << "Type in the value for a1: " << endl;
    for (int i=0;i<3;i++)


Any help would be much appreciated :)
It's possible with just a couple of for loops. Since I kind of got crapped on last time I just gave the answer away, I'll explain it while I show it to 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
#include<iostream>
#include<cmath>         //or #include<math.h>
using namespace std;
int main()
{
double dis[3][2];
double car[2][2];
cout << "Type in values for dis\n(a1,b1,c1,a2,b2,c2)\n";
for (int i = 0; i < 3; i++)
{
	for (int j = 0; j < 2; j++)
	{
	if (!(cin))
		cin.clear();
	cin >> dis[i][j]
	}
}
cout << "Type in values for car\n(ax1,ay1,bx2,by2)\n";
for (int i = 0; i < 2; i++)
{
	for (int j = 0; j < 2; j++)
	{
	if (!(cin))
		cin.clear();
	cin >> car[i][j];
	}	
}
return 0;
}


That's probably the easiest way to go about doing it...instead of having the user input a1, then prompt again for a2, I prompt for the entire value of dis. So, rather than "Enter a1" then "Enter a2" you simply enter in dis, you'll enter all the values in the correct order. Now for what the loops do...

The first for loop counts the rows of your 2D array, and the nested for loop counts the columns. The nested for loop does the same thing as a regular array does, and in a way, so does the outer loop. When you reach the end of the columns, you go to the next row, and start over. Once you run out of rows, you're finished.

If you insist on having a prompt each time ("Type in a value for a1...type in a value for a2...") there is a way to do that, it's a little more cryptic though...
Thank you for your reply!

Your code has helped, I now understand how to approach the rows/columns as a nested for loop.

However it is quite necessary for my program to have a prompt each time, as each of the different values represent different things. My variables aren't really called a1, a2 etc. I've just simplified it for the sake of clarity on here. I've also never come across this before:
1
2
if (!(cin))
		cin.clear();


Would it be possible for you to show how to do it for each prompt? It would be massively helpful :)

Thanks again!
Last edited on
EDIT:
accidentally put braces around each string

Okay, glad I could help. As for the
1
2
if (!(cin))
cin.clear();

That's just in case the "failbit" of cin is set. It happens when you try to use cin.operator>>() too many times in a row (at least, that's how it ends up getting set when I do it) a solution would be to use cin.get() or cin.getline() instead, but for simplicity I just use cin.operator>>(). The clear() function clears the failbit, so cin can be used again without accidentally being skipped (or, getting called but the user having no chance to input information).

Now, for your "prompt each time" problem, you can have a 2D array of chars hold each string, OR, you can have a 1D array of string objects. I'll do it with an array of strings...

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
#include<iostream>
#include<cstring>       //or #include<string.h> <- don't forget to add this part!
#include<cmath>         //or #include<math.h>
using namespace std;
int main()
{
string Strings[6] = 
{
	"Type in value for a1: ",
	"\nType in value for a2: ",
	"\nType in value for b1: ",
	"\nType in value for b2: ",
	"\nType in value for c1: ",
	"\nType in value for c2: "
};
string Morestrings[4] =
{
	"Type in value for ax1: ",
	"\nType in value for bx2: ",
	"\nType in value for ay2: ",
	"\nType in value for by2: "
};
double dis[3][2];
double car[2][2];

for (int i = 0, k = 0; i < 3; i++)
{
	for (int j = 0; j < 2; j++)
	{
	cout << Strings[k];
	if (!(cin))
		cin.clear();
	cin >> dis[i][j]
	k++;
	}
}

for (int i = 0, k = 0; i < 2; i++)
{
	for (int j = 0; j < 2; j++)
	{
	cout << Morestrings[k];
	if (!(cin))
		cin.clear();
	cin >> car[i][j];
	k++;
	}	
}
return 0;
}


That's the edited code. Don't forget about k, and don't forget to include the string header file!

I use k as an index to which string to output in the loop. k updates in each nested loop (so we get to the next string), but it needs to be created in the outer for loop so it doesn't get reset every time we finish a row. There's no conditional test for k, but if you look closely you'll see you won't need to test it, because the number of strings is equal to the number of rows in both outer for loops, meaning testing i and testing k, would be identical.

I'm a bit uncomfortable with
 
cout << String[k];

because I'm not sure if it will just output the first character of each string in the array, or if it'll output the entire string. Tell me how it goes! If strings aren't being displayed properly, I do have a fix for it, but again, it'll be a bit more cryptic. Hope this helps.
Last edited on
Topic archived. No new replies allowed.