Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Hybrid View

  1. #1
    Join Date
    Sep 2002
    Location
    So. Cal.
    Posts
    133

    C# Homework #1 Help

    I've got my list of items for the vending machine to work but I can't figure out how to set up the code for when the user makes an item selection? Can someone give me a hint on how to go about this or what part of the video to watch? I keep going over the video but I can't figure this out.

    M.

  2. #2
    Join Date
    May 2004
    Location
    San Francisco
    Posts
    586
    Sorry I don't have time to re-watch the video to find the spot, but this is what I did to get the user section:

    Code:
    pickedItem = int.Parse(Console.ReadLine());

  3. #3
    Join Date
    Mar 2009
    Location
    Bellevue, WA
    Posts
    803
    Hi Mordekai.

    I didn't watch those videos but from my experience can assume what you're trying to do. If I'm wrong correct me. I assume that since this was the very first class things like printing out info and getting input from the user were covered. It sounds to me like you are printing out a variety of items to the screen for the user to choose from and you want to get input from the user and then decide how to handle the input from the user correct? Not sure if loops were covered cause they may want you to loop and continue the process allowing the user to make multiple selections. If not I can probably assume they just want the user to make one selection.

    Since you are having trouble deciding what to do after the user makes a selection I will also assume you've already got code in place to handle the user input and the user is probably typing either a integer value (a whole number) or a string.

    If you are storing the user input and want to decide how to handle it based on the input that was given I would look into using a Switch statement.

    A switch statement will allow you to pass into it a value be it a integer, string or enum and then based on the value in the data(variable) you pass it, it will compare that to the cases you provide and then if it matches a case value it will complete the code inside the brackets for that particular case. If it doesn't find a case value you provided that matches it will either fall out of the switch statment and continue with any statements after the switch unless you provide a default case which is a case that will handle any value that's not a case value and then continue with any statements after the switch. I'll give you a simple example:

    Let's say I have a integer value of 1 that I will store in a integer variable
    Code:
    int num = 1;
    I can now use this variable in a switch statement. The switch statement will look at the value in num and see it has a value of 1 and then compare it to any cases I provide in the switch to see if the case value is 1. Be careful though and make sure when setting up your cases you set a break after each case or it will fall through and execute code in your other cases until it hits a break in another case or finally reaches the end of the switch statement.

    Code:
    switch (num)
    {
          case 0:
          {
                // If num is 0 do some action
          }
          break;
    
          case 1:
          {
                // If num is 1 do some action
          }
          break;
    
          default:
          {
                // If num doesn't match any case value perform some action
          }
    }
    I hope this helps a little. Rember case doesn't have to be a integer value it can also be a string or enum if you guys convered those. Also, you don't need the curly brackets after the case but I like to use them to help better define a block of code for each case. It keeps things neat and easy to read.
    " Imagination is the preview of life's coming attractions " - Albert Einstein
    " If you can't explain it simply, you don't understand it well enough." - Albert Einstein

    My Website: http://www.gamedevlounge.com/

    If any post is informative please provide positive feedback by clicking the star below.

  4. #4
    Join Date
    May 2004
    Location
    San Francisco
    Posts
    586
    Great explanation Kwest, but we didn't cover switch statements in week 1.
    We do in week 2 or 3.
    We had to use a while loop & if's

  5. #5
    Join Date
    Mar 2009
    Location
    Bellevue, WA
    Posts
    803
    OH! Sorry. That's what I get for not watching the videos
    " Imagination is the preview of life's coming attractions " - Albert Einstein
    " If you can't explain it simply, you don't understand it well enough." - Albert Einstein

    My Website: http://www.gamedevlounge.com/

    If any post is informative please provide positive feedback by clicking the star below.

  6. #6
    Join Date
    Oct 2009
    Location
    The In Between. US
    Posts
    2,030
    we didn't even go over loops yet either

    so you would use an if statement

    Code:
    if(choice == 1)
      //do this
    else 
      //do this
    elevating to new levels of knowledge (that's the plan at least )

  7. #7
    Join Date
    Sep 2002
    Location
    So. Cal.
    Posts
    133
    Sorry for the bad formatting!

    This is what I have been trying:

    Code:
    int shotgun;
    				shotgun = int.Parse(Console.ReadLine());
    
    					if (shotgun == 1)
    					{
    						Console.WriteLine("You've selected the 12 Gage Shotgun!");
    					}
    That 'if' statement is currently nested, though I'm not sure if this is causing problems. I see the code as the shotgun being declared and if the user was to press the 1 key it would then print to the console "you got the blah blah blah". Is my thinking correct or I'm I way off?
    Last edited by Mordekai; 04-03-2012 at 05:49 PM. Reason: formatting problems

  8. #8
    Join Date
    May 2004
    Location
    San Francisco
    Posts
    586
    Try something like this....

    Code:
    int pickedItem;
        pickedItem = int.Parse(Console.ReadLine());
    
        if (pickedItem == 1)
       {
           Console.WriteLine("You've selected the 12 Gage Shotgun!");
       }
    
       if (pickedItem == 2)
       {
          Console.WriteLine("You've selected hand gun!");
       }

  9. #9
    Join Date
    Sep 2002
    Location
    So. Cal.
    Posts
    133
    This worked for me. I was trying to declare a new type int for each of my items, which of course wasn't working.

    Thanks everyone for your help!


    Quote Originally Posted by BazzaCAD View Post
    Try something like this....

    Code:
    int pickedItem;
        pickedItem = int.Parse(Console.ReadLine());
    
        if (pickedItem == 1)
       {
           Console.WriteLine("You've selected the 12 Gage Shotgun!");
       }
    
       if (pickedItem == 2)
       {
          Console.WriteLine("You've selected hand gun!");
       }

  10. #10
    Join Date
    Mar 2009
    Location
    Bellevue, WA
    Posts
    803
    What if statement is your current if statement shown above nested in? Have you tried setting a break point on the int.parse line shown above and stepping down to the next statement to see what value is currently in shotgun? You can also go to the watch window and type in shotgun and then watch the value of it change as you step through the code.
    " Imagination is the preview of life's coming attractions " - Albert Einstein
    " If you can't explain it simply, you don't understand it well enough." - Albert Einstein

    My Website: http://www.gamedevlounge.com/

    If any post is informative please provide positive feedback by clicking the star below.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •