0
#include "stdafx.h"
#include "windows.h"


#define A_P 4.55

double pounds;
double subT = 0.0;
double totalP = 0.0;
double totalSub = 0.0;

void main()
{
char item;

    while (1) {
        printf("Please select your item.\n");
        scanf("%c", &item);
        if (item == 'a') {
            price = A_P;
            printf("Please enter the amount in pounds.(lb)");
            scanf("%f", &pounds);
            if (pounds > 0) {
                subT = price * pounds;
                totalP += pounds;
                totalSub += subT;
            }
            else {
                printf("1st");
            }
        }
        else {
            printf("2nd");
        }

    }

I don't understand why i am getting this logical error. When the user enters an item, i ask the user for another input which is the "pounds" of the item. But i have no idea why it goes to the last else condition which prints out "2nd". Can anyone explain what is happening. I initially tried this with a switch as well but the same problem occurs.

case 'A':
        case 'a':
            price = A_P;
            printf("Please enter the amount in pounds.(lb)");
            scanf("%f", &pounds);
            if (pounds > 0) {
                subT = price * pounds;
                totalP += pounds;
                totalSub += subT;
            }
            else
                Beep(500, 500);
            break;

...

default:// goes straight to the defualt statement ignores second scanf which reads pounds varaible
            printf("That is an invlalid input.\n");
            Beep(500, 1000);
HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
user3505212
  • 105
  • 1
  • 3
  • 9
  • `scanf("%c", &item);` --> `scanf(" %c", &item);` (add space) Else on 2nd loop, `item` has the value of `'\n'` – chux - Reinstate Monica Nov 04 '15 at 03:03
  • Please see http://stackoverflow.com/questions/26945123/unable-to-use-scanf-twice-for-scanning-a-string-and-then-scanning-a-char – PeCosta Nov 04 '15 at 03:06
  • Possible duplicate of [C: function skips user input in code](http://stackoverflow.com/questions/9441501/c-function-skips-user-input-in-code) – rob mayoff Nov 04 '15 at 05:16
  • *Always* check return value of `scanf`. It's pointless to debug IO code which does not detect "normal" error conditions. This is doubly important with `scanf` as it can have parse errors in addition to the usual IO errors. – hyde Nov 04 '15 at 05:45

1 Answers1

0

The problem here is that scanf() in the second iteration of the loop is "consuming" the \n from the first iteration. This is why scanf() doesn't prompt you the second time and automatically goes to the "2nd" condition. chux is right--you can fix this by adding a space before the %c:

scanf(" %c", &item);

For more information, see this thread: Using the scanf function in while loop

Side note: it doesn't look like you're declaring the price variable anywhere. You also should use %lf instead of %f for the scanf() taking in the double.

Community
  • 1
  • 1
jsherman256
  • 100
  • 5