samedi 27 juin 2015

Why doesn't my program stop when I press 0? C program

I made a simple program asking if you could enter a bar if you were of age. Then proceeded to ask how much you were willing to spend, and after asked how much would your first buy be. it proceeds to ask if you would like to buy more, however if I didn't want to buy more I could enter the value "0", but when I enter "0" it doesn't close the program.

The code is as follows:

#include <stdio.h>

int main() {
int age, legal = 18;
float money, buy;

    printf("At the bar the security must ID the customers that look underage, what is your age?: ");
    scanf("%d", &age);

        if(age < legal){
            printf("\nYou are not allowed!\n");
        }

        else if(age >= legal){
            printf("\nYou may enter, and buy whatever you wish!\n");

            printf("\nHow much money are you willing to spend tonight?: ");
            scanf("%f", &money);

            printf("\nWith the %.2f dollars you want to spend tonight what is the price of food or beverage you wish to buy first?: ", money);
            scanf("%f", &buy);

            money = money - buy;

            while(money > 0 || buy==0) {
                printf("\nYou now have %.2f dollars remaining, what else would you like to buy? (Press 0 to stop if you don't wish to buy any more!!): ", money);
                scanf("%f", &buy);
                money = money - buy;
            }
                if(money == 0){
                    printf("\nYou have spent all the money that you wished to spend at the bar, we hope you enjoyed your stay!!\n");
                }

                else if(money > 0){
                    printf("\nYou still have %.2f dollars left, We hope you enjoyed your stay at the bar!!\n");
                } 

        }       
}

Aucun commentaire:

Enregistrer un commentaire