473,320 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
3 3117
Banfa
9,065 Expert Mod 8TB
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
9,065 Expert Mod 8TB
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
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

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

Similar topics

6
by: PG | last post by:
When deleting a row from the database, that id is now missing. So what I'm trying to do is update in a loop (maybe an sql loop if there is one) of all of the id numbers - sort of like renaming...
28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
4
by: August1 | last post by:
A handful of articles have been posted requesting information on how to use these functions in addition to the time() function as the seed to generate unique groups (sets) of numbers - each group...
4
by: Japhy | last post by:
Hello, I'm am pulling data from a mysql db and want to use the data to populate a <ul. Here are relavent parts of my code : $wohdate = mysql_result($wohRS,$wohndx,woh_date); $woh_display...
1
by: Arash | last post by:
hi, I read couple of emails in the group regarding using the form to display a message (instead of MsgBox which needs user input to be closed). I'm not very familiar with forms, I would...
5
by: Atara | last post by:
I am trying to convert the following code to VB .Net, I still have some gaps (the lines that are marked with (*)) and also I need an ending condition for the while loop. any help would be...
1
by: nanoman | last post by:
Hello, I've got a Nested Set structure in MySQL 4 here with - id - lft - rgt - parent_id - root_id I wrote some test scripts and i discovered that the Nested Set (the
6
by: jackj | last post by:
Hi, I am first time C++ student and doing the usual tasks. This one is to create a triangle based on user input of how large (how many rows) and what symbol to use. I have managed to create a...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.