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 .
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.^"
}
}
elseif(rGuess < y) // same thing here, except its either the other one or this that will execute
{
cout << ".";
if(i == rGuess)
{
cout << "v";
}
}
elseif(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 << "<";
}
}
elseif(cGuess < x)
{
if(j == cGuess)
{
cout << ">";
}
}
elseif(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:
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