Messed up output

• 08-03-2005
Extropian
Messed up output
Here's the code:

Code:

```         /*        Programming In C Assignment 4, Project 1                 Program Name:        Mileage Calculator                 Date:                        July 10th                   Developed by:  Bryan Bucknell                 Description:          This program will calculate  miles per                                                 gallon for a car's gas tank         */ #include <stdio.h> int main() {                 int                account;                        float        balance;                                float        charges;                 float        credit;                 float        limit;                                                 while(account != -1){                         printf("Enter account number (-1 to end): ");                         scanf("%d" , &account);                         printf("Enter beginning balance: ");                                         printf("Enter total charges: ");                         scanf("%.2f" , &charges);                         printf("Enter total credits: ");                         scanf("%.2f", &credit);                         printf("Enter credit limit: ");                                 scanf(".2f", &limit);                                                 if(credit > limit){                                 printf("Credit Limit Exceeded\n");                         }                                                }                 return 0;                 }```
All the enter total charges, enter credits..etc run together without waiting for input, I wondered in someone could help me sort this mess out.
Thanks,
Extro
• 08-03-2005
orbitz
Account is uninitialized so attempting to compare it's value to -1 is undefined behavior. You are missing a magical '%' character in your scanf's.
• 08-03-2005
Epo
Though account should be initialized, it is very rarely going to influence this program. The check is whether or not account equals -1. Since account gets set to the random garbage memory that was left behind by another program when declared, it will never (except for a very very slim chance) start off as -1.

The problem comes when you use your scanf.

Say you enter:
4456
for your account number. When you hit "enter", this gets sent to the input buffer (i.e. data from the keyboard waiting to be processed):
4456\n
scanf() reads in the integer: 4456
However, you're still left with a '\n' (note, this is 1 character, not two, it sybolizes a new line) character. So, the very next scanf() (for total charges) reads in the '\n' character, and all of a sudden you're at "total credits".

Now, there are several ways to clear the input buffer so that this doesn't happen. After every scanf() you have one of these options:

Option 1
fflush(stdin);
NEVER USE THIS OPTION (Read why here).

Option 2
Create your own code. Along the lines of:
Code:

```int ch; while(((ch = getchar()) != '\n') && (ch != EOF));```
Note: stdio.h must be included (for the EOF macro definition), but since you're using scanf(), you'll usually be okay (unless you turn this into its own function). This can also be used for waiting for a "keypress" as you can read about here (from the faq).

This option systematically reads from the input buffer until a newline is encountered.

Hope this helps some
• 08-03-2005
Extropian
Ok I fixed up a few minor errrors but the input questions are still running into one another from "enter total charges" forward. No idea whats causeing this to happen. here's the code again:
Code:

``` #include <stdio.h> int main() {                 int                account;                        float        balance;                                float        charges;                 float        credit;                 float        limit;                                 account = 0;                         while(account != -1){                         printf("Enter account number (-1 to end): ");                         scanf("%d" , &account);                         printf("Enter beginning balance: ");                         scanf("%.2f" , &balance);                                 printf("Enter total charges: ");                         scanf("%.2f" , &charges);                         printf("Enter total credits: ");                         scanf("%.2f", &credit);                         printf("Enter credit limit: ");                                 scanf(".%2f", &limit);                                                 if(credit > limit){                                 printf("Credit Limit Exceeded\n");                         }                                                }                 return 0;                 }```
• 08-03-2005
Dave_Sinkula
Both "%.2f" and ".%2f" are incorrect, for "one" thing (scanf is not identical to printf). Input does not have precision and the decimal point preceding a directive is significant.

I'd recommend reading user input as a string and then attempting to convert. Something similar is posted in the FAQ.

That said, this may be closer to what you are trying to do.
Code:

```#include <stdio.h> int main() {   float balance, charges, credit, limit;   int  account = 0;   for ( ;; )   {       printf("Enter account number (-1 to end): ");       scanf("%d" , &account);       if ( account == -1 )       {         break;       }       printf("Enter beginning balance: ");       scanf("%f" , &balance);       printf("Enter total charges: ");       scanf("%f" , &charges);       printf("Enter total credits: ");       scanf("%f", &credit);       printf("Enter credit limit: ");       scanf("%f", &limit);       if ( credit > limit )       {         printf("Credit Limit Exceeded\n");       }   }   return 0; }```
But I'd really take the "input as string, then convert" approach. And maybe add several fflush(stdout)'s.