0

In my code the answer for total = 0 always. I don't know what's the problem in my code. Can anyone help me please to solve this problem.

I tried using 'if else' condition instead of 'switch'. However the problem doesn't solve.

I think the 'pkg' value does not assign for the variable. But what is the reason for that. As I know it I have used the 'scanf' function correctly.

#include<stdio.h>

int main(void)
{
    int i = 1;
    float km, amount, total = 0;
    char next, loyal, pkg;

    printf("Enter Loyalty (Y / N) ?");
    scanf("%*c%c", &loyal);

    do{
        printf("Package No : ");
        scanf("%c*c", &pkg);

        printf("Total Distance : ");
        scanf("%f", &km);

        if(km <= 80) {
            switch (pkg){
                case 'A':
                    amount = 1500;
                    break;
                case 'B':
                    amount = 10000;
                    break;
                case 'C':
                    amount = 13000;
                    break;
                case 'D':
                    amount = 12000;
                    break;
            }
        }
        else {
            switch (pkg) {
                case 'A':
                    amount = 1500 + 150 * (km - 80);
                    break;
                case 'B':
                    amount = 10000 + 150 * (km - 80);
                    break;
                case 'C':
                    amount = 13000 + 150 * (km - 80);
                    break;
                case 'D':
                    amount = 12000 + 150 * (km - 80);
                    break;
            }
        }

        total = total + amount;
        i++;

        printf("\nDo you have more customers (Y / N): ");
        scanf("%*c%c", &next);

        printf("----------------------------------------------\n");

    } while (next == 'Y' && i <= 3);

    printf("\n\nPrice = %.2f", total);

    return 0;
}
  • 3
    Add default cases to all of your switch statements, giving an appropriate error when reached. Print the values of the variables you input, and see if they're what you expect. There's lots you can do that you haven't. Chances are you're falling right through one of both of your switch statements, and `amount` is remaining zero. Test it. – Tom Karzes Apr 12 '19 at 04:25
  • 2
    Scanf has a return value for a reason, you are ignoring it at your own risk. – Yunnosch Apr 12 '19 at 04:29
  • Init all your variables to recognisable values, it will hint at where your problem is. – Yunnosch Apr 12 '19 at 04:30
  • `scanf("%*c%c", &loyal);` --> makes `loyal` have the value of `'\n'`. – chux - Reinstate Monica Apr 12 '19 at 04:31
  • 1
    Tip: use `fgets()` for _all_ user input. Or invest in lots of time leanring `scanf()` - your call. – chux - Reinstate Monica Apr 12 '19 at 04:32
  • `scanf` is a bad Idea – asio_guy Apr 12 '19 at 05:13
  • 2
    `scanf("%*c%c", &loyal);` ignores the first character and places the second into `loyal` — that's probably a newline. Fortunately, you don't use `loyal` after this. `scanf("%c*c", &pkg);` most probably doesn't find a `*` and a `c` after you type the package letter — fortunately, this doesn't matter much, either (and there's no way to know that the match failed with that format). Most probably, you were thinking of using `scanf("%c%*c", &pkg);` but forgot the second `%`. This is now the reverse sequence from the `loyal` format. Using `scanf()` correctly is hard; it demands the utmost care. – Jonathan Leffler Apr 12 '19 at 05:37
  • 1
    You forgot to add one of the most important information: What is your input. Also What is your expected output for each set of input. Only providing one wrong output is not enough information at all. – Gerhardh Apr 12 '19 at 05:40

3 Answers3

0

I tried your code and the problem here is that i think you are using lowercase inputs that is why you are getting wrong output use a uppercase function on your inputs or use uppercase inputs to avoid wrong result.

Here is output with uppercase inputs: enter image description here

You should always use default in switch to avoid more problems.

Faiz Khan
  • 288
  • 1
  • 9
  • How did you find out lower case inputs were given? – MayurK Apr 12 '19 at 05:04
  • 2
    i tried both of them and uppercase inputs were working fine, on lowercase output is always zero – Faiz Khan Apr 12 '19 at 05:09
  • @FaizKhan, You just assumed he entered the wrong input for his own program? In that case, he could have also entered anything gibberish and still ended up at 0. – Pratik Sampat Apr 12 '19 at 05:13
  • 3
    @FaizKhan OK. Few suggestions to improve your answer: (1) Don't put image of the output. Past the output itself as text. (2) You can suggest how the program can be improved to handle this case. i.e. how lower case also can be used with minimal code change. (3) How invalid inputs can be detected. – MayurK Apr 12 '19 at 05:14
  • i told that if he will use default in switch and use function to convert to uppercase on inputs then her program would work fine. Thanks @MayurK for suggestions – Faiz Khan Apr 12 '19 at 05:18
  • @PratikSampat as i told him to use default in switch to avoid any gibberish. – Faiz Khan Apr 12 '19 at 05:20
0

The answers pretty simple:

change this:scanf("%c*c", &pkg); to this:scanf("%c%*c", &pkg);

You missed the % which meant there was an extra '\n'.

zerii
  • 16
  • 1
  • 4
  • 1
    Well, you are usually [introducing undefined behavior](https://stackoverflow.com/questions/2979209/using-fflushstdin) in your programs, I'm afraid. – Bob__ Apr 12 '19 at 14:09
  • The C standard specifically says that `fflush(stdin)` is undefined behavior. Suggest: `int ch; while( ( ch=getchar() ) != EOF && ch != '\n' ){;}` – user3629249 Apr 14 '19 at 14:45
  • Thanks, I changed the answer. I'll make sure not to use it in the future – zerii Apr 20 '19 at 01:49
-1

The code should be change as follows, scanf("%c%*c", &loyal);

The error was in the 'scanf' function.