By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,732 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

Using i++ and and a while lopp to create a counter.

P: 2
Hi,
I'm new to this boards, and I need some help finishing a project for school.
Im using nested if-statements inside a while loop to execute different actions. action C consist of printing the number of times a person executed action A and action B. For that a declared variables intCounterB and intCounterC, and included the intCounterB=++intCounterB to achieve my desired results, but I ended up getting some funny numbers.

The code I use is the following.
____________________________________

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main (void) {
  4.  
  5.     int intOption;
  6.     float floatTotal=2000;
  7.  
  8.     printf("\n");
  9.     printf("Ficticious Bank of American\n");
  10.     printf("\n");
  11.     printf(" 1) Deposit funds (credit transaction)\n");
  12.     printf(" 2) Withdraw funds (debit transaction)\n");
  13.     printf(" 3) Print statement of account\n");
  14.     printf(" 4) Compute interest on last day and exit the program\n");
  15.  
  16.     while (intOption<4) { 
  17.         int intCounterCredit, intCounterDebit;
  18.  
  19.         printf("\n");
  20.         printf(" Please select an option:  ");
  21.         scanf("%d", &intOption);
  22.         printf(" you selected option %d\n", intOption);
  23.  
  24.         if (intOption == 1){
  25.             float floatCredit;
  26.             int intDate, intCounterCredit;
  27.             intCounterCredit=++intCounterCredit;
  28.  
  29.             printf("\n");
  30.             printf(" Please enter today's date: ");
  31.             scanf("%d", &intDate);
  32.             printf(" Amount to be credited: ");
  33.             scanf("%f", &floatCredit);
  34.  
  35.             floatTotal=floatTotal+floatCredit;
  36.             printf(" Your new account blances is $%.2f\n", floatTotal);
  37.             }
  38.             else
  39.             {
  40.             if (intOption == 2){
  41.                 float floatDebit;
  42.                 int intDate;
  43.                 intCounterDebit=++intCounterDebit;
  44.  
  45.                 printf("\n");
  46.                 printf(" Please enter today's date: ");
  47.                 scanf("%d", &intDate);
  48.                 printf(" Amount to be dedited: ");
  49.                 scanf("%f", &floatDebit);
  50.  
  51.                 floatTotal=floatTotal-floatDebit;
  52.                 printf(" Your new account blances is $%.2f\n", floatTotal);
  53.                 }
  54.                 else
  55.                 {
  56.                     if (intOption == 3){
  57.                     int intDate;
  58.  
  59.                     printf("\n");
  60.                     printf(" Please enter today's date: ");
  61.                     scanf("%d", &intDate);
  62.                     printf(" Your new account blances is $%.2f\n", floatTotal);
  63.                     printf(" Total number of credit transactions: %d\n", intCounterCredit);
  64.                     printf(" Total number of debit transactions: %d\n", intCounterDebit);
  65.                     }
  66.                     else
  67.                     {
  68.                     printf("FU");
  69.                     }
  70.                 }
  71.             }
  72.         }
  73.  
  74.   return 0;
  75. }
  76.  
_____________________________________

The results were as follow after running it.

Ficticious Bank of American

1) Deposit funds (credit transaction)
2) Withdraw funds (debit transaction)
3) Print statement of account
4) Compute interest on last day and exit the program

Please select an option: 1
you selected option 1

Please enter today's date: 15
Amount to be credited: 200
Your new account blances is $2200.00

Please select an option: 1
you selected option 1

Please enter today's date: 19
Amount to be credited: 200
Your new account blances is $2400.00

Please select an option: 2
you selected option 2

Please enter today's date: 19
Amount to be dedited: 650
Your new account blances is $1750.00

Please select an option: 3
you selected option 3

Please enter today's date: 19
Your new account blances is $1750.00
Total number of credit transactions: -16843009
Total number of debit transactions: 43197

Please select an option:
___________________________________________

The problem is with the number of transactions printed.
Its supposed to be 1 and 2.


Any help is deeply appreciated, since I have to turn this project tomorrow morning.
Sep 22 '06 #1
Share this Question
Share on Google+
3 Replies


Banfa
Expert Mod 5K+
P: 8,916
At least 1 problem is with this line of code
This is wrong it is just very poor style.

intCounterDebit=++intCounterDebit;

This produces undefined behaviour because you are trying to access intCounterDebit too any times in the same statement.
Also wrong this would be bad and introduce undefined behavior intCounterDebit=++intCounterDebit+intCounterDebit+ +; I think what you have is just poor style. Please read on.

Think about what this says,

increment the value of ++intCounterDebit, then assign it to intCounterDebit. This is not a very sensible thing to do.

Assuming that you want to increment the value of intCounterDebit then you can do it inany of the following ways

++intCounterDebit;
intCounterDebit;
intCounterDebit += 1;
intCounterDebit = intCounterDebit + 1;

These 4 statements all have the same effect, increment the value of intCounterDebit by 1.
Sep 22 '06 #2

Banfa
Expert Mod 5K+
P: 8,916
The actual problem is that you declare

int intCounterCredit, intCounterDebit;

but you never initialise them so that have the value of whatever was last on the stack. Also since you declare them inside the while loo you are going to have trouble initialising them just once.

Move

int intCounterCredit, intCounterDebit;

outside the while loop and initialise both variables to 0.
Sep 22 '06 #3

P: 2
Thanks for the help.

I just tried what you said and now I'm getting "0" for both variables no matter how many times the loop runs.
Sep 22 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.