Program ends unexpectedly

This is a discussion on Program ends unexpectedly within the C Programming forums, part of the General Programming Boards category; I have been staring at this program for school which is supposed to be a dummy atm machine and I ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    11

    Program ends unexpectedly

    I have been staring at this program for school which is supposed to be a dummy atm machine and I can't figure out why, but after the first run through of the program it closes automatically, right after you imput the second menu choice. Any help would be greatly appreciated.
    Code:
    #include <stdio.h>
    
    int menu(){
    int func_option;
    func_option=0;
       
        printf("Transaction options: \n");
        printf("1 - Deposit funds (credit transaction) \n");
        printf("2 - Withdraw funds (debit transaction) \n");
        printf("3 - Print statement of account \n");
        printf("4 - Compute interest and exit \n");
        printf("Please indicate your option: \t");
        scanf("%d",&func_option);
        printf("\n %d",func_option);
        system("pause");
        return func_option;
    }
    int main()
    {
    
    int option, ctran, dtran, ttran;
    float bal, change, minbal, interest, final;
        
    option=menu();
    bal=2000;
    minbal=2000;
    ctran=0;
    dtran=0;
    ttran=ctran+dtran;
    
    
    if(option==1){
          printf("\nYour current balance is:\t $%.2f \n How much do you want to deposit?\n", bal);
          scanf("%f", &change);
          bal=bal+change;
          ctran=ctran+1;
          printf("Your current balance is:\t $%.2f\n\n", bal);
          return menu();
    }
    if(option==2){
            printf("\nYour current balance is:\t $%.2f\n", bal);
            printf("How much do you want to withdraw?\n");
            scanf("%f", &change);
            bal=bal-change;
            if(minbal>=bal){
            minbal=bal;}
            dtran=dtran+1;
            printf("Your current balance is:\t $%.2f\n\n", bal);
            return menu();
    }
    if(option==3){
            printf("\n Current Balace:\t $%.2f \n", bal);
            printf("Minimum Balance:\t $%.2f \n", minbal);
            return menu();
    }
    if(option==4){
            interest=minbal*(.035/12);
            final=interest+bal;
            printf("Statement of account:\n");
            printf("Credit Transactions: \t %d \n", ctran);
            printf("Debit Transactions: \t %d \n", dtran);
            printf("Current Balance: \t $%.2f \n", bal);
            printf("Minimum Balance: \t $%.2f \n", minbal);
            printf("Interest Computed: \t $%.2f \n", interest);
            printf("Final Balance: \t\t $%.2f \n", final);
            printf("Good Bye!\n\n");
    }      
    
    
    system("pause");
    }

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    That's because you told it to,

    Code:
    if(option==2){
            printf("\nYour current balance is:\t $%.2f\n", bal);
            printf("How much do you want to withdraw?\n");
            scanf("%f", &change);
            bal=bal-change;
            if(minbal>=bal){
            minbal=bal;}
            dtran=dtran+1;
            printf("Your current balance is:\t $%.2f\n\n", bal);
            return menu();
    }
    Also you must return a value from main ALWAYS, if the option is not [1,4] then you'll return nothing, consider:
    Code:
        system("pause");
        return 0;
    }
    Also why are you returning the option # from your program?
    PS: Work on your indenting

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    11
    I told it to return menu() because I want the menu to return everytime it's done with running the fuctions for that menu choice. My problem is that the first run through goes fine, then the second time the menu appears with options, as soon as you give it an imput (1,4) it closes.

    I am printing the option number just to error check myself.

    I fixed the return 0; under main. And I'll work on my indentation

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    11
    It seems as if once func_option is stored and used once, when I go back to the menu to overwrite it again the thing just closes, no error or nothing. Thats what happens everytime with all the playing around Im doing. Is there something I am missing in the menu fuction to temporarily hold the value or something like that?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Need Delimiter Program helpful hints.
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 02-16-2002, 05:27 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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