nid sum guide

can u please help me in my codes..
if i choose a col and row it wont add the surrounding mines of it..wats d wrong of my code?..and also can u please change the array from '1'-'0' which '1' is the bomb to '?' and 'x'..and which 'x' is the bomb

please help..my instructor gave me only 1 1/2 days to finish ds..T.T

#include<iostream.h>
#include<conio.h>


void main()



{


clrscr();


{
int bomb[5][5]= {{0,1,0,0,1},
{1,1,0,1,0},
{0,0,1,1,0},
{1,0,0,1,0},
{0,0,1,0,0}};
int bombscan[5][5]= {{-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1}};
int vert,hor,r,c;
int bombc=0;
int mnRw,mnCl,mxRw,mxCl;
int flag=0,check=0;
while(check<=7)
{
clrscr();
for(r=0;r<5;r++)
{
for(c=0;c<5;c++)
{
if(bombscan[r][c]==-1)
cout<<"[ ]";
else if(bombscan[r][c]==-10)
cout<<"[x]";
else
cout<<"["<<bombscan[r][c]<<"]";
}
cout<<endl;
}
if (check==19)
{

cout<<"finish!!";
goto exit;
}
if(flag==1)
{
cout<<"game over!";
goto exit;
}
cout<<"choose cell coordinates:"<<endl;
cout<<"\tvertical:";cin>>vert;
cout<<"\thorizontal:";cin>>hor;
vert=vert-1;
hor=hor-1;


if(bomb[vert][hor]==1)
{
bomb[vert][hor]=-10;
flag=1;
}
else
{
check++;
if(vert==0)
mnRw=0;
else
mnRw=vert-1;
if(hor==0)
mnCl=0;
else
mnCl=hor-1;
if(hor==2)
mxRw=2;
else
mxRw=+1;
if(hor==2)
mxCl=2;
else
mxCl=hor+1;
for(r=mnRw;r<=mxRw;r++)
{
for(c=mnCl;c<=mxCl;c++)
{if(bomb[r][c]==1)
{bombc++;
}
}
}
bombscan[vert][hor]=bombc;
bombc=0;
}
}
exit:
}
getch();
}
Last edited on
There are a lot of things wrong with this code....
wat is it?..
can u make corrections on my code?
i really nid to pass it b4 august 4..T.T
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
#include<iostream.h> // This header is depreciated,
                     //   meaning it's 10 years old, and has since been replaced)
#include<conio.h>    // Not standard, but not big deal in this case.
/* You didn't declare a using directive, yet you are using members from the
 * std namespace with no qualifiers, i.e "cout" instead of "std::cout" */


void main() /* Main should not be a void function,
 * You need to return 0 or EXIT_SUCCESS on a successful exit, with no errors,
 * and return an error status when an error occurs.
 * GNU compiler won't even let you use a void main(),
 * but some (IMO, inferior for this reason) compilers will, which is bad.
 * ISO C++ Standard dictates that it has to be int main(), basically. */

{
    clrscr();
    /* clrscr() is very non-standard. It is specific to only a few compilers,
     * not including GNU, which is what I use.
     * Preferably, you should use a non-OS-specific, non-compiler-specific
     * method to do this ... Actually not doing this at all is best ...
     * But system("cls") is at least portable to different compilers,
     * though is still Windows specific. */

    { // <-- What is this doing here? The start bracket for main() is already in here.
        // This one is redundant and will cause a compile error.
    int bomb[5][5] = { { 0, 1, 0, 0, 1 }, { 1, 1, 0, 1, 0 }, { 0, 0, 1, 1, 0 },
            { 1, 0, 0, 1, 0 }, { 0, 0, 1, 0, 0 } };
    int bombscan[5][5] = { { -1, -1, -1, -1, -1 }, { -1, -1, -1, -1, -1 }, {
            -1, -1, -1, -1, -1 }, { -1, -1, -1, -1, -1 },
            { -1, -1, -1, -1, -1 } }; 
    int vert, hor, r, c, mnRw, mnCl, mxRw, mxCl, bombc = 0, flag = 0, check = 0;
    while (check <= 7) {
        clrscr();
        for (r = 0; r < 5; r++) {
            for (c = 0; c < 5; c++) {
                if (bombscan[r][c] == -1)
                    cout << "[ ]";
                else if (bombscan[r][c] == -10)
                    cout << "[x]";
                else
                    cout << "[" << bombscan[r][c] << "]";
            }
            cout << endl;
        }
        if (check == 19) {

            cout << "finish!!";
            goto exit;
            /* Never, EVER, use a goto like this! This isn't a batch script! 
             * In this case, you can simply use a return statement. */
        }
        if (flag == 1) {
            cout << "game over!";
            goto exit;
        }
        /* You don't need endl to simply print a newline.
         * Use '\n' whenever possible, endl is more resource intensive,
         * and is unnecessary here */
        cout << "Enter cell coordinates:" << endl;
        cout << "\tVertical:"; cin >> vert;
        cout << "\tHorizontal:"; cin >> hor;
        vert = vert - 1;
        hor = hor - 1;

        if (bomb[vert][hor] == 1) {
            bomb[vert][hor] = -10;
            flag = 1;
        } else {
            check++;
            if (vert == 0)
                mnRw = 0;
            else
                mnRw = vert - 1;
            if (hor == 0)
                mnCl = 0;
            else
                mnCl = hor - 1;
            if (hor == 2)
                mxRw = 2;
            else
                mxRw = +1;
            if (hor == 2)
                mxCl = 2;
            else
                mxCl = hor + 1;
            for (r = mnRw; r <= mxRw; r++) {
                for (c = mnCl; c <= mxCl; c++) {
                    if (bomb[r][c] == 1) {
                        bombc++;
                    }
                }
            }
            bombscan[vert][hor] = bombc;
            bombc = 0;
        }
    }
    exit:  
}
getch();
} // <-- This will cause a compile error, as main() ends with the bracket above,
//      and this one is useless.

