Thread: Error checking for homework

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    3

    Error checking for homework

    Hey guys, I'm new here. I'm taking an intro to C class and just finished an assignment, but I need a little help with error checking. There are 3 checks I need:

    1 - People trying to bet or sell more chips than they have
    2 - People trying to spend more money than they have
    3 - People trying to bet less than one chip

    This is my code, it's for a craps game:

    http://pastebin.com/m35adcf5b


    Any help as to where to place and how to write to error checks would be greatly appreciated!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Not going to click click click just to see the code.

    Basic idea: you've already written what the checks should look like. You should check for errors as soon as feasible (i.e., as soon as they try to place a bet you should check it makes sense).

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    3
    I tried doing this for error check #3, but it won't print the "Invalid..." statement nor go back to the menu. I haven't figured out the other 2 yet.

    Code:
    if(menu == 3)
                      {
                              
                              printf("How many chips do you want to bet?\n");
                              scanf("%d", &bet_chips);
    
                              do
                              {
                              scanf("%d", &bet_chips);
                              }while(bet_chips < 1);
                              printf("Invalid number of chips!\n");

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, look at what you've written: you will _always_ get input twice -- do-while loops are guaranteed to run -- and then once you have gotten valid input, you will then print out that you have an invalid number of chips. And notice that you never prompt for more input -- you'll just get a blinky cursor and your user has to know what to type.

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    3
    I'm confused. So what should the loop look like then?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. If you don't want the loop to always run, then you shouldn't use do-while.
    2. If you expect someone to give you input, you should provide a prompt so that they know
      1. what was wrong
      2. what you're asking them to re-enter
    3. There's more ways to be "wrong" then just to be negative -- to get past the gate, the input must be correct; if you check first for negative, then for betting too much, this will happen:
      Code:
      How many chips do you want to bet? -1
      Please enter a positive number of chips!
      How many chips do you want to bet? 1000
      You don't have that many chips!
      How many chips do you want to bet? -1
      Okay.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by drogahnus View Post
    I'm confused. So what should the loop look like then?
    A loop is merely logic.
    So take your logic that you want to do and translate it into code. That's the basic idea.
    In case you can't get it right, then do a flowchart first, or pseudo code and translate into real code.

    Another thing you might try is translating your current code into pseudo code / flowchart, and you should see why it's wrong.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    If your do-while loops sole purpose is to check user input, then you will need an if conditional placed before your do statement.

    Code:
    printf("How many chips do you want to bet?\n");
    scanf("%d", &bet_chips);
    
    if (bet_chips < 1 ) 
    {
         do
         {
             printf("Invalid number of chips!\n");
             printf("How many chips do you want to bet?\n");
             scanf("%d", &bet_chips);
          }while(bet_chips < 1);
    }
    I would place more error checking within your do-while though, as Tabstop mentioned.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    We should not need to tell others this kind of thing. This should be worked out by logical thinking!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed