Thread: I need to create a running balance once the user enters the value for savings, ect.

  1. #1
    Registered User
    Join Date
    Jan 2018
    Posts
    3

    I need to create a running balance once the user enters the value for savings, ect.

    I am having trouble creating the void functions and assigning the pointers so that I can have a running balance for the checking and savings balances.

    insert
    Code:
    #include <stdio.h>
    
    
    
    
    void add_acc();
    void deposit(savings, depositAmount);
    void withdrawal(savings, withDrawalAmount);
    void transfer(transfer, transFer, savings, checking, transferAmount);
    void acc_info();
    void transaction();
    
    
    int main()
    {
        int        choice;
        float    accountBalance, depositAmount =0, withDrawalAmount, savingsBalance, checkingBalance, transferAmount;
        char    Exit;
    
    
        double *savings, *checking;
    
    
        savings = &savingsBalance;
        checking = &checkingBalance;
    
    
    
    
        Exit = 'N';
    
    
        while (Exit = 'N')
        {
    
    
            printf("1. Create New Account\n");
            printf("2. Cash Deposit\n");
            printf("3. Cash Withdrawl\n");
            printf("4. Fund Transfer\n");
            printf("5. Account Information\n");
            printf("6. Transaction Information\n");
            printf("7. Exit\n");
            printf("\n");
            printf("Press a choice between the range of [1-7]");
            scanf_s("%d", &choice);
    
    
            switch (choice)
            {
            case 1:
                printf("Adding a new account is not possible at this time:\n ");
                scanf_s("%d", &choice);
                break;
    
    
            case 2:
                printf("Process a cash deposit.\n");
                printf("Enter the current account balance\n");
                scanf_s("%f", &savings);
                printf("Enter the amount of the deposit\n");
                scanf_s("%f", &depositAmount);
    
    
                printf("Your new balance is: $%.2f\n", deposit);
                scanf_s("%.2f", &savings);
                break;
    
    
            case 3:
                printf("Process a cash withdrawal.\n");
                printf("The current savings account balance is: %.2f \n", savings);
                printf("Enter the amount to be withdrawn\n");
                scanf_s("%f", &withDrawalAmount);
    
    
                printf("The new balance is: $%.2f\n", withdrawal);
                break;
    
    
            case 4:
                printf("Transfer funds from savings to checking.\n");
                printf("The current savings account balance is %.2f \n", savings);
                printf("Enter the current checking account balance\n");
                scanf_s("%f", &checkingBalance);
                printf("Enter the amount to be transferred\n");
                scanf_s("%f", &transferAmount);
    
    
                printf("The new savings account balance is: $%.2f\n", transfer);
    
    
                printf("\n");
    
    
                printf("The new checking account balance is: $%.2f\n", transFer);
                break;
    
    
            case 5:
                printf("Customer account balances.\n");
                printf("Your savings account balance is: $%.2f", savings);
                printf("Your checking account balance is: $%.2f", checking);
                break;
    
    
            case 6:
                printf("There is no transaction history availavle at this time.\n");
                scanf_s("%d", &choice);
                break;
    
    
            case 7:
                printf("Are you sure you want to exit, Y-yes:N-no:");
                scanf_s(" %c", &Exit);
                if (Exit == 'Y' || Exit == 'y')
                {
                    return 0;
                }
                break;
    
    
            default:
                printf("That wasn't 1-7... Please re-enter a number between (1-7)\n");
                break;
    
    
            }
        }
        return 0;
    }
    void deposit(double savings, float depositAmount)
    {
        return savings + depositAmount;
    }
    void withdrawal(double savings, float withDrawalAmount)
    {
        return savings - withDrawalAmount;
    }
    void transfer(transfer, transFer, savings, checking, transferAmount )
    {
        if (transfer)
        {
            return savings - transferAmount;
        }
        else (transFer);
        {
            return checking + transferAmount;
        }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Your function prototypes need types on the variables.

    Your pointers to double are pointing at floats. Are you getting any warnings?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2018
    Posts
    3
    Yes, I would get warnings about changing them to double. This code has so much wrong with it. It won't run. I stopped because I wasn't sure if I was using the prototypes and pointers the right way. In my transfer, I don't know how to set it up so that I can use both the subtraction and addition in the same function.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    A development process
    Start again, start small, compile and test often.

    There is no point trying to write a whole program all at once, then pressing "compile" - only to wonder at the sight of dozens of error messages you have no idea how to correct.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,112
    Plus, if you are returning data from the function, don't declare it as void.

    Code:
    double deposit(double savings, float depositAmount)
    {
        return savings + depositAmount;
    }
    "void" only if the function does NOT return a value! I didn't check for other errors. Start over with the suggestions @Salem made.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by rstanley View Post
    Plus, if you are returning data from the function, don't declare it as void.

    Code:
    double deposit(double savings, float depositAmount)
    {
        return savings + depositAmount;
    }
    "void" only if the function does NOT return a value! I didn't check for other errors. Start over with the suggestions @Salem made.
    The below is a prototype for the function above.
    Code:
    double deposit(double savings, float depositAmount);
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Jan 2018
    Posts
    3
    Okay, so I started back with my working program. How do I create a balance that is kept after the user enters it the first time? So at the first prompt where it asks to enter your account balance, I want that to be what I call on to add or subtract from and it stores the new value. I think I am just using the wrong syntax for pointers. Can someone please help?

    Code:
    #include<stdio.h>
    float add_acc();
    float deposit(float accountBalance, float depositAmount);
    float withdrawal(float accountBalance, float withDrawalAmount);
    float s_transfer(float savingsBalance, float transferAmount);
    float c_transfer(float checkingBalance, float transferAmount);
    
    
    
    
    int main()
    {
    	int		choice;
    	float	accountBalance, depositAmount, withDrawalAmount, savingsBalance, checkingBalance, transferAmount;
    	char	Exit;
    
    
    
    
    	Exit = 'N';
    
    
    	while (Exit = 'N')
    	{
    
    
    		printf("1. Create New Account\n");
    		printf("2. Cash Deposit\n");
    		printf("3. Cash Withdrawl\n");
    		printf("4. Fund Transfer\n");
    		printf("5. Account Information\n");
    		printf("6. Transaction Information\n");
    		printf("7. Exit\n");
    		printf("\n");
    		printf("Press a choice between the range of [1-7]");
    		scanf("%d", &choice);
    
    
    		switch (choice)
    		{
    		case 1:
    			printf("Adding a new account is not possible at this time:\n ");
    			scanf("%d", &choice);
    			break;
    
    
    		case 2:
    			printf("Process a cash deposit.\n");
    			printf("Enter the current account balance\n");
    			scanf("%f", &accountBalance);
    			printf("Enter the amount of the deposit\n");
    			scanf("%f", &depositAmount);
    
    
    			printf("Your new balance is: $%.2f\n", deposit(accountBalance, depositAmount));
    			break;
    
    
    		case 3:
    			printf("Process a cash withdrawal.\n");
    			printf("Enter the current account balance\n");
    			scanf("%f", &accountBalance);
    			printf("Enter the amount to be withdrawn\n");
    			scanf("%f", &withDrawalAmount);
    
    
    			printf("The new balance is: $%.2f\n", withdrawal(accountBalance, withDrawalAmount));
    			break;
    
    
    		case 4:
    			printf("Transfer funds from savings to checking.\n");
    			printf("Enter the current savings account balance\n");
    			scanf("%f", &savingsBalance);
    			printf("Enter the current checking account balance\n");
    			scanf("%f", &checkingBalance);
    			printf("Enter the amount to be transferred\n");
    			scanf("%f", &transferAmount);
    
    
    			printf("The new savings account balance is: $%.2f\n", s_transfer(savingsBalance, transferAmount));
    
    
    			printf("\n");
    
    
    			printf("The new checking account balance is: $%.2f\n", c_transfer (checkingBalance, transferAmount));
    			break;
    
    
    		case 5:
    			printf("Customer account holder information is unavailale at this time.\n");
    			scanf("%d", &choice);
    			break;
    
    
    		case 6:
    			printf("There is no transaction history availavle at this time.\n");
    			scanf("%d", &choice);
    			break;
    
    
    		case 7:
    			printf("Are you sure you want to exit, Y-yes:N-no:");
    			scanf(" %c", &Exit);
    			if (Exit == 'Y' || Exit == 'y')
    			{
    				return 0;
    			}
    			break;
    
    
    		default:
    			printf("That wasn't 1-7... Please re-enter a number between (1-7)\n");
    			break;
    
    
    		}
    	}
    	return 0;
    }
    
    
    float add_acc()
    {
    
    
    }
    float deposit(float accountBalance, float depositAmount)
    {
    	return accountBalance + depositAmount;
    }
    float withdrawal(float accountBalance, float withDrawalAmount)
    {
    	return accountBalance - withDrawalAmount;
    }
    float s_transfer(float savingsBalance, float transferAmount)
    {
    	return savingsBalance - transferAmount;
    }
    float c_transfer(float checkingBalance, float transferAmount)
    {
    	return checkingBalance + transferAmount;
    }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > while (Exit = 'N')
    There's a difference between = and ==

    > printf("Your new balance is: $%.2f\n", deposit(accountBalance, depositAmount));
    You print the new balance, but you don't update it.

    Say
    accountBalance = deposit(accountBalance, depositAmount));
    printf("Your new balance is: $%.2f\n", accountBalance);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. While-loop go on until user enters a 0. Different solutions.
    By DecoratorFawn82 in forum C++ Programming
    Replies: 3
    Last Post: 11-05-2015, 10:10 AM
  2. Keep looping until user enters a blank line?
    By gribbles in forum C++ Programming
    Replies: 2
    Last Post: 08-04-2013, 04:05 AM
  3. adding numbers that user enters as a single string
    By metalinspired in forum C++ Programming
    Replies: 10
    Last Post: 04-28-2006, 10:18 AM
  4. If user enters wrong information in scanf
    By Beast() in forum C Programming
    Replies: 23
    Last Post: 06-17-2004, 08:33 AM

Tags for this Thread