Hello all!

I'm having trouble with my homework assignment - I've been working on it for hours and have gotten it down to one syntax error and then I think it will compile so I can move on, I just can't seem to figure out what the problem is. Here is my code.... THANKS!!

#include <iostream>
#include <string>
using namespace std; 
int main()
{ 
unsigned int board = 511;
unsigned int mask = 256;
int i, j;
int number; 
do { 
cout << "Press a number [1-9 or 0 to quit]:";
cin>> number; 
switch (number)
{ 
 
case 1: board = board ^ 416; // if (input == 1) then (switch 1,2,4) 
break; 
case 2: board = board ^ 464; // if (input == 2) then (switch 1,2,3,5)
break; 
case 3: board = board ^ 200; // if (input == 3) then (switch 2,3,6)
break; 
case 4: board = board ^ 308; // if (input == 4) then (switch 1,4,5,7)
break; 
case 5: board = board ^ 186; // if (input == 5) then (switch 2,4,5,6,8)
break; 
case 6: board = board ^ 89; // if (input == 6) then (switch 3,5,6,9)
break; 
case 7: board = board ^ 38; // if (input == 7) then (switch 4,7,8)
break; 
case 8: board = board ^ 39; // if (input == 8) then (switch 4,7,8,9)
break; 
case 9: board = board ^ 11; // if (input == 9) then (switch 6,8,9)
break; 
} 
for (i=0; i<3; i++)
 
{ 
for (j=0; j<3; j++)
{ 
 
if ((board & mask) == 0) cout << "0"; 
else cout << "1"; 
mask = mask >> 1; 
} 
while (board!=0); }
{ 
if (board == 0)cout << "YOU WIN" << endl; 
 
} 
cout << endl; 
 
}
 
return 0; 
}

Recommended Answers

All 3 Replies

I've been working on it for hours and have gotten it down to one syntax error

Next time, please give the syntax error. If you had learnt to properly indent your code, you wouldn't have got this error. Here is the corrected code.

#include <iostream>
#include <string>
using namespace std; 
int main()
{ 
    unsigned int board = 511;
    unsigned int mask = 256;
    int i, j;
    int number; 
    do 
    { 
        cout << "Press a number [1-9 or 0 to quit]:";
        cin>> number; 
        switch (number)
        { 
            case 1: 
                board = board ^ 416; // if (input == 1) then (switch 1,2,4) 
                break; 
            case 2: 
                board = board ^ 464; // if (input == 2) then (switch 1,2,3,5)
                break; 
            case 3: 
                board = board ^ 200; // if (input == 3) then (switch 2,3,6)
                break; 
            case 4: 
                board = board ^ 308; // if (input == 4) then (switch 1,4,5,7)
                break; 
            case 5: 
                board = board ^ 186; // if (input == 5) then (switch 2,4,5,6,8)
                break; 
            case 6: 
                board = board ^ 89; // if (input == 6) then (switch 3,5,6,9)
                break; 
            case 7: 
                board = board ^ 38; // if (input == 7) then (switch 4,7,8)
                break; 
            case 8: 
                board = board ^ 39; // if (input == 8) then (switch 4,7,8,9)
                break; 
            case 9: 
                board = board ^ 11; // if (input == 9) then (switch 6,8,9)
                break; 
        } 
        for (i=0; i<3; i++)
        { 
            for (j=0; j<3; j++)
            { 
                if ((board & mask) == 0)
                    cout << "0"; 
                else 
                    cout << "1"; 
                mask = mask >> 1; 
            } 
        }// was missing
    }// was missing
    while (board!=0); 
    // } is extra
    // {  not needed
    if (board == 0)
        cout << "YOU WIN" << endl; 
 
    // } is extra
    cout << endl; 
 
    // } not needed
 
    return 0; 
}

Thanks for the help! I am still so confused. The board is suppose to look like this at the beginning:

111
111
111

and then when a number is pressed all numbers up, down and to each side of it will change including the number pressed. So, if a 5 is pressed at this point the board will look like this

101
000
101

and then if a 1 is pressed it will switch to this
011
100
101
and so forth.....

I had been able to have the print of the board working until I started adding code to it to try to get it to loop back up to the top until someone can make the board have all zeros (that is how the "game" is won).

Can you give me some insight into:

1) why isn't the board set up in 3 rows of 3 anymore
and
2) where else am I going wrong in my loop, I should be able to continue playing and pressing a number again and again until I win or press zero to quit.

Thanks!
confused!

> switch (number)
With a suitable table of values (ie, an array), you could do all this with one line board ^= table[number];

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.