Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
127 views
in Technique[技术] by (71.8m points)

c - Question on Purchasing which involves a while and switch statement

The code is basically asking for the total cost of bags made for each different colour, I wanted the while loop to exit and the total printed and calculated. However I met upon a complication, It works to some extent but I want it to consistently loop until "6" is entered so it can total but it doesn't at all. I was wondering how to get it to loop the main question and exit plus total. It will loop the question, it can exit but the total is equal to a trash figure.

#include <stdio.h>
#include <stdlib.h> 

int main()
{
    int total, price, bags, colour, cost, Exit ;
    
    printf ("********************************************
 Enter 6 to exit if you desire to do so
");
    printf ("********************************************
Enter the colour you desire:
 1)Black
 2)Red
 3)Yellow
 4)Green
 5)Other
********************************************
");
    scanf("%d", &colour);
    while (Exit!=6)
    {
        switch (colour)
        {
            case 1:
            {
                printf("Enter the number of bags you intend:  ");
                scanf("%d", &bags);
                price=400;
                cost=cost+(price*bags);
            }
            break;
            case 2:
            {
                printf("Enter the number of bags you intend:  ");
                scanf("%d", &bags);
                price=350;
                cost=price*bags;
            }
            break;
            case 3:
            {
                printf("Enter the number of bags you intend:  ");
                scanf("%d", &bags);
                price=120;
                cost=cost+(price*bags);
            }
            break;
            case 4:
            {
                printf("Enter the number of bags you intend:  ");
                scanf("%d", &bags);
                price=200;
                cost=cost+(price*bags);
            }
            break;
            case 5:
            {
                printf("Enter the number of bags you intend:  ");
                scanf("%d", &bags);
                price=50;
                cost=cost+(price*bags);
            }
            break;
            
        }
        printf("********************************************
 Are you complete with your purchase? If so enter 6
");
        scanf("%d", &Exit);
    }
   
    printf("The total is %d", total=total+cost);
    return 0;
}
question from:https://stackoverflow.com/questions/65892665/question-on-purchasing-which-involves-a-while-and-switch-statement

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Make sure you initialize total and cost to zero first. As mentioned, your current setup causes undefined behavior. Here is a solution:

int total = 0, price, bags, colour, cost = 0, Exit = 0;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...