none
C# Console Application , need help getting input from user

    Question

  • Hey everybody,

     

    I'm trying to write  a C# program that displays a menu to the user that says "1 - Do this, 2 - Do that" etc.

    I have the program displaying the menu, but I don't know how to get information from the user and store it in a variable.

    I also need the user inputting 0 to close the program

     

    I know it seems simple but I'm very new to C# and very used to coding in C++  so any help would be greatly appreciated

    Monday, May 31, 2010 8:11 PM

Answers

  • Please try:

    do{

    Console.Writeline("1. Do Something");

    Console.Writeline("2. Do That");

    string choice = Console.ReadLine();

    switch (choice)

    {

    case "1": // Do Something

    break;

    case "2": //Do that

    break;
    }

    }while (choice != "0")

     

    Welcome to c# ;p


    Regards, Christofel
    • Marked as answer by OMizzy Tuesday, June 01, 2010 1:52 AM
    • Edited by Christofel Hakim Tuesday, June 01, 2010 4:54 AM
    Tuesday, June 01, 2010 12:56 AM

All replies

  • bool correctInput = false;
    while (!correctInput)
    {
      correctInput = true;
    
      string decision = Console.ReadLine();
      int iDecision;
      if (int.TryParse(decision, out iDecision))
        switch (iDecision)
        {
          case 0:
            // do something
            break;
          case 1:
            // do something else
            break;
          default:
            correctInput = false;
            break;
        }
      else
        correctInput = false;
    
      if (!correctInput)
        Console.WriteLine("Tell user their input was bad.");
    }
    Monday, May 31, 2010 8:17 PM
  • Thanks but thats not quite what I'm looking for

     

    but where you have:

    string decision = Console.ReadLine();

    is that how you get the information from the user and store it in a variable? because when I try that it doesnt seem to work

    Monday, May 31, 2010 11:18 PM

  • The read line gets a string version of the "1", "2" or "3", while the int.TryParse() gets an integer value from what the user typed.

    --
    Mike
    Monday, May 31, 2010 11:36 PM
  • Please try:

    do{

    Console.Writeline("1. Do Something");

    Console.Writeline("2. Do That");

    string choice = Console.ReadLine();

    switch (choice)

    {

    case "1": // Do Something

    break;

    case "2": //Do that

    break;
    }

    }while (choice != "0")

     

    Welcome to c# ;p


    Regards, Christofel
    • Marked as answer by OMizzy Tuesday, June 01, 2010 1:52 AM
    • Edited by Christofel Hakim Tuesday, June 01, 2010 4:54 AM
    Tuesday, June 01, 2010 12:56 AM
  • I will try to remember my C++ skills since 1999 :)

    The concole.RealLine is equivalent to cin>>decision. It will wait until the users enter the text and press the return key.

    On the other hand, Concole.WriteLine is equivalent to cout<<"some text" and it will end that with line feed.

    to write you app you would write

    Concole.WirteLine("1. Do this")

    Concole.WirteLine("2. Do this")

    Concole.WirteLine("ask the user to enter a number")

    String input;

    boolean ValidInput;

    do{

    input = Concole.ReadLine();

    ....

    }while(!ValidInput)

    Please let me know if you need more help.

    Tuesday, June 01, 2010 12:57 AM
  • So this whole statement takes the data the user typed and then converts it from string to int?

     string
     decision = Console.ReadLine();
    int iDecision;
    if (int .TryParse(decision, out iDecision))



    and then the switch case calls whatever it is i need to call from that conversion


    then what is the command if it was set up so that if the case was 0 the program would close

    switch
    (iDecision)
    {
    case 0:
    // do something
    break ;

    Tuesday, June 01, 2010 12:57 AM
  • You can still compare using strings. You don't have to convert to integers.
    Tuesday, June 01, 2010 12:59 AM
  • Please try:

    do{

    Console.Writeline("1. Do Something");

    Console.Writeline("2. Do That");

    string choice = Console.ReadLine();

    switch (choice)

    {

    case "1": // Do Something

    break;

    case "2": //Do that

    break;
    }

    }while (choice !=0)

     

    Welcome to c# ;p


    Regards, Christofel

    Its telling me I can't apply the '!=' operator to a string
    Tuesday, June 01, 2010 1:08 AM

  • The last line should be:
     
    } while (choice != "0")

    --
    Mike
    Tuesday, June 01, 2010 1:13 AM
  • surround the 0 with quotes (i.e. "0")

     

    I just tried

     string i = "1";
          if (i != "0")
          {
    
          }

     The compiler didn't complain

    Tuesday, June 01, 2010 1:13 AM
  • I think I may have gotten it to work stay tuned for updates but thanks a ton you guys are the best!
    Tuesday, June 01, 2010 1:19 AM
  • Finished it everybody! Thanks so much for your help!!!
    Tuesday, June 01, 2010 1:51 AM
  • Hi, i'm sorry, i have already edited my code, its actually

    ... } while (choice != "0") // with double qoutes

    hm.. if you are using .net framework 3.5 why don't you try using WorkFlow Console Application? it vary easy and helpful =)


    Regards, Christofel
    Tuesday, June 01, 2010 4:59 AM