Secret number guessing program

Hi, I am trying to make a program that has a secret number and then the user has to guess that secret number by entering a guess for the row and a guess for the column. I am making a grid to display the amount of rows and columns and should have an "X" where the user's guess is and hint characters (<, >, ^, v) for giving hints as to which way the point really is.

Here's what my grid should look like if the user enters 2 for row and 3 for column and the row number secret point is higher and the column number secret point is smaller:
. . < . . .
v v X v v v
. . < . . .
. . < . . .

But mine is all messed up and looks more like this:
. . . > . . .
. v . vX . v > . v . v .

. . . > . . .

Can someone please help me fix it? :(

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
y = (rand() % MAX_ROWS) + 1;
	x = (rand() % MAX_COLUMNS) + 1;

for (int i = 1; i <= MAX_ROWS; i++)
					{
						for (int j = 1; j <= MAX_COLUMNS; j++)
						{

							if((i == rGuess) && (j == cGuess))
							{
								cout << 'X';
							}

							if(rGuess > y)
							{
							cout << ".";
							
								if(i == rGuess)
								{
									cout << "^";
								}
	 
							}

							else if(rGuess < y)
							{
								cout << ".";
								
								if(i == rGuess)
								{
									cout << "v";
								}
							}

							else if(rGuess == y)
							{
								cout << ".";
							}


							if(cGuess > x)
							{
								if(j == cGuess)
								{	
									cout << "<";
								}
							}

							else if(cGuess < x)
							{
								
								if(j == cGuess)
								{
									cout << ">";
								}
							}

							else if(cGuess == x)
							{
								cout << ".";
							}
						}
						cout << "\n";
					}
Last edited on
shouldn’t you use a switch() for that table of else ifs?

What is cQuess and rGuess?

shouldn’t one iteration of the innermost only print at most 1 character?

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
y = (rand() % MAX_ROWS) + 1;
    x = (rand() % MAX_COLUMNS) + 1;

for (int i = 1; i <= MAX_ROWS; i++)
                    {
                        for (int j = 1; j <= MAX_COLUMNS; j++) /*/innermost loop, you have to make sure that 1 iteration of this loop only prints 1 character  */
                        {

                            if((i == rGuess) && (j == cGuess)) // if this is true then it will print X
                            {
                                cout << 'X';
                            }

                            if(rGuess > y) // if this is true then it will print also print .
                            {
                            cout << ".";
                            
                                if(i == rGuess) // if this is true and the outer if is also true then combined it will print .^
                                {
                                    cout << "^"; // at this point it could print a string like this "X.^"
                                }
     
                            }

                            else if(rGuess < y) // same thing here, except its either the other one or this that will execute
                            {
                                cout << ".";
                                
                                if(i == rGuess)
                                {
                                    cout << "v";
                                }
                            }

                            else if(rGuess == y)
                            {
                                cout << "."; //this means that the row is correct right?
                            }


                            if(cGuess > x) // umm i think you get the point now, and i dont really get what your doing after this point....
                            {
                                if(j == cGuess)
                                {    
                                    cout << "<";
                                }
                            }

                            else if(cGuess < x)
                            {
                                
                                if(j == cGuess)
                                {
                                    cout << ">";
                                }
                            }

                            else if(cGuess == x)
                            {
                                cout << ".";
                            }
                        } //at this point is could of printed a whole bunch of characters...
                        cout << "\n";
                    }
Well, I guess I could use a switch statement but I'd rather not. :/

Sorry, here is the code for the user input:
1
2
3
4
5
6
 
05	cout << "\nWhich row do you guess? (1...4):";
06	cin >> rGuess;
07	 
08	cout << "\nWhich column do you guess? (1...6):";
09	cin >> cGuess;


What I have to do is generate a secret point (x, y) and then the user has to try and guess it. By showing a grid of simple "." characters, it's supposed to show the user their guessed point (cGuess, rGuess) and then hint characters pointing them in the right direction.

For example, if the user entered 3 for their row guess and 4 for their column guess (and x is larger than cGuess and y is smaller than rGuess) the grid should look like this:

. . . < . .
. . . < . .
^^^X^^
. . . < . .

But mine doesn't look like that. And I don't know what I'm doing wrong.
the problem is that it is possible for more then one of those if statements to be true. that means, you could print more then 1 character per cycle of the loop that prints the rows, giving you weird out put.

this is some code i wrote before to do something related:

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
void printgrid (int pg [10][10]){ // this prints a multidimensional array
	cout << endl;

	for (int y=1;y <= 10 ; y++){//this part is the same as you have
		for (int x=1; x <=10;x++){//this part is too

			switch (pg[x][y]){ //see how the switch can only possible run 1 case, so it can only execute 1 of the cout,
			case 3:
				cout << "[d]";
				break;
			case 4:
				cout << "[b]";
			break;
			case 5:
				cout << "[c]";
				break;
			case 2:
				cout << "[p]";
				break;
			case 13:
				cout << "[O]";
				break;
			case -1:
				cout << "[X]";
				break;
			default:
				cout << "[ ]";
				break;

			}// end of the switch statment

		}

		cout << "\n"; // same as you have
	}

} // end of function 


output:

[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][d][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][d][ ]
[ ][p][ ][ ][b][ ][ ][ ][d][ ]
[ ][p][ ][ ][b][ ][ ][ ][ ][ ]
[ ][ ][d][ ][b][ ][ ][ ][ ][ ]
[ ][ ][d][c][b][c][c][c][ ][ ]
[ ][ ][d][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]


now to fix yours....
i put it in a function for 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
30
31
32
33
void prindgrid(int rGuess, int cGuess){
int y = (rand() % 10) + 1;
int x = (rand() % 10) + 1;

for (int i = 1; i <= 10; i++)
					{
						for (int j = 1; j <= 10; j++)
						{

							if((i == rGuess) && (j == cGuess))
							{
								cout << 'X';
							}

								else{

									if (cGuess==j){
										if (cGuess<x) cout<<'<';
										else if(cGuess>x) cout<<'>';
									}
									else if (rGuess==i){
										if (cGuess<y) cout << 'V';
										else if (cGuess>y) cout << '^';
									}
									else cout << ".";

								}

						}
						cout << "\n";
					}
}

there was a lot wrong with the original code.


....<.....
....<.....
....<.....
^^^^X^^^^^
....<.....
....<.....
....<.....
....<.....
....<.....
....<.....
so, did it help?
Topic archived. No new replies allowed.