calculation error
-
hello, i am having a problem with this code. I do not know what is wrong. I enter the begbal, the ourdep and the withdraw. But in the end when goes to get the final balance. The answer is always the same as the input of begin bal. Any help would be greatful.
#include <stdio.h>
#define SENTINEL 0
float endbal,
finbal;
int date,
outdep,
withdraw;int main (void)
{
printf("BANK RECONCILIATION FOR: 10/21/98"); printf("\\n\\nEnding balance on statement:"); scanf ("%f", & endbal); printf("Enter Outstanding dep (or %d to quit)>", SENTINEL); scanf ("%d", & outdep);
while (outdep!= SENTINEL)
{
finbal = finbal + outdep;
printf("Enter outstanding dep (%d to quit)>", SENTINEL);
scanf("%f", & outdep);}
printf("Outstanding check/withdrawal (or %d to quit)>", SENTINEL);
scanf ("%d", & withdraw);while (withdraw!=SENTINEL)
{
finbal = finbal - withdraw;
printf("Enter withdrawal (%d to quit)>", SENTINEL);
scanf("%f", & withdraw);} finbal = endbal + outdep - withdraw; printf("Your final balance in your checkbook should be $ %f" , finbal); return 0;
}
-
hello, i am having a problem with this code. I do not know what is wrong. I enter the begbal, the ourdep and the withdraw. But in the end when goes to get the final balance. The answer is always the same as the input of begin bal. Any help would be greatful.
#include <stdio.h>
#define SENTINEL 0
float endbal,
finbal;
int date,
outdep,
withdraw;int main (void)
{
printf("BANK RECONCILIATION FOR: 10/21/98"); printf("\\n\\nEnding balance on statement:"); scanf ("%f", & endbal); printf("Enter Outstanding dep (or %d to quit)>", SENTINEL); scanf ("%d", & outdep);
while (outdep!= SENTINEL)
{
finbal = finbal + outdep;
printf("Enter outstanding dep (%d to quit)>", SENTINEL);
scanf("%f", & outdep);}
printf("Outstanding check/withdrawal (or %d to quit)>", SENTINEL);
scanf ("%d", & withdraw);while (withdraw!=SENTINEL)
{
finbal = finbal - withdraw;
printf("Enter withdrawal (%d to quit)>", SENTINEL);
scanf("%f", & withdraw);} finbal = endbal + outdep - withdraw; printf("Your final balance in your checkbook should be $ %f" , finbal); return 0;
}
Change
kbury wrote:
printf("\n\nEnding balance on statement:"); scanf ("%f", & endbal);
to
printf("\n\nEnding balance on statement:");
scanf ("%f", & finbal);and then remove the following line
kbury wrote:
finbal = endbal + outdep - withdraw;
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Change
kbury wrote:
printf("\n\nEnding balance on statement:"); scanf ("%f", & endbal);
to
printf("\n\nEnding balance on statement:");
scanf ("%f", & finbal);and then remove the following line
kbury wrote:
finbal = endbal + outdep - withdraw;
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]I made the changes and did the execute, this is what came out. still not the right answer. I do not know what else to do with this. I have tried so many things. BANK RECONCILIATION FOR: 10/21/98 Ending balance on statement:20 Enter outstanding dep (0 to quit)>10 Enter outstanding dep (0 to quit)>10 Enter outstanding dep (0 to quit)>10 Enter outstanding dep (0 to quit)>0 Enter outstanding dep (0 to quit)>0 Outstanding check/withdrawal (or 0 to quit)>10 Enter withdrawal (0 to quit)>10 Enter withdrawal (0 to quit)>0 Your final balance in your checkbook should be $ 2185232384.000000 "c:\cop2000\a5\lcc2\score.exe" Return code 0 Execution time 12.438 seconds Press any key to continue...
-
I made the changes and did the execute, this is what came out. still not the right answer. I do not know what else to do with this. I have tried so many things. BANK RECONCILIATION FOR: 10/21/98 Ending balance on statement:20 Enter outstanding dep (0 to quit)>10 Enter outstanding dep (0 to quit)>10 Enter outstanding dep (0 to quit)>10 Enter outstanding dep (0 to quit)>0 Enter outstanding dep (0 to quit)>0 Outstanding check/withdrawal (or 0 to quit)>10 Enter withdrawal (0 to quit)>10 Enter withdrawal (0 to quit)>0 Your final balance in your checkbook should be $ 2185232384.000000 "c:\cop2000\a5\lcc2\score.exe" Return code 0 Execution time 12.438 seconds Press any key to continue...
Well, I didn't notice you made a bad mix of floats and ints. Try
#define SENTINEL 0
int endbal,
finbal,
date,
outdep,
withdraw;#include <stdio.h>
int main (void){
printf("BANK RECONCILIATION FOR: 10/21/98"); printf("\\n\\nEnding balance on statement:"); scanf ("%d", & finbal); printf("Enter Outstanding dep (or %d to quit)>", SENTINEL); scanf ("%d", & outdep); while (outdep!= SENTINEL) { finbal = finbal + outdep; printf("Enter outstanding dep (%d to quit)>", SENTINEL); scanf("%d", & outdep); } printf("Outstanding check/withdrawal (or %d to quit)>", SENTINEL); scanf ("%d", & withdraw); while (withdraw!=SENTINEL) { finbal = finbal - withdraw; printf("Enter withdrawal (%d to quit)>", SENTINEL); scanf("%d", & withdraw); } printf("Your final balance in your checkbook should be $ %d" , finbal); return 0;
}
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
hello, i am having a problem with this code. I do not know what is wrong. I enter the begbal, the ourdep and the withdraw. But in the end when goes to get the final balance. The answer is always the same as the input of begin bal. Any help would be greatful.
#include <stdio.h>
#define SENTINEL 0
float endbal,
finbal;
int date,
outdep,
withdraw;int main (void)
{
printf("BANK RECONCILIATION FOR: 10/21/98"); printf("\\n\\nEnding balance on statement:"); scanf ("%f", & endbal); printf("Enter Outstanding dep (or %d to quit)>", SENTINEL); scanf ("%d", & outdep);
while (outdep!= SENTINEL)
{
finbal = finbal + outdep;
printf("Enter outstanding dep (%d to quit)>", SENTINEL);
scanf("%f", & outdep);}
printf("Outstanding check/withdrawal (or %d to quit)>", SENTINEL);
scanf ("%d", & withdraw);while (withdraw!=SENTINEL)
{
finbal = finbal - withdraw;
printf("Enter withdrawal (%d to quit)>", SENTINEL);
scanf("%f", & withdraw);} finbal = endbal + outdep - withdraw; printf("Your final balance in your checkbook should be $ %f" , finbal); return 0;
}
kbury wrote:
finbal = finbal + outdep;
finbal
is an r-value which has not been properly initialized."Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
hello, i am having a problem with this code. I do not know what is wrong. I enter the begbal, the ourdep and the withdraw. But in the end when goes to get the final balance. The answer is always the same as the input of begin bal. Any help would be greatful.
#include <stdio.h>
#define SENTINEL 0
float endbal,
finbal;
int date,
outdep,
withdraw;int main (void)
{
printf("BANK RECONCILIATION FOR: 10/21/98"); printf("\\n\\nEnding balance on statement:"); scanf ("%f", & endbal); printf("Enter Outstanding dep (or %d to quit)>", SENTINEL); scanf ("%d", & outdep);
while (outdep!= SENTINEL)
{
finbal = finbal + outdep;
printf("Enter outstanding dep (%d to quit)>", SENTINEL);
scanf("%f", & outdep);}
printf("Outstanding check/withdrawal (or %d to quit)>", SENTINEL);
scanf ("%d", & withdraw);while (withdraw!=SENTINEL)
{
finbal = finbal - withdraw;
printf("Enter withdrawal (%d to quit)>", SENTINEL);
scanf("%f", & withdraw);} finbal = endbal + outdep - withdraw; printf("Your final balance in your checkbook should be $ %f" , finbal); return 0;
}
None of the values have been initialized and it always causes problem as the programs works on a random garbage value. 1.
endbal
is not initialized. Either set it to 0 or some other positive value at the start of your program. 2.finbal
must be set to 0 at the start of main() function. You can initialize these at the time of declaration :float endbal = 0, finbal = 0;
Also the statementfinbal = endbal + outdep - withdraw;
is wrong. You have already added deposit amount and subtracted withdrawn amount in the variablefinbal
. So all you need isendbal = endbal + finbal
. Finally, you are displaying the wrong value. You should displayendbal
in the lastprintf
statement. I hope this helps you.modified on Wednesday, October 14, 2009 6:59 PM
-
hello, i am having a problem with this code. I do not know what is wrong. I enter the begbal, the ourdep and the withdraw. But in the end when goes to get the final balance. The answer is always the same as the input of begin bal. Any help would be greatful.
#include <stdio.h>
#define SENTINEL 0
float endbal,
finbal;
int date,
outdep,
withdraw;int main (void)
{
printf("BANK RECONCILIATION FOR: 10/21/98"); printf("\\n\\nEnding balance on statement:"); scanf ("%f", & endbal); printf("Enter Outstanding dep (or %d to quit)>", SENTINEL); scanf ("%d", & outdep);
while (outdep!= SENTINEL)
{
finbal = finbal + outdep;
printf("Enter outstanding dep (%d to quit)>", SENTINEL);
scanf("%f", & outdep);}
printf("Outstanding check/withdrawal (or %d to quit)>", SENTINEL);
scanf ("%d", & withdraw);while (withdraw!=SENTINEL)
{
finbal = finbal - withdraw;
printf("Enter withdrawal (%d to quit)>", SENTINEL);
scanf("%f", & withdraw);} finbal = endbal + outdep - withdraw; printf("Your final balance in your checkbook should be $ %f" , finbal); return 0;
}
sorry, but that is horrible code. you have amounts in int and float types, it might be wise and safe to be consistent. you have an int withdraw yet you fill it with a %f in scanf. you have two balance variables, you only need one. you don't initialize variables that need it. you have duplicated several lines, indicating you have chosen the wrong loop construct (use a do while instead!) and what happens when the input isn't a number (say an empty line, or some text)? :~
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
modified on Wednesday, October 14, 2009 7:50 PM
-
sorry, but that is horrible code. you have amounts in int and float types, it might be wise and safe to be consistent. you have an int withdraw yet you fill it with a %f in scanf. you have two balance variables, you only need one. you don't initialize variables that need it. you have duplicated several lines, indicating you have chosen the wrong loop construct (use a do while instead!) and what happens when the input isn't a number (say an empty line, or some text)? :~
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
modified on Wednesday, October 14, 2009 7:50 PM
-
I am still learning. I was not required to write a code for this but just a draw a flowchart. Any pointers would be appreciated.
Hi, not tested:
// read a number, return it in delta; return TRUE if more to be handled, FALSE when done (with delta=0!)
float getNumber(char* prompt, float* delta) {
float number;
printf(prompt);
scanf("%f", number);
... add whatever checking you consider important
*delta=number;
return number!=0;
}int main(void) {
float balance, delta;
int more;
printf("BANK RECONCILIATION FOR: 10/21/98");
printf("\n\nEnding balance on statement:");
scanf("%f", &balance);
do {
more=getNumber("Enter Outstanding dep (or 0 to quit)", &delta);
balance+=delta;
while(more);
do {
more=getNumber("Outstanding check/withdrawal (or 0 to quit)");
balance-=delta;
while(more);
printf("Your final balance in your checkbook should be $ %f" , balance);
return 0;
}:)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!