Looping problem!!

This is a discussion on Looping problem!! within the C Programming forums, part of the General Programming Boards category; OK here is what I have so far. Code: #include<stdio.h> #include<math.h> #define PIN 2424 #define TRUE 1 /* Function Prototypes ...

  1. #1
    bignood
    Join Date
    Sep 2005
    Posts
    33

    Looping problem!!

    OK here is what I have so far.

    Code:
    #include<stdio.h>
    #include<math.h>
    #define PIN 2424
    #define TRUE 1
    
    /* Function Prototypes */
    void bank_logo(void);
    
    void validate_pin(void);
    
    int get_data(int money);
    
    int get_balance(double balance, int bal_cash);
    
    void fifties_twenties_tens(int tot_bal, int *fifty, int *twenty, int *ten);
    
    int main(void)
    {
    double open_bal;
    int cash;
    int new_cash;
    int money;
    int balance_cash;
    int fif;
    int twen;
    int tens;
    int remaining_balance;
    
    bank_logo();
    validate_pin();
    
    printf("Enter the opening balance of your account  $");
    scanf("%lf", &open_bal);
     printf("Enter how much cash you need?  $");
    scanf("%d", &cash);
    
     balance_cash = get_data(cash);
    printf("\nThe amount you are withdrawing =  %d\n", balance_cash);
    
    remaining_balance = get_balance(open_bal, balance_cash);
    printf("Remaining Balance in your account = %d\n\n", remaining_balance);   
    printf("    *** Take your cash as $$$ bills ***    \n\n");
    
    fifties_twenties_tens(balance_cash, &fif, &twen, &tens);
    
    printf(" %5d fifty %5d twenties and %5d tens\n\n", fif, twen, tens);
    /*
    printf(" %5d fifty %5d twenties and %5d tens", fifties_twenties_tens(balance_cash, &fif, &twen, &tens);
    */
    return(0);
    }
    
    void bank_logo(void)  
    {
    
    printf("$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
    printf("$                        $\n");
    printf("$  BankOne Money Access  $\n");
    printf("$                        $\n");
    printf("$       24 hours         $\n");
    printf("$    7 days a week       $\n");
    printf("$   All year long!!!     $\n");
    printf("$                        $\n");
    printf("$$$$$$$$$$$$$$$$$$$$$$$$$$\n\n\n");
    
    }
    
    void validate_pin(void)
    {
    int id; /* input of user's PIN number */
    int count;
    
    for (count = 0; count < 3; ++count){
    
    printf("Enter your 4-digit personal identification (PIN) >>  ");
    scanf("%d", &id);
            if (id != PIN)
            printf("Sorry! Invalid PIN number !\n");
            else if (count == 3)
            printf("\nYour chances are up.  Better luck next time.\n\n");
            else
            break;
           }
    /*if (count == 3)
            printf("\nYour chances are up.  Better luck next time.\n\n");*/
    
    }
    
    
    int get_data(int money)
    {
      while( TRUE )
      {
        /* test but dont change money */
        if(money % 10 == 0)
            {
              return money;
        }else{
          printf("\n\nThis machine dispenses ONLY $50, $20, $10...\n\n");
          printf("Please enter again:> ");
          scanf("%d", &money);
        }
      }
    }
            
    int get_balance(double balance, int bal_cash)
    {
    double final_bal;
            
    final_bal = balance - bal_cash;
    return final_bal;
    }
    
    void fifties_twenties_tens(int tot_bal, int *fifty, int *twenty, int *ten)
    {
     
    *fifty = tot_bal / 50;
    int rem_twenty = tot_bal % 50;
    *twenty = rem_twenty / 20;
    int rem_ten = rem_twenty % 20;
    *ten = rem_ten / 10;
              
    /*
    *fifty = 50;
    *twenty = 20;
    *ten = 10;
    */   
    }
    and here is a sample output so far
    Code:
    Enter your 4-digit personal identification (PIN) >>  2424
    Enter the opening balance of your account  $500
    Enter how much cash you need?  $60
    
    The amount you are withdrawing =  60
    Remaining Balance in your account = 440
    
        *** Take your cash as $$$ bills ***    
    
         1 fifty     0 twenties and     1 tens
    What I want to do now is to make a loop so that it asks if the user wants another transaction, and if they enter yes, it should ask how much cash they need. And then to subtract that from the remaining balance (In the example would be 440). And that at the end I want it to print out how many transactions there were.

    Any help is appreciated and thanks in advance!

  2. #2
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Just saying, but if you actually wrote the code above, you probably would be able to loop it. If not then try using a while loop.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  3. #3
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Code:
    int tester
    
    while(tester)
    {
           your code 
           printf("Do you want another transaction ?");
           Scan for user input 
           Change tester accordingly 
    }
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  4. #4
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Well how about where this code is in your above post:
    Code:
    printf("Enter the opening balance of your account  $");
    scanf("%lf", &open_bal);
     printf("Enter how much cash you need?  $");
    scanf("%d", &cash);
    Maybe add this while loop around the whole part you want repeated so something like this:
    Code:
    char exit[256];
    while(strcmp(exit,"exit")!=0)
    {
    //Put some kind of check here to see if user wants to continue
    printf("Enter the opening balance of your account  $");
    scanf("%lf", &open_bal);
     printf("Enter how much cash you need?  $");
    scanf("%d", &cash);
    //rest of code you want included
    }
    So now if the user wants to quit they can type exit, and if they don't it will keep looping until they do.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  2. looping problem
    By chris285 in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2005, 11:03 AM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. Looping problem
    By sketchit in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 02:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21