English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

for some reason i keep getting 1245036 as the new credit limit, no matter what I put in. Help?



#include


int main()
{

int employee = 1;
int accountNumber;
int creditBefore;
int currentBalance;
int creditAfter;

while ( employee <= 3 ){


printf( "Enter account number: ");
scanf( "%d", &accountNumber );

printf( "Enter current balance: ");
scanf( "%d", ¤tBalance );

printf( "Enter current credit limit: ");
scanf( "%d", &creditBefore );

creditAfter = creditBefore / 2;

printf( "Your new credit limit is %d\n", &creditAfter );

if ( creditAfter < currentBalance ){
printf( "Current balance exceeds new credit limit.\n" );
}
else{
printf( "Current balance does not exceed new credit limit.\n" );
}

}

return 0;

}

2007-02-05 19:33:06 · 3 answers · asked by bloodyhell1981 1 in Computers & Internet Programming & Design

3 answers

It's been a while since I coded in C, but here goes:

In the printf("Your new credit limit is %d\n", &creditAfter); statement, the & indicates that you want to print the ADDRESS of creditAfter, not the value. Try it without the &.

I'm assuming that you're just playing around here. So the following two points aren't related to the problem that you're having but may yield unintended results.

If you divide an odd numbered integer by 2, the result will be a real number. By declaring creditAfter as int, any decimal portion will be truncated.

The condition for your while loop checks that employee is less than 3; however, employee is not being incremented within the loop. This will result in an infinite loop.

2007-02-05 20:00:29 · answer #1 · answered by lsu_tiger_in_dallas 3 · 1 0

This line
printf( "Your new credit limit is %d\n", &creditAfter );
must be in the form
printf( "Your new credit limit is %d\n", creditAfter );

Here is the complete better program:

#include

int main()
{
int employee = 1;
int accountNumber;
int creditBefore;
int currentBalance;
int creditAfter;

while ( employee <= 3 ){


printf( "\n\n\nEnter account number: ");
scanf( "%d", &accountNumber );

printf( "\nEnter current balance: ");
scanf( "%d", ¤tBalance );

printf( "\nEnter current credit limit: ");
scanf( "%d", &creditBefore );

creditAfter = creditBefore / 2;

printf( "\nYour new credit limit is %d\n", creditAfter );

if ( creditAfter < currentBalance ){
printf( "\nCurrent balance exceeds new credit limit.\n" );
}
else{
printf( "\nCurrent balance does not exceed new credit limit.\n" );
}

}

return 0;

}

2007-02-05 20:38:26 · answer #2 · answered by iyiogrenci 6 · 1 0

the statement scanf( "%d", ¤tBalance ); has to use ¤tBalance, this particular variable is not declared, also the symbol should be & to mention the address

2007-02-05 19:43:55 · answer #3 · answered by thatsme s 1 · 1 0

fedest.com, questions and answers