Alisinna
Alisinna

Reputation: 121

Logic Error in C Program on xcode

I have written this code for a grocery store.. This code makes perfect sense to me. However, I keep getting a logic error. Every time the user enters a bill and then presses -1 to exit, he is taken back to the main menu. When user presses choice 2 to EXIT program, the program is NOT exiting and he taken back to case 1 for some reason. Could you please help me? Thank you!

#include <stdio.h>

int main(void){
    double prices[7];
    prices[0]=2.55;
    prices[1]=12.07;
    prices[2]=2.00;
    prices[3]=0.55;
    prices[4]=5.35;
    prices[5]=8.65;
    prices[6]=2.55;
    int choice;
    int productCode;
    int quantity;
    char stop[3];
    int compare;
    double price;
    double totalPrice=0;

    do{
        printf("\n1. Create new bill");
        printf("\n2. EXIT");
        printf("\n\nEnter choice: ");
        choice=scanf("%d", &choice);

        switch(choice){
            case 1:{
                do{
                    printf("\nEnter product code: ");
                    scanf("%d",&productCode);
                    printf("\nEnter quantity of product: ");
                    scanf("%d",&quantity);
                    price=prices[productCode]*quantity;
                    totalPrice=totalPrice+price;
                    printf("\nTo stop entering products enter -1.. to continue press any other character ");
                    scanf("%s", &stop);
                    compare=strcmp(stop, "-1");
                }while(compare!=0);
                break;
            }
            case 2: break;

            default: printf("\nInvalid choice");
        }
    }while(choice!=2);

    getchar();
    return 0;
}

Upvotes: 0

Views: 119

Answers (1)

Sully
Sully

Reputation: 14943

Instead of

choice=scanf("%d", &choice);

Do

scanf("%d", &choice);

scanf return value is:

On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.

If a reading error happens or the end-of-file is reached while reading, the proper indicator is set (feof or ferror). And, if either happens before any data could be successfully read, EOF is returned.

If an encoding error happens interpreting wide characters, the function sets errno to EILSEQ. http://www.cplusplus.com/reference/cstdio/scanf/

for (;;){

    printf("\n1. Create new bill");
    printf("\n2. EXIT");
    printf("\n\nEnter choice: ");

    scanf("%d", &choice); 

    if(choice == 2 ){
        break;
    } else if(choice == 1){

        do{

            printf("\nEnter product code: ");
            scanf("%d",&productCode);
            printf("\nEnter quantity of product: ");
            scanf("%d",&quantity);
            price=prices[productCode]*quantity;
            totalPrice=totalPrice+price;
            printf("\nTo stop entering products enter -1.. to continue press any other character ");
            scanf("%s", &stop);
            compare=strcmp(stop, "-1");

        }while(compare!=0);

    } else {
        printf("\nInvalid choice");
    }

}

Upvotes: 2

Related Questions