Problem with robot code

The problem is that Im trying to get rid of a gap in the output(at bottom) but to do so I need to get rid of the first endl under the case'W' but if I do that the output will get worse.

At the bottom I'll put the output I'm trying to get, the output I'm getting, and the output I'll get if i get rid of the endl.

The inputs are:
S B U U W R R W D D W D D W M T R T

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
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <fstream>
using namespace std;

int main()
{

   char command;
   int x, y, vm, bm, bc;
   ifstream inF;
     

x = 0; y = 0; vm = 0; bm = 0; bc = 0;
    
    inF.open("robot100.cmds");
    
   do {
               inF >> command;

               //-| Handle the command.
               switch (command)
               {

                     case 'S': // start robot.
                        cout << "STARTED AT POSITION (" << x <<"," << y << ")" << endl;
                           break;
                       
                     case 'R':
                        if (x < 10){
                            x++;
                            vm++;
                            cout << "RIGHT ";
                           
                        }
                        else if(x = 10){
                               cout << "RIGHT BAD MOVE TO (" << "11," << y << ")" << endl;
                               bm++;
                               break;
                           }
                       break;
                       
                       
                     case 'U':
                       if (y < 10){
                        y++;
                        vm++;
                        cout << "UP ";
                        
                       }
                       else if(y = 10){
                               cout << "UP BAD MOVE TO (" << x << ",11" << ")" << endl;
                               bm++;
                               break;
                           }
                       break;
                       
                     case 'L':
                        if (x > 0){
                            x--;
                            vm++;
                            cout << "LEFT ";
                        }
                        else if(x == 0){
                               cout << "LEFT BAD MOVE TO (" << "-1," << y << ")" << endl;
                               bm++;
                               break;
                           }
                        break;
                       
                     case 'D':
                       if (y > 0){
                        y--;
                        vm++;
                        cout << "DOWN ";   
                       }
                        else if(y == 0){
                               cout << "DOWN BAD MOVE TO (" << x << ",-1" << ")" << endl;
                               bm++;
                               break;
                           }
                       break;
                       
                   case 'W':
                       cout << endl << "POS = (" << x << "," << y <<")" << endl;
                       break;
                       
                     case 'T':
                       cout << "TERMINATED AT POSITION (" << x <<"," << y << ")" << endl;
                       break;

                 default:  cout << "BAD COMMAND '" << command
                                << "' IGNORED." << endl;
                           break;
               }//end_switch
  

            } while (command != 'T');


    return 0;

} //main   


CORRECT OUTPUT:

STARTED AT POSITION (0,0)
BAD COMMAND 'B' IGNORED.
UP UP
POS = (0,2)
RIGHT RIGHT
POS = (2,2)
DOWN DOWN
POS = (2,0)
DOWN BAD MOVE TO (2,-1)
DOWN BAD MOVE TO (2,-1)
POS = (2,0)
BAD COMMAND 'B' IGNORED.
TERMINATED AT POSITION (2,0)


CURRENT OUTPUT:

STARTED AT POSITION (0,0)
BAD COMMAND 'B' IGNORED.
UP UP
POS = (0,2)
RIGHT RIGHT
POS = (2,2)
DOWN DOWN
POS = (2,0)
DOWN BAD MOVE TO (2,-1)
DOWN BAD MOVE TO (2,-1)
// trying to get rid of this gap
POS = (2,0)
BAD COMMAND 'M' IGNORED.
TERMINATED AT POSITION (2,0)


OUTPUT WITHOUT the endl:

STARTED AT POSITION (0,0)
BAD COMMAND 'B' IGNORED.
UP UP POS = (0,2)
RIGHT RIGHT POS = (2,2)
DOWN DOWN POS = (2,0)
DOWN BAD MOVE TO (2,-1)
DOWN BAD MOVE TO (2,-1)
POS = (2,0)
BAD COMMAND 'M' IGNORED.
TERMINATED AT POSITION (2,0)
Last edited on
Compile with maximum warnings.
1
2
3
4
5
6
7
8
9
10
11
$ g++ -Wall -Wextra foo.cpp
foo.cpp: In function ‘int main()’:
foo.cpp:38:39: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
                         else if(x = 10){
                                       ^
foo.cpp:53:38: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
                        else if(y = 10){
                                      ^
foo.cpp:12:22: warning: variable ‘bc’ set but not used [-Wunused-but-set-variable]
    int x, y, vm, bm, bc;
                      ^

Topic archived. No new replies allowed.