Thread: Need help with a validation step in my program

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    Norwich, CT
    Posts
    5

    Need help with a validation step in my program

    I have this much compliled and running, but I am stuck on how to place a validation step in the event the user inputs something not within the required input ($10 denominations). For instance if I input 35 I wil get back one 20 and 1 10. I would like to be able to tell the user that ther input was invalid please reenter desired amount.
    Code:
    * This program will take input from the user of an ATM and provide the user with the number
    * of $50, $20 and $10 dollar bills the ATM will dispense based on the user's input. It will 
    * also prompt the user to reenter their amountif they have entered it incorrectly.*/ 
    
    #include <stdio.h>
    
    void bills_dispensed (int amount_entered)
    {
      int amount_remaining;	/* Holds on to the amount_remaining amount through each step of the calculations. */
    
      printf("\nYour amount of %d dollars is dispensed as follows.\n", amount_entered);
    
      /* Compute the total number of 50 dollar bills in the amount. */
      printf("50 dollar bills : %d\n", (amount_entered/50));
    
      /* Compute amount_remaining to be dispensed after dispensing 50 dollar bills. */
      amount_remaining = (amount_entered%50);
    
      printf("20 dollar bills : %d\n", (amount_remaining/20));
      amount_remaining = (amount_remaining%20);
    
      printf("10 dollar bills : %d\n", (amount_remaining/10));
    }
    
    int main ()
    {
      int amount_entered;
    
      printf("**************************************\n");
      printf("** Welcome to your neighborhood ATM **\n");
      printf("**************************************\n\n");
      printf("This ATM will only distribute $50, $20 and $10 denominations.\n\n");
      printf("Please enter the amount (multiple of 10 dollars)you wish to withdraw :$ ");
      scanf("%d", &amount_entered);
      bills_dispensed(amount_entered);
    
      return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Think about the problem for a bit. How should your amount_entered relate to 10? What symbol (which you use in your program!) gives you that information?

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Write a validation function that can be used like this:
    Code:
    printf("Please enter the amount (multiple of 10 dollars)you wish to withdraw :$ ");
    scanf("%d", &amount_entered);
    while (validation_func(amount_entered) != 0) {
        printf("Try again!");
        scanf("%d", &amount_entered);
    }
    bills_dispensed(amount_entered);
    So, validation_func() should return 0 if everything is okay (in which case, the while loop won't happen) and non zero if there is a problem. You could also use that returned value in a switch/case to flesh the problem out (eg, case 2: "That number is too high!").
    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
    Registered User
    Join Date
    Mar 2010
    Location
    Norwich, CT
    Posts
    5

    I ended up using this thank you for the help.

    Code:
     /* Validation step*/
    
      while (amount_entered %10) {
        printf("Try again, you have entered an incorrect amount! \n\n");
    	printf("Please enter the amount (multiple of 10 dollars) you wish to withdrawl : $");
        scanf("%d", &amount_entered);
      }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hi, Quiz C program Assignment updated
    By Eman in forum C Programming
    Replies: 19
    Last Post: 11-22-2009, 04:50 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM