Question:
Can you tell me what's wrong with this C Language code?
Mark
2010-09-05 13:37:10 UTC
I've been working on this code for a week, and I still can't get it to work. It's supposed to allow you to input a beginning total, then allow you to type in "D" to add in a deposit, and a "W" to subtract a withdrawal, and a "F" to display the final total and end the program.

Please, if you know C Language, please tell me what's wrong.

#include
#pragma warning(disable:4996)

void explanation();
double deposit();
double withdrawl();
double results();
double finish();
void print_results();

void main (void)

{


int total = 0.0;
int n;
char x;
int dep;
int wit;


explanation();

printf("Please enter the old balance: ");
scanf("%d", &n);
printf("The current total is %d\n\n", n);

printf("Enter the transactions now.\n");
printf("Enter an F for the transaction type when you are finished.\n\n");

printf("Transaction Type (D=deposit, W=withdrawl, F=finished): \n ");
scanf("%c", &x);

if(x=='D')
deposit(dep, n);
if(x=='W')
withdrawl(wit, n);
if(x=='F')
finish();

total = n;

printf("Your ending balance is: %d", n);


}

double deposit(double dep, double n)
{

double result;

printf("\n Amount: ");
scanf("%d", &dep);

result = dep + n;

return result;

}

double withdrawl(double wit, double n)
{

double result;

printf("\n Amount: ");
scanf("%d", &wit);

result = n - wit;

return result;

}

double finish( void )

{

printf("Your final total is: ");

return 0;
}

void explanation()
{
printf("Written by Mark Mansur. \n");
printf("This program allows the user to input an initial balance, \n");
printf("then deposit or withdraw any number of dollars, \n");
printf("then end the program by typing in 'f'. \n\n");
printf("BANK ACCOUNT PROGRAM \n");
printf("-------------------- \n");
}
Three answers:
Shadow Wolf
2010-09-05 14:26:05 UTC
pragma? What warning are you turning off? If you write your program correctly, there is never a need to turn off warnings. Make the compiler happy as it is trying to conform to a standard and you may be short circuiting error detection and hiding real problems with the program. Like "goto", "pragma" should almost never be used. When you get the experience where you might have an exception, you'll probably also be able to change the code to avoid it.



You aren't using any of the return values. Your program is not done. You need to use the results to get your answer.



I really hate the wasted white space of that programming style. Since leading tabs and spaces get eaten here, I used underline for indents. And I'm also aware how you are probably being taught but it is just so much uninformed academic BS. If you spread out the code like you have it, it is that much harder to see large sections of it and in many cases to see your mistakes. The more compact, the easier it is to see more of the code on your screen. Compact styles also use less paper should you have to make hard copies.



double withdrawl(double wit, double n){

__double result;

__printf("\n Amount: ");

__scanf("%d", &wit);

__result = n - wit;

__return result; /* You aren't using the return value at all. Same comment for other functions. */

}



What exactly are you doing with the results? Note I reformatted the code so it is easier to read and added variables to collect the returned values. When you have simple small if statements, leave them on one line. You may want to change things in the actual program.



if(x=='D') D = deposit(dep, n);

if(x=='W') W = withdrawl(wit, n);

if(x=='F') F = finish();

total = n; /* Where exactly are you getting N and how is it computed? */



This might fix your warning problem.



void explanation(void); /* A bit of self documenting and gets you more warning/error information. */

double deposit(double dep, double n) ;

double withdrawl(double wit, double n) ;

double results(); /* Where is this function? */

double finish( void ) ; /* Why return a zero value at all if nothing is needed? Incomplete function? */

void print_results(); /* Where is this function? */



Note that I didn't fix your code. I only pointed out several problems. Don't expect anyone to be doing your homework for you.



Shadow Wolf
2010-09-05 13:54:55 UTC
Its because there was still information in the standard input buffer. That is why it skipped the second scanf.



I just added an __fpurge(stdin) after the first scanf. However only some Unix-based system have fpurge, so if this was for a Windows assignment, you would need to find a different way.



Also as a side note, you never added anything if nether D,W, and F was entered on the second scanf.



UPDATE: what operating system and compiler are you using? My compiler is saying that line 55 and 69, it was expecting an integer variable, yet the variables wit and dep are doubles parables.



UPDATE: Ok on line 55 and 69, change the %d to %lf, (thats a lower case L, not a capital i) this should clear that warning. This way the compiler will expect the double variable
?
2016-06-01 15:24:04 UTC
= is used for assignment. In C unlike java any positive value (.i.e., > 0) equals true. So in the statement if (tt=1), you are not comparing but assigning tt value 1. After its assigning it compares with the result, the value of t which is 1 and greater than 0. So its true and you always get nice. The error is, you are assigning value (=) instead of comparing (==) in the if statement. Another funda for fun, in this same code if you keep if(tt=0) printf("\nnice"); else if(tt=2) printf("\nbad"); changed tt=1 to tt=0 Since 0 is false for C the first will always be false and always tt=2 will become true and it will print bad. The catch is the value will get assigned to tt and the value of tt will be evaluated in the if statement.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...