mardi 3 novembre 2015

2nd Scanf in loop is not is not being read.jumps to else condition

#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);

Aucun commentaire:

Enregistrer un commentaire