Minesweeper

Hello everyone i am in the steps of making a minesweeper game but first i got to do this program:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
#include<string>

using namespace std;
int main()
{int i=0;
 char field[4][4];
 do{cin>>field[0][i];
    i++;}
 while(i<=4);
 cout<<field[0][1];
    system("PAUSE");
    return 0;}

It is supposed to read the first line of a 2d array([4][4]) and display its first elementh. The compilation is ok so i think there is a logical problem. after i enter the input it stucks anything i do it just goes on without displaying output or closing. Do you think you can help?
Thanks in advance!
The first element is not field[0][1], it's field[0][0].
Aside from that, your input loop tries to write to field[0][4].
i am sorry but i don't understand your second observation. Can you explain it or maybe rephrase it?( i must admit that i am an absolute begginer)
When you declare the char field[4][4] you are declaring a 4x4 array. Since arrays count from 0 and not 1, that means the highest index will be 3, but your while loop will be true when i=4.
on line 10 change it to a < operator instead of <=. Also you are only looping for the second column not the first for that you need to do a loop inside of a loop.

also since you have a known game size you should make that a constant value and use a for loop since the amount of times to loop is known.

1
2
3
4
5
6
7
8
9
10
const int WIDTH = 4 , HEIGHT = 4;
char field[WIDTH][HEIGHT];

for( int i = 0; i < WIDTH; ++i )
{
    for( int j = 0; j < HEIGHT; ++j )
    {
        cin >> field[i][j];
    }
}


Hope this helps let me know if you don't understand.
Ok, i now understand. Thank you very much for your help!
Topic archived. No new replies allowed.