Author Topic: The programming help thread!  (Read 2195 times)

nat

  • Hero Member
  • *****
  • Posts: 7085
Re: The programming help thread!
« Reply #45 on: June 20, 2007, 04:41:32 PM »
Thanks guys. :) I'll be back when I have another problem. :wink:

Fine with me. But as far as I know, I'm only one person. My name isn't Legion. :)

offsidewing

  • Hero Member
  • *****
  • Posts: 631
Re: The programming help thread!
« Reply #46 on: June 25, 2007, 01:26:58 PM »
Thanks guys. :) I'll be back when I have another problem. :wink:

Fine with me. But as far as I know, I'm only one person. My name isn't Legion. :)

So that means you are not many?

GIVE US A KISS, DEAR!!

nat

  • Hero Member
  • *****
  • Posts: 7085
Re: The programming help thread!
« Reply #47 on: June 25, 2007, 01:30:23 PM »
So that means you are not many?

GIVE US A KISS, DEAR!!

Be careful what you wish for.

Dark Fact

  • Hero Member
  • *****
  • Posts: 1147
Re: The programming help thread!
« Reply #48 on: July 04, 2007, 01:45:38 PM »
Okay, back with another problem.

I've been stewing with this program for a whole day.  The question in the book asks me to write a whole bunch of functions for an accounting program.  One of them involves getting the opening balance to an account, the other involves displaying a menu, another involves validating a user's choice, and another that displays the final results.

The problem I'm having with the program is the accumulation of the total amounts of deposits, withdrawals, and checks appearing properly.  Now, the funny thing is, when I'm typing in values for the program and finally end it afterwards, sometimes the values appear correct but other times, they don't.  For example, suppose I do the following:

Opening Balance: $40000
Deposit: $1000
Deposit: $1000
Withdrawal: $250
Withdrawal: $100
Check: $50

And then I end the program which gives me the following totals:

OPENING BALANCE: $40000
TOTAL DEPOSIT: $2000
TOTAL WITHDRAWAL: $350
TOTAL CHECKS: $50
--------------------------
CLOSING BALANCE: $41600

Now here's the same program using different amounts:

Opening Balance: $40000
Deposit: $60000
Deposit: $50000
Withdrawal: $24000
Check: $15000
Deposit: $25000

Here's what I may get:

OPENING BALANCE: $40000
TOTAL DEPOSIT: $-120000
TOTAL WITHDRAWAL: $156301606014
TOTAL CHECKS: $68500
--------------------------
CLOSING BALANCE: $1258917250218000000000000000000

See? Sometimes, good amounts, sometimes bad amounts.  Question is...what do I do to prevent any of these garbage values from appearing at all? Is it a problem with my identifiers? By the way, my identifiers for my variables are all double.

Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

nat

  • Hero Member
  • *****
  • Posts: 7085
Re: The programming help thread!
« Reply #49 on: July 04, 2007, 03:54:09 PM »
First, if you're only dealing with whole dollars in this program change your data type for all your variables to long int, although it's probably not a big deal.

Next, without seeing your code, I'm going to guess you aren't initializing your variables. Depending how your program is set up this could make a big difference. I always believe it's good practice to initialize variables as you declare them regardless:

long int         nat_rules = 0,
                 dark_fact_rules = 0,
                 we_all_rule = 0;


Without seeing your code it's going to be tough debugging the program further.... Try the initialization thing and if that doesn't work, post up your code.
           

Dark Fact

  • Hero Member
  • *****
  • Posts: 1147
Re: The programming help thread!
« Reply #50 on: July 05, 2007, 04:22:57 AM »
I'm not using whole numbers in my program though.  I'm using real numbers.

I also initialized my totals for each category to 0.00 so that it has a value to start off with before accumulations.  However, this doesn't solve the problem as my program is still giving me crap values.

Here's the code to make it more easier:

Code: [Select]
/* Prob 10-3
*  Created by Yuriy Mokriy
*/

#include <stdio.h>
#include <stdlib.h>

double Get_Opening_Balance();                                 /* Gets the user's opening account balance */
void Choice_Menu();                                           /* Displays a menu for choices */
void Validate_Choice(double);                                 /* Validates the user's choice from the menu */
double Deposit(double, double);                               /* Gets the user's deposit amount */
double Withdrawal(double, double);                            /* Gets the user's withdrawal amount */
double Check(double, double);                                 /* Gets the user's check amount */
void Display_Results(double, double, double, double, double); /* Displays all results and totals */

