help for my programming exercice

This is a discussion on help for my programming exercice within the C# Programming forums, part of the General Programming Boards category; Hello, I am studying programming at a beginner level at university and I am stuck into an exercice I would ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    3

    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");
    command=int.Parse(Console.ReadLine());//input command to variable
    }
    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();
    }
    }
    }
     


    Thank you in advance for your help
    best regards
    Thibault

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well... except this isn't C code.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Methinks it is the very java-esque C#.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    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#
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    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. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Your implementation of a do/while loop is incorrect. The format is:
    Code:
    do
    {
    } while(condition);
    Try something like this instead:
    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: ");
                    command = int.Parse(Console.ReadLine());
    
                    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.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About Device Driver Programming And Socket Programming
    By pritesh in forum Linux Programming
    Replies: 6
    Last Post: 01-23-2010, 02:46 AM
  2. Replies: 1
    Last Post: 08-19-2007, 03:55 AM
  3. small programming job VCPP / Object Oriented Programming
    By calgonite in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 01-04-2006, 10:48 PM
  4. Total newb to programming here... Question about the many programming languages. Ty!
    By tsubotakid1 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-05-2003, 10:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21