0

I want to ask the user how many tickets he wants to buy and then have a loop that will specify what type of ticket he wants. Each kind of ticket has a different value and I want to calculate the total sum of the tickets. The problem is that for some reason it iterates in a weird way. Why is that? I tried with a for loop as well and an array of chars but the results were the same. What am I missing here?

#include <stdio.h>
#include <stdbool.h>

int main()
{
   int n,i;
   printf("How many tickets would you like?\n");
   scanf("%d", &n);
   char c;
   float sum = 0;
   while(n)
   {
       printf("What type of ticket do you want?\n");
       scanf("%c",&c);
       switch (c)
       {
        case ('E'):
            {
            sum +=0.23;
            break;
            }
        case('A'):
            {
              sum+=0.7;
            break;
            }
        case('T'):
            {
            sum+=0.15;
            break;
            }
        }
        n--;
   }
   printf("Sum is: %f", sum);
   return 0;
}
Greggs
  • 1
  • 4
  • What do u mean with " it iterates in a weird way." ? Have you tried printing `n` for debugging? – Ivan Nov 17 '21 at 13:27
  • Could you please elaborate on your problem? What do you mean by 'weird'? – Refugnic Eternium Nov 17 '21 at 13:32
  • It printed the question What type of ticket do you want? twice but I realised that scanf counts the '\n' and thus I am "missing" iterations. I edited scanf to scanf("%c\n",&c); and it works as intended. Thank you for taking the time to help me. – Greggs Nov 17 '21 at 13:45

0 Answers0