main()
{   
      double open_balance_amount;            /* The user's opening account balance */
     
      /* Call functions */
     
      open_balance_amount = Get_Opening_Balance();
      Choice_Menu();     
      Validate_Choice(open_balance_amount);
}

double Get_Opening_Balance()
{
      double open_acct_balance;              /* The user's opening account balance */
     
      /* Prompt the user */
     
      printf("\nEnter the opening account balance: $");
      scanf("%lf", &open_acct_balance);
     
      if (open_acct_balance < 0.00)
         exit(0);
      else
         return open_acct_balance;     
}

void Choice_Menu()
{
      /* Display menu */
     
      printf("\nSelect your choice: ");
      printf("\n(D)Deposit");
      printf("\n(C)Check");
      printf("\n(W)Withdrawal");
      printf("\n(Q)Quit"); 
      printf("\n"); 
}

void Validate_Choice(double opening_balance_amt)
{
      char choice;                           /* The choice entered by the user */
      double total_deposit_amt,              /* The total amount deposited */
             total_withdrawal_amt,           /* The total amount withdrawn */
             deposited_amt,                  /* The amount deposited */
             withdrawn_amt,                  /* The amount withdrawn */
             total_check_amt,                /* The total check amount */
             check_amt,                      /* The check amount */
             account_balance,                /* The current account balance */
             closing_balance_amt;            /* The closing balance amount */
             
      /* Initialization */ 
       
      total_deposit_amt = 0.00;
      total_withdrawal_amt = 0.00;
      total_check_amt = 0.00;
      account_balance = 0.00;

      account_balance += opening_balance_amt;
         
      /* Get choice */
     
      getchar();
      choice = getchar();
     
      /* Validate choice */
     
      while (choice != 'q' || choice != 'Q')
      {
       if (choice == 'd' || choice == 'D')
       {
          account_balance = Deposit(deposited_amt, account_balance);
          total_deposit_amt = account_balance - opening_balance_amt - total_withdrawal_amt - total_check_amt;
       }
       else if (choice == 'w' || choice == 'W')
       {
          account_balance = Withdrawal(withdrawn_amt, account_balance);
          total_withdrawal_amt = -(account_balance - opening_balance_amt - total_deposit_amt - total_check_amt);
       }
       else if (choice == 'c' || choice == 'C')
       {
          account_balance = Check(check_amt, account_balance);
          total_check_amt = -(account_balance - opening_balance_amt - total_deposit_amt - total_withdrawal_amt);
       }
       else if (choice == 'q' || choice == 'Q')
       {
         closing_balance_amt = account_balance;
         Display_Results(opening_balance_amt, total_deposit_amt, total_withdrawal_amt, total_check_amt, closing_balance_amt);
         exit(0);
       } 
       else
         printf("\nError! You must enter the following choices listed on the menu.");
         
         Choice_Menu();
         getchar();
         choice = getchar();
      }       
}

double Deposit(double deposit_amount, double pres_account)
{         
      /* Prompt the user */
     
      printf("\nEnter the amount to be deposited: $");
      scanf("%lf", &deposit_amount);
     
      pres_account += deposit_amount; 
     
      return pres_account;
}

double Withdrawal(double withdrawal_amount, double present_account_balance)
{   
      /* Prompt the user */
     
      printf("\nEnter the amount to be withdrawn: $");
      scanf("%lf", &withdrawal_amount);
     
      if (withdrawal_amount > present_account_balance)
      {
        printf("\nWarning! The amount you have withdrawn exceeds your current account balance.\n");
        exit(1);
      }
     
      else
      {
        present_account_balance -= withdrawal_amount;
        return present_account_balance;
      }
}

double Check(double check_amount, double curr_account)
{       
      /* Prompt the user */
     
      printf("\nEnter the check amount $");
      scanf("%lf", &check_amount);
     
      if (check_amount > curr_account)
      {
        printf("\nWarning! The check amount exceeds your current account balance.\n");
        exit(1);
      }
     
      else
      {
        curr_account -= check_amount;
        return curr_account; 
      }
}

