1

i am trying to build a small program that uses the input from the user, and then uses a switch case to perform an action that fits the user input. i have been tring to find what is wrong in my program, with no luck. there is clearly something i am missing.

here is the code, can you help me find what is wrong with it?

#include <stdio.h>
#define A 8.5
#define B 7.6
#define C 7.7
#define D 7.7

int main ()
{


    char fuel;
    float amount,total;

    printf("please enter the desired fuel\n");
    scanf_s("%c",&fuel);

   switch(fuel)
   {
   case 'A' :
         printf("how many liters of fuel do you want?\n");
         scanf_s("%f",&amount);
         total=amount*A;
         if(total>150)
         {
             printf("the total price to pay is %.3f: \nYou have won a newspaper", total);
         }
         else
         printf("the total price to pay is %.3f", total);
         break;

      case 'B' :
          printf("how many liters of fuel do you want?\n");
         scanf_s("%f",&amount);
         total=amount*B;
         if(total>150)
             printf("the total price to pay is %f: \nYou have won a newspaper", total);
         else
         printf("the total price to pay is %.3f", total);

          break;
      case 'C' :
         printf("how many liters of fuel do you want?\n");
         scanf_s("%f",&amount);
         total=amount*C;
         if(total>150)
             printf("the total price to pay is %f: \nYou have won a newspaper", total);
         else
         printf("the total price to pay is %.3f", total);
         break;

      case 'D' :
         printf("how many liters of fuel do you want?\n");
         scanf_s("%f",&amount);
         total=amount*D;
         if(total>150)
             printf("the total price to pay is %f: \nYou have won a newspaper", total);
         else
         printf("the total price to pay is %f", total);

      break;

      default: 
      printf("no\n");
      break;
   }



}

Even when i input 'A', 'B', 'C' or 'D' it goes to the default, and not to the apropriate case. thanks for the help.

  • Have you tried using a debugger? Problems such as these are trivial to solve with one. – dandan78 Nov 24 '15 at 10:01
  • Works all right here. Are you sure you typed `A` and not `'A'` or `a`? – Weather Vane Nov 24 '15 at 10:02
  • Note that this seems to be specific to the MS specific `scanf_s()` function. Using `scanf()` works (checked with VS 12). However, you should [avoid using the `scanf()` family at all and use something like fgets() instead](http://stackoverflow.com/questions/2430303/disadvantages-of-scanf) - read the whole line and parse/handle the input in your application. – Andreas Fester Nov 24 '15 at 10:02
  • @WeatherVane is i am sure. –  Nov 24 '15 at 10:10
  • @dandan78 i have tried. that is why im writing this here. –  Nov 24 '15 at 10:10
  • 1
    You must read the man page please: *"Unlike scanf and wscanf, scanf_s and wscanf_s require the buffer size to be specified for all input parameters of type c, C, s, S, ...*" So you are missing an argument `scanf_s("%c",&fuel);` should be `scanf_s("%c",&fuel, 1);` – Weather Vane Nov 24 '15 at 10:11
  • @GabrielMichaeli What is the actual value of your `fuel` variable then? – dandan78 Nov 24 '15 at 10:13
  • @dandan78 the actual value is the users choise. he choses between differnty kinds of fuel, and then it goues to the switch. –  Nov 24 '15 at 10:17
  • 1
    @GabrielMichaeli we can see that, you were asked to look at the *actual* value of the variable that seems to be misbehaving, because it is obviously not what you *think* it should be. – Weather Vane Nov 24 '15 at 10:22
  • You could make life simple by calling `getchar()` and thereby completely skip the quirks of the `scanf` family of functions., Note that you should also change: `char fuel;` to `int fuel;` – user3629249 Nov 25 '15 at 18:09
  • the posted code does not prompt the user with the available choices for input, Therefore, the user will see a prompt to enter 'something' but no indication of exactly what the 'something' should be. Suggest a few printf() statements to display a menu to the user, then if the user does not enter a valid input, loop to display the menu again – user3629249 Nov 25 '15 at 18:13
  • when compiling, always enable all the warnings, then fix those warnings. The compiler would have told you that the `scanf_s()` call was not correct. – user3629249 Nov 25 '15 at 18:16

1 Answers1

5

You havn't used the scanf_s function correctly. According to its documentation :

Unlike scanf and wscanf, scanf_s and wscanf_s require the buffer size to be specified for all input parameters of type c, C, s, S, or string control sets that are enclosed in []. The buffer size in characters is passed as an additional parameter immediately following the pointer to the buffer or variable.

And checking for errors should also be done, so you should do:

char fuel;
if (scanf_s("%c", &fuel, 1) != 1)  {
    puts("Error from scanf_s");
    return 1;
}
nos
  • 223,662
  • 58
  • 417
  • 506