/* There is also no return statement, i.e. "return 0;", and getch() is outside
 * of main(), and is thus useless, and also will cause a compilation error. 
 */
Last edited on
1
2
} // <-- This will cause a compile error, as main() ends with the bracket above,
//      and this one is useless. 
well, actually, its fine there because we have our friend here
1
2
    { // <-- What is this doing here? The start bracket for main() is already in here.
        // This one is redundant and will cause a compile error. 


these shouldnt be causing compile errors, but i argree that theyre useless, and should both be removed
Last edited on
OK, I tried, but I can't get it to work correctly, I don't think. The problem is I don't know how most of it is supposed to work... I don't know what a lot of the code is supposed to do, so I don't know what to correct it with..

I did correct the things I mentioned in the code above.
Here is the code with those corrections, and a couple others:
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
#include<iostream>
#include<conio.h>
using namespace std;

int vert, hor, r, c, mnRw, mnCl, mxRw, mxCl, bombc, flag, check;
// Note: I think this will initialize them all as 0, but I'm not sure...

int main() {
    int bomb[5][5] = { { 0, 1, 0, 0, 1 }, { 1, 1, 0, 1, 0 }, { 0, 0, 1, 1, 0 },
            { 1, 0, 0, 1, 0 }, { 0, 0, 1, 0, 0 } };
    int bombscan[5][5] = { { -1, -1, -1, -1, -1 }, { -1, -1, -1, -1, -1 }, {
            -1, -1, -1, -1, -1 }, { -1, -1, -1, -1, -1 },
            { -1, -1, -1, -1, -1 } };
    system("cls");

    while (check <= 7) {
        system("cls");
        for (r = 0; r < 5; r++) {
            for (c = 0; c < 5; c++) {
                if (bombscan[r][c] == -1)
                    cout << "[ ]";
                else if (bombscan[r][c] == -10)
                    cout << "[x]";
                else
                    cout << "[" << bombscan[r][c] << "]";
            }
            cout << endl;
        }
        if (check == 19) {

            cout << "finish!!";
            getch();
            return 0;
        }
        if (flag == 1) {
            cout << "game over!";
            getch();
            return 0;
        }
        cout << "Enter cell coordinates ...\n";
        cout << "\tVertical:";
        cin >> vert;
        cout << "\tHorizontal:";
        cin >> hor;
        vert--; // You can use decrement operator here
        hor--; // and here

        if (bomb[vert][hor] == 1) {
            bomb[vert][hor] = -10;
            flag = 1;
        } else {
            check++;
            if (vert == 0)
                mnRw = 0;
            else
                mnRw = vert--;
            if (hor == 0)
                mnCl = 0;
            else
                mnCl = hor--;
            if (hor == 2)
                mxRw = 2;
            else
                mxRw = +1;
            if (hor == 2)
                mxCl = 2;
            else
                mxCl = hor++;
            for (r = mnRw; r <= mxRw; r++) {
                for (c = mnCl; c <= mxCl; c++) {
                    if (bomb[r][c] == 1) {
                        bombc++;
                    }
                }
            }
            bombscan[vert][hor] = bombc;
            bombc = 0;
        }
    }
    getch();
    return 0;
}
Last edited on
Skillless wrote:
1
2
} // <-- This will cause a compile error, as main() ends with the bracket above,
//      and this one is useless.  

