not sure how to fix this?

This is a discussion on not sure how to fix this? within the C Programming forums, part of the General Programming Boards category; I have written this program for an assignment and I have told the program to count the number of deposits ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    8

    Question not sure how to fix this?

    I have written this program for an assignment and I have told the program to count the number of deposits and withdrawals then print them when the user is finished. My problem is that it also counts when a deposit = 0 or a withdrawal = 0. I have an idea but I dont know how to write it, maybe something like this?

    if (deposit == 0)
    then disregard count

    Can someone give me some advice please


    /* this program prompts the user to enter a starting balance, /
    then prompts the user to enter a deposit amount, /
    then prompts for a withdrawal amount, /
    then gives the user an option to continue or exit the program, /
    if input continue loops again, if input exit prints total deposit, withdrawals /
    and end balance including %1 applied to all transactions */

    #include <stdio.h>
    #include <conio.h>

    void main(void)
    {
    float tax, balance[2], deposit, withdraw, total[2] = {0, 0};
    char answer;
    int count[2] = {0, 0};

    clrscr();

    printf("This program will calculate the closing account balance based\n");
    printf("on your inputs. On exit it will give you a total of the\n");
    printf("deposits, withdrawals, tax and closing balance.\n\n");

    printf("Please enter your opening account balance$)");
    scanf("%f", &balance[0]);
    fflush(stdin);
    clrscr();

    do
    {
    printf("Enter a deposit amount$)");
    scanf("%f", &deposit);
    fflush(stdin);
    total[0] = total[0] + deposit; /* accept deposit and add to deposit total */
    count[0] = count[0] + 1; /* count number of deposits */

    printf("Enter a withdrawal amount$)");
    scanf("%f", &withdraw);
    fflush(stdin);
    total[1] = total[1] + withdraw; /* accept withdrawal and add to withdrwal total */
    count[1] = count[1] + 1; /* count number of withdrawals */
    clrscr();

    printf("Your deposit is equal to $%0.2f\n", deposit);
    printf("Your withdrawal is equal to $%0.2f\n", withdraw);

    printf("\nHave you finished entering deposits or withdrawals?(y/n)\n");
    scanf("%c", &answer);
    fflush(stdin);
    }
    while (answer == 'n' || answer == 'N');

    if (answer == 'y' || answer == 'Y')
    clrscr();
    {
    printf("\nYour opening balance was $%0.2f\n", balance[0]);
    printf("\nThe number of deposits are %d and total amount is $%0.2f\n", count[0], total[0]);
    printf("\nThe number of withdrawals are %d and total amount is $%0.2f\n", count[1], total[1]);

    tax = (total[0] * 0.01) + (total[1] * 0.01); /* calculate tax */
    printf("\nThe total goverment tax at 1 percent per transaction is $%2.2f\n", tax);

    balance[1] = balance[0] + total[0] - total[1] - tax; /* calculate closing balance */
    printf("\nYour closing balance is $%0.2f\n", balance[1]);

    if (balance[1] < 0)
    printf("Easy on the withdrawals, your account is not looking so good :-(\n");
    else
    printf("Keep those deposits coming! :-)\n");

    printf("\n\n\nThanks for using my program, See ya later!\n");
    printf("Brought to you by ZeD\n");
    }

    scanf("%c");
    fflush(stdin);
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Well, the easiest way is this:

    Instead of

    count[0] = count[0] + 1;

    write:

    if (deposit != 0) count[0] = count[0] + 1;

    And of course the analogous expression for withdrawls. Basically, you only increment if deposit is nonzero.

    BTW, you can also use:

    count[0]++;

    instead of

    count[0] = count[0] + 1;

    It's a very very common C/C++ shorthand for incrementing by one -- in fact, that's where the name C++ comes from -- it's an increment above C.

    If you need to make your code handle all kinds of erroneous input, you may consider checking if they enter a negative number. Sometimes professors like to see what happens if your program is given deliberately bad input.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    8
    Thanks for the help, it works like a dream

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. compiling my code, i can't fix error
    By howru in forum C Programming
    Replies: 2
    Last Post: 04-22-2008, 04:38 AM
  3. C++ code need help to fix
    By McReal in forum C++ Programming
    Replies: 9
    Last Post: 05-12-2007, 03:48 PM
  4. Help me fix my mess!!!!
    By Starr in forum C++ Programming
    Replies: 35
    Last Post: 02-01-2006, 03:40 PM
  5. My new FIX IT game.......:)
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-18-2002, 02:58 PM

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