help with graphic class

i have this programe that is suppose to pull up this menu

//showing menu
cout << "Please select one of the following options\n";
cout << "by pressing the indicated key:\n";
cout << "\n\t8: Pen up\n";
cout << "\n\t2: Pen down\n";
cout << "\n\t4: Turn left\n";
cout << "\n\t6: Turn right\n";
cout << "\n\t5: Move\n";
cout << "\n\tM: Menu\n";
cout << "\n\tQ: Quit\n";

when i enter the programme however i can only type Q or M
when i type the other options it gives illegal command , type m to see the menu
perhaps it was my if statements or switches? i am not sure.
i also think their is something wrong with my printBoard function
does a number of things. It first clears the screen so that a fresh instance of the board is being displayed. This is accomplished by calling the command system("cls");//clears the screen; make sure to include either <stdlib> or <stdlib.h>
Next, it prints an arrow on the screen that shows the current direction.
i
1
2
f (direction == NORTH)
	cout<<"N\n^\n|\n|\n"; //should produce something like  
N
^
|
|
Below that arrow, it then prints the two dimensional array, floor, to the screen. You should know how to print an array by now. Here is a hint about doing this although you will need to improve on it and make it more dynamic rather than hardcode the number of vertical bars etc. For starters, you will need to use two for loops and the constant ARRAY_SIZE. The example below is ONLY to provide you with an idea of how to go about doing this.
1
2
3
4
5
6
7
8
9
10
int t;

for (t=0; t<3; t++) 
{
cout<<" "<< floor[t][0] << " | "<< floor [t][1] << " | "
		<< floor [t][2];
if(t!=2)
cout <<"\n---|---|---\n"; //you should not hardcode this line as you see here
}
cout<<"\n";


i dont quite understand it and my code looks like this
perhaps this is why it is not printing?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void Board::printBoard()
{
    system("cls"); //clears the screen;
    // loop for array's rows
    if (direction == North)
        cout << "N\n^\n|\n|\n"; //should produce something like

    for (int t = 0; t < ARRAY_SIZE; t++) //loop to represent row
        for (int i = 0; i < ARRAY_SIZE; i++) //loop to represent column
        {
            cout << " " << floor[t][i] << " | ";

            if (t != 2) {
                cout << "\n---|---|---\n"; //you should not hardcode this line as you see here
            }
            cout << "\n";
        }
}


this is the main
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
using std::cout;
using std::cin;
using std::endl;

#include "user.h"
#include "board.h"

int main(void)
{
    User player; //1.creating  a User object called player
    Board gameBoard; //2.creating a Board object called gameBoard

    char theCommand; //char variable to store the player's current command
    do {
        theCommand = player.getCommand(); //3.Using object's getCommand function to get a command and assign it to variable theCommand

        if ((theCommand == 'M') || (theCommand == '4') || (theCommand == '8') | (theCommand == '6') || (theCommand == '2') || (theCommand == '5')) { //4.check if theCommand is not the quit command Q

            //theCommand is not to quit, so it is passed to the gameBoard to process
            gameBoard.doCommand(theCommand);
        }

    } while (theCommand != 'Q'); //quits when the player presses 'Q'
    gameBoard.printBoard();

    return 0;
};


and this is the .cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include "user.h"
#include "board.h"

Board::Board() //constructor
{
    direction = East;
    penDown = false;
    xPos = 0;
    yPos = 0;
    for (int x = 0; x < ARRAY_SIZE; x++) //for loop to represent rows
        for (int y = 0; y < ARRAY_SIZE; y++) //for loop to represent columns
            floor[x][y] = clearSymbol; //initializing array flow to data member char ' ';
}


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
void Board::movePen()
{
    if (penDown)
        floor[xPos][yPos] = playerSymbol;

    switch (direction) {
    case East:
        if (xPos < ARRAY_SIZE - 1)
            ++xPos;
        break;
    case West:
        if (xPos > 0)
            --xPos;
        break;
    case South:
        if (yPos < ARRAY_SIZE - 1)
            ++yPos;
        break;
    case North:
        if (yPos > 0)
            --yPos;
        break;
    }
}

void Board::penUp()
{
    penDown = false; //initializing penDown
}
void Board::setPenDown()
{
    penDown = true; //Initializing penDown
}
void Board::turnRight()
{
    direction++; //incrementing direction
    if (++direction >= 4)
        direction = 0;
}
void Board::turnLeft()
{
    direction--; //decrementing direction
    if (--direction < 0)
        direction = 3;
}

void Board::printBoard()
{
    system("cls"); //clears the screen;
    // loop for array's rows
    if (direction == North)
        cout << "N\n^\n|\n|\n"; //should produce something like

    for (int t = 0; t < ARRAY_SIZE; t++) //loop to represent row
        for (int i = 0; i < ARRAY_SIZE; i++) //loop to represent column
        {
            cout << " " << floor[t][i] << " | ";

            if (t != 2) {
                cout << "\n---|---|---\n"; //you should not hardcode this line as you see here
            }
            cout << "\n";
        }
}

void Board::doCommand(char cmd)
{
    switch (cmd) //creating switch
    {
    case '4':
        turnLeft();
        break;

    case '8':
        penUp();
        break;
    case '6':
        turnRight();
        break;
    case '2':
        setPenDown();
        break;
    case '5':
        movePen();
        break;
    }
}

bool Board::getPenStatus()
{
    return penDown; //return whether penDown is true or false
}
[code]

#include <iostream>

using namespace std;
#include <ctype.h>
#include <stdio.h>
#include "user.h"
#include "board.h"

User::User()
{
showMenu(); //caling user
}
char User::getCommand()
{
char cmd; //the command that will be input by the user

while (true) //keep trying until the user enters an acceptable command
{
cout << "\n> "; //prompt the user
cmd = static_cast<char>(cin.get()); //grab a single char at a time and assign it to cmd
cmd = toupper(cmd); //convert the command (cmd) to upper-case and assign it back to itself

//call function isLegal and pass it cmd. If isLegal returns true if cmd is a valid command
if (isLegal(cmd)) {
if (cmd == 'M') //if the command entered is 'M', show the menu and stay in the loop
showMenu();
else
return cmd; //getting here means the command entered was valid but not 'M'
}
else //command entered was illegal
{
cout << "\n** Illegal command.";
cout << " Type M to see menu.";
}
}
}

bool User::isLegal(char cmd)
{
if ((cmd == 'Q') || (cmd == 'M') || (cmd == '4') || (cmd == '8') || (cmd == '6') || (cmd == '2') || (cmd == '5')) //checking if any of these characters is == to cmd
{
return true;
}
else {
return false;
}
}
void User::showMenu()
{ //showing menu
cout << "Please select one of the following options\n";
cout << "by pressing the indicated key:\n";
cout << "\n\t8: Pen up\n";
cout << "\n\t2: Pen down\n";
cout << "\n\t4: Turn left\n";
cout << "\n\t6: Turn right\n";
cout << "\n\t5: Move\n";
cout << "\n\tM: Menu\n";
cout << "\n\tQ: Quit\n";
}

[/code]



Last edited on
Please make a single compilable version of your program. And tell us where things are going wrong which is confusing right now.
Topic archived. No new replies allowed.