void Display_Results(double opening_balance, double total_deposit, double total_withdrawal, double total_check, double closing_balance)
{
      /* Display the results */
     
      printf("\nOPENING BALANCE:        $%.2lf", opening_balance);
      printf("\nTOTAL AMOUNT DEPOSITED: $%.2lf", total_deposit);   
      printf("\nTOTAL AMOUNT WITHDRAWN: $%.2lf", total_withdrawal);
      printf("\nTOTAL CHECK AMOUNT:     $%.2lf", total_check);
      printf("\n----------------------------------");
      printf("\nCLOSING BALANCE:        $%.2lf", closing_balance);
      printf("\n");
}

Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

Dark Fact

  • Hero Member
  • *****
  • Posts: 1147
Re: The programming help thread!
« Reply #51 on: July 07, 2007, 07:18:57 AM »
So, any luck?

Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

nat

  • Hero Member
  • *****
  • Posts: 7085
Re: The programming help thread!
« Reply #52 on: July 07, 2007, 09:17:24 AM »
Sorry, been busy with work so I haven't glanced at your code yet. I DO have a real job M-F. ;)

Today is Saturday so I'll take a look a little bit later and get back to you.

Dark Fact

  • Hero Member
  • *****
  • Posts: 1147
Re: The programming help thread!
« Reply #53 on: July 07, 2007, 09:47:40 AM »
Ok, no problemo. :)

Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

Dark Fact

  • Hero Member
  • *****
  • Posts: 1147
Re: The programming help thread!
« Reply #54 on: July 10, 2007, 04:50:11 PM »
Hey nat, I've already found someone else to look at the problem for me.  The program is working fine now so you don't have to worry about it. :)

Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

nat

  • Hero Member
  • *****
  • Posts: 7085
Re: The programming help thread!
« Reply #55 on: July 10, 2007, 05:02:48 PM »
sorry dude, I know I'm lame. I still haven't even looked at it yet. :) I'll get to it next time, I promise. But it always helps to post the code right away with your problem, cuz usually without code I can only speculate at what's going on.

What was the problem, BTW?
« Last Edit: July 10, 2007, 05:06:46 PM by nat »

Bonknuts

  • Hero Member
  • *****
  • Posts: 3292
Re: The programming help thread!
« Reply #56 on: July 11, 2007, 02:53:46 AM »
First, if you're only dealing with whole dollars in this program change your data type for all your variables to long int, although it's probably not a big deal.

Next, without seeing your code, I'm going to guess you aren't initializing your variables. Depending how your program is set up this could make a big difference. I always believe it's good practice to initialize variables as you declare them regardless:

long int         nat_rules = 0,
                 dark_fact_rules = 0,
                 we_all_rule = 0;


Without seeing your code it's going to be tough debugging the program further.... Try the initialization thing and if that doesn't work, post up your code.

Funny, I've heard the argument for not initializing your variables on declaration - for debugging reasons.


nat

  • Hero Member
  • *****
  • Posts: 7085
Re: The programming help thread!
« Reply #57 on: July 11, 2007, 03:36:47 AM »

Funny, I've heard the argument for not initializing your variables on declaration - for debugging reasons.

Well, you're probably a much more serious/experienced programmer than I am.

I'm sure there are merits to both "philosophies" but in my experience I've found it beneficial to initialize variables on declaration more often than not. As with everything, there are exceptions to the rule....

Bonknuts

  • Hero Member
  • *****
  • Posts: 3292
Re: The programming help thread!
« Reply #58 on: July 11, 2007, 05:52:49 PM »

Quote
I'm sure there are merits to both "philosophies" but in my experience I've found it beneficial to initialize variables on declaration more often than not. As with everything, there are exceptions to the rule....

 I totally agree. I had an associate once tell me there only one way to do things - insinuating that there is only one right way. I nodded my head, but laughed to myself. I lost *all* respect for him after that  :mrgreen:

 I always initialized my variables in declaration in the past. I just came across that article recently.

 I'm still "amateur hour" in C - ASM's my strong point.




guyjin

  • Hero Member
  • *****
  • Posts: 3896
Re: The programming help thread!
« Reply #59 on: July 12, 2007, 02:17:42 AM »

 I totally agree. I had an associate once tell me there only one way to do things - insinuating that there is only one right way.


Yeah, pretty much anyone who says this, or anything like it, is a tool and can safely be ignored.*

*(unless you're dealing with potentially fatal stuff, then it's okay sometimes)
"Fun is a strong word." - SNK
"Today, people do all kind of shit." - Tatsujin