# Thread: help for my programming exercice

1. ## help for my programming exercice

Hello,

I am studying programming at a beginner level at university and I am stuck into an exercice I would be glad if you could help me to solve it.
we have to implement a menu driven interface for robby(robot).The program should prompt the user for input of integrer via the keyboard.

The menu should provide the following behaviour on typing the number 1-5:

1-move
2-turn left
3-turn right
4-perform a random move(either 1,2 or 3)
5-exit

all other cases should indicate an error

Code:
``` usingSystem;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CE0721a
{
classtut6_3
{
publicvoid run()
{
//make object
Robotrobby=newRobot();
Room room=newRoom(8);
Picture picture=newPicture(room,robby);

// declare variable
Random rndm = newRandom(); //random number generator
int command=0;//variable declared to store the command input
do
{
Console.Write("1. move\n");
Console.Write("2.turn left\n");
Console.Write("3. turn right\n");
Console.Write("4. random move\n");
Console.Write("5. exit\n");
while(true)
{
Console.WriteLine("Enter robby command");
}
if (command != 1)
robby.move();
elseif (command != 2)
robby.left();
elseif (command != 3)
robby.right();
{
if (command != 4)
command = rndm.Next(5) + 1; // Random.Next(inclusive low, exclusive high)
}
elseif (command != 5)
exit();
}
}
}
```

best regards
Thibault

2. Well... except this isn't C code.

3. Methinks it is the very java-esque C#.

4. Regardless of what it is, the OP needs to make sure that they do "Copy/Paste Text" rather than "Copy/paste HTML" (or some other "enhanced" format).

Moved to C#

5. First thing I'd say is use int.TryParse rather than int.Parse, otherwise if the user enters a non-numeric input it will throw an exception and your program will crash.

6. Your implementation of a do/while loop is incorrect. The format is:
Code:
```do
{
} while(condition);```
Code:
```    class Program
{
static void Main(string[] args)
{
Robot robby = new Robot();
Random rndm = new Random();

int command;
do
{
Console.WriteLine("1. Move");
Console.WriteLine("2. Turn Left");
Console.WriteLine("3. Turn right");
Console.WriteLine("4. Random move");
Console.WriteLine("5. Exit");

Console.Write("Enter robby command: ");

if (command == 4)
command = rndm.Next(1, 4);

if(command == 1)
robby.Move();
else if(command == 2)
robby.Left();
else if(command == 3)
robby.Right();
} while (command != 5);
}```
As theobe pointed out, int.TryParse() is much better for accepting user input to avoid exceptions. Also, the code listing provided doesn't account for syntactically valid but logically invalid responses such as -5 or 7.