C# Assignment help

Black_Rose

Black_Rose

@black-rose-OYtC6o Oct 22, 2024
Agh, okies I'm trying to loop my if statement, which loop should I use?

if (Tnumber > 25)
{
Console.Write("Invalid input!\n\nPlease enter a table number between 1 and 25: ");
Tnumber = Int32.Parse(Console.ReadLine());
}
Basically what the objective is, that if the user keeps entering a value higher than 25 it should continue to show that message until such time that a value below 25 is entered.. also whilst I'm asking... Can you also tell me how do I add another clause so that anything but the numbers between 1 and 25 also returns the same error message.

Thanks =)

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • shalini_goel14

    shalini_goel14

    @shalini-goel14-ASmC2J May 4, 2009

    Hi Black_Rose,

    I guess you can better use do...while loop

    something like below:
    ...
    do{
    if (Tnumber > 25)
    {
    Console.Write("Invalid input!\n\nPlease enter a table number between 1 and 25: ");
    Tnumber = Int32.Parse(Console.ReadLine());
    } 
    else
    {
    Console.Write("Invalid input!\n\nPlease enter a table number between 1 and 25: ");
    break;
    
    } 
    }while(Tnumber > 0)  // or condition that proves that user has input a number.
     
    
    Hope I understood your problem and have answered well. 😀
  • Black_Rose

    Black_Rose

    @black-rose-OYtC6o May 4, 2009

    Shalini, that's absolutely perfect thank you. I attempted the do while loop, but messed it up somehow :S I've changed the following on your code:

    else
    {
    Console.Clear ();
    break;
    }
    Just so when I'm done it clears the screen and displays new information.

    =)
  • Black_Rose

    Black_Rose

    @black-rose-OYtC6o May 4, 2009

    Okay this is another part I'm struggling to understand. I'm asking the user to code in p for pizza and d for drinks. If it's a p then a pizza menu will show up if d is typed in than a drinks menu. How do I assign the 'd' and 'p' to show their respective menus? I've done the following:

    Console.WriteLine("\n\n\t\t\tPlease enter which menu you would like to order from: \n\n\t\t\t\t1) Type 'p' for Pizza \n\n\t\t\t\t2) Type 'd' for Drinks\n");
    Console.ReadLine();
    =)
  • shalini_goel14

    shalini_goel14

    @shalini-goel14-ASmC2J May 5, 2009

    Hi Black_Rose,

    I am not sure whether you tried something like following:

    Console.WriteLine("\n\n\t\t\tPlease enter which menu you would like to order from: \n\n\t\t\t\t1) Type 'p' for Pizza \n\n\t\t\t\t2) Type 'd' for Drinks\n");
     
    // assigns whatever user enters to a variable menuSelectOption
    String menuSelectOption= Console.ReadLine(); 
    
    Hope I understood your problem correctly. 😀
  • Black_Rose

    Black_Rose

    @black-rose-OYtC6o May 5, 2009

    Shalini, thanks alot for your help, don't worry I found out I need to use switch statements =) <3