i cant figure out the problem

hi im stuck as to why when i run the programme only the 'q' and 'm'command work and not the rest.

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
ifndef BOARD_H
#define BOARD_H



class Board
{
public:
    Board();
    void penUp();
    void setPenDown();
    void turnRight();
    void turnLeft();
    void movePen();
    void printBoard();
    bool getPenStatus();
    void doCommand(char cmd);
private:

    const static int ARRAY_SIZE = 12;
    int xPos= ARRAY_SIZE;
    int yPos=ARRAY_SIZE;
    char floor[12][12];
    int direction;
    bool penDown;
    const static int East=0;
    const static int South=1;
    const static int West=2;
    const static int North=3;
    const static char playerSymbol ='x';
    const static char clearSymbol =' ';
};

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
#ifndef USER_H
#define USER_H



class User
{
public:
    User();
    char getCommand();
private:
    bool isLegal(char element);
    void showMenu();



};

#endif // USER_H

#include <stdlib.h>
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include "user.h"
#include "board.h"


Board::Board()
{    direction = East;
     penDown = false;
     xPos = 0;
     yPos = 0;
     for(int x = 0; x < ARRAY_SIZE; x++)
         for(int y = 0; y < ARRAY_SIZE; y++)
             floor[x][y] = clearSymbol;
 }


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
 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;

 }
 void Board:: setPenDown(){
     penDown=true;

 }
 void Board ::turnRight(){
    direction++;
    if (++direction >= 4) direction = 0;
 }
 void Board ::turnLeft(){
   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++)
         for(int i=0;i<ARRAY_SIZE;i++)
     {
     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)
    {
    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;
 }


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
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

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



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

    char theCommand; 	//char variable to store the player's current command
    do
    {
        theCommand=player.getCommand();//3.Use the player 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

            //if theCommand is not to quit, then pass it to the gameBoard to process
            gameBoard.doCommand(theCommand);
       }

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


    return 0;
};


1
2
#include <iostream>
#using namespace std; 

#include <ctype.h>
#include <stdio.h>
#include "user.h"
#include "board.h"

User::User()
{showMenu();

}
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'))
{
return true;
}
else{
return false;
}
}
void User::showMenu(){
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";

}


#endif // BOARD_H[/code]
Last edited on
In what way does it not work?
when i run the program and the menu pops up no other command works only the m and q
maybey i did something wrong with the other characters?
it keeps saying illegal command type M to see menu
1
2
3
4
5
6
7
8
9
10
void User::showMenu(){
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";

}
none of the other choices work
Last edited on
Topic archived. No new replies allowed.