-1

I tried to use for loop calculate the number of books keyed in and sum up their total price, but at the end i only get zero price in C program. What is my problem ? How to solve it?

 #include<stdio.h>     

 int main(void)          
 {      
        int booknum;                  
        float bookfee,bookgst,nogst,totfee,newfee,newfee_nogst;           
        bookgst=0.0;                
        nogst=0.0;                
        int cnt;                    
        char code;                           
        printf("Key in total books purchased >> ");                 
        scanf("%d",&booknum);

        for(cnt=1;cnt<=booknum;cnt++)
        {
            printf("\n\n");
            printf("Key in price of the book >> ");
            scanf("%f",&bookfee);
            printf("Key in type( S=standard gst,Z=zero gst) \n>> ");
            scanf("%c",&code);
            getchar();
            if(code=='S')
            {
                newfee=bookfee+0.6;
            }
            else if(code=='Z')
            {
                newfee_nogst=bookfee;
            }
            bookgst=bookgst+newfee;
            nogst=nogst+newfee_nogst;
            printf("\n");

        }
        totfee=bookgst+nogst;
        printf("Book purchased with GST    : RM %.2f\n",bookgst);
        printf("Book purchased without GST : RM %.2f\n",nogst);
        printf("Total payment              : RM %.2f\n",totfee);

        return 0;
    }
  • I think you want to move the updates to `bookgst` and `nogst` up into the `if/else` bodies, other wise a code-Z followed by 3 code-Ss will result in `newfee_nogst` being added to `nogst` 4 times. Inside your for loop, you probably want an `else { printf("not a valid code: %s (0x%02x)\n", code, (int)code);}` I think your `scanf`s are leaving the keystroke in the input buffer as well---adding the 'else' case should help diagnose this. – lockcmpxchg8b Nov 25 '17 at 08:48
  • Thanks for the informations guys. – JUZ Aviewer Nov 25 '17 at 08:57
  • What if an invalid `code` is entered? – Paul Ogilvie Nov 25 '17 at 08:58
  • @JUZ Aviewer Use a space in the format specifier scanf(" %c",&code); Otherwise new line character is read. – Vlad from Moscow Nov 25 '17 at 08:59

1 Answers1

0

There are a few problems with this code, but you're almost there!

First code reading needs to eat the previous \n (see this), otherwise the code is neither Z not S (it's a newline), and that's why the fees are never added. (Search also for "fgets vs scanf" to see how to use the safer fgets).

scanf(" %c",&code);

then these lines

bookgst=bookgst+newfee;
nogst=nogst+newfee_nogst;

add the newfee / newfee_nogst ; these variables are set to 0 before the loop, but at the next occurence, they're still set to the value of the previous occurrence, thus either set them to 0 at the beginning of the loop, or, add the value directly in the if (see below). And since we're here, print an error if the code is wrong (and maybe subtract one to cnt to do one more loop with a correct code, in this case).

Also, the GST calculation is probably wrong, 6% of x is 0.06 * x, and if you want GST added to the value that's x * 1.06

if(code=='S')
{
    bookgst = bookgst + bookfee*1.06; // or bookgst += bookfee*1.06
}
else if(code=='Z')
{
    nogst = nogst + bookfee; // or nogst += bookfee
}
else {
    printf("Code not understood\n");
}
Déjà vu
  • 28,223
  • 6
  • 72
  • 100