well, actually, its fine there because we have our friend here
1
2
    { // <-- What is this doing here? The start bracket for main() is already in here.
        // This one is redundant and will cause a compile error.  

these shouldnt be causing compile errors, but i argree that theyre useless, and should both be removed


That's what I thought at first, but my IDE, Eclipse, was telling me there were errors on those brackets, and were gone when I removed them (also, if you'll notice, it didn't indent the second to last } or getch()... maybe the goto: threw it off.) ... But I know you can enclose any set of statements like that in {} and it's basically harmless, though pointless.
Last edited on
Maybe a bit off topic, but could someone please explain how you could make a return statement for when an error occurs?

And when do these errors occur?

RyanCaywood wrote
1
2
/** You need to return 0 or EXIT_SUCCESS on a successful exit, with no errors,
 * and return an error status when an error occurs.*/



Thanks! :)
http://cplusplus.com/reference/clibrary/cstdlib/EXIT_SUCCESS/

http://cplusplus.com/reference/clibrary/cstdlib/EXIT_FAILURE/

When do the errors occur? Whenever you get a horrible error in your code that you cant deal with so you have to return =P
Last edited on
Also, the standard int return codes are 0 for success, 1 for failure. Usually anyway.

Example of returning on error or success:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main() {
    int input = 0;
    cout << "Enter an integer: ";
    if (!(cin >> input)) { // Check if input to int 'input' is not successful
        cout << "Error: Bad input type, must be integer\n"; // print error msg
        return 1; // return 1 for failure
    }
    return 0; // Otherwise return 0 for success
}

If you were to input a character or string instead of a number, you'd get an error message, and the error status would be set to 1, otherwise if you put in an int, you'd get 0 status. This is important because most operating systems use the standard 0-1 return codes, and read returned codes to check if the last thing executed was successful or failed. Scripts do this quite often. If you're in Windows or DOS, you can check the return status of the last executed command or program with echo %ERRORLEVEL%, or for Linux, echo $?
Last edited on
Ok thanks guys! :)
Now i've been trying to apply it to a calculator I've made.
There're 2 options:
1) applying it to every place it's needed seperately
2) making a function and then apply it (making the code much more readable)

So I've been trying to make a function out of it, which I couldn't do...

What type of function should I make it? Because if i make it a void, you can't have a return value. But when I make it a double the two ways I can think of are:
1
2
3
4
5
6
7
8
double Test(){
double num1 =0;
if (!(cin >> num1)) { // Check if input to int 'input' is not successful
        cout << "Error: Bad input type, must be a number"; // print error msg
        return 1; // return 1 for failure
        }
else{cin>>num1;}
}

and
1
2
3
4
5
6
7
8
double Test(double num1){
num1 =0;
if (!(cin >> num1)) { // Check if input to int 'input' is not successful
        cout << "Error: Bad input type, must be a number"; // print error msg
        return 1; // return 1 for failure
        }
else{cin>>num1;}
}


I know they're both similar, yet they both don't work.. The 2nd one doesn't work, because I yet have to extract the input (otherwise the program asks for an input twice).
And the first one gives me some random number...

Part of the code I'm working with is:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
int main(){
double num1, num2;
cout << "Enter number 1: ";         
Test();    //usually i'd put the cin here...
        cout<<"\nEnter number 2: ";
         Test(); //the function you gave
         result = num1 + num2;
         cout<<num1<<" + "<<num2<<" = "<<result<<"\n";
cin.get();
return0;
}


Where the Test() function is the one you gave me.


Oh and also, how can I make it assign the input to a variable (like num2) after using a function in the inputstream?

If I'm unclear please tell,

Thanks!
Seriously you shouldve made a new thread but

1
2
3
4
5
6
7
double Test()
{
    double num1 = 0;
    while(!(cin >> num1))
        cout << "Error: Bad input type, must be a number";
    return num1;
}



1
2
3
4
5
6
7
8
9
double num1, num2;
cout << "Enter number 1: ";         
num1 = Test();
cout << "\nEnter number 2: ";
num2 = Test();
cout << num1 << " + " << num2 << " = " << num1+num 2 << "\n";
cin.get();
return0;
}
Last edited on
Haha ok thanks!
caywood is ur codes working now?
yawa66 wrote:
caywood is ur codes working now?
Hmm? What do you mean?

Oh, you mean your code that I was trying to get to work? To be honest, I gave up on it.. You'll have to explain it to me quite descriptively, how each thing in the code is supposed to work, and supposed to do.

Ideally, you should put in comments in your code, describing each piece in the code... Then post the code with all the comments here and I'll go over it and maybe understand it better.
Last edited on
Topic archived. No new replies allowed.