首页 > 解决方案 > 难以根据用户选择编写循环

问题描述

我很难编写一个循环来询问用户他/她是否想继续购买产品。这是我在学校课程的一部分。我已经编写了所有必需的函数,但我正在努力让循环工作。

基本上,我希望循环做的是询问用户他/她是否想继续购买,答案应该是 Y 或 N。然后它将占用剩余的预算,将其实施回程序并再次运行它考虑到该预算,然后扣除所选产品的价值。

我一直在尝试编写该循环超过 2 个小时,但我的想法已经不多了。

这就是我正在使用的代码:

#include <stdio.h>

int getItemPrice(char itemPrefix, int applePrice, int orangePrice, int pearPrice);
displayMenu(int applePrice, int orangePrice, int pearPrice);
withinBudget(int budget, char purchase, int applePrice, int orangePrice, int pearPrice);
purchaseItem(int budget, char purchase, int applePrice, int orangePrice, int pearPrice);

int main()
{
    
    int orangePrice, applePrice, pearPrice, budget; 
    char purchase;                                      
    printf("*****************\n");
    printf("Item prefixes\n");
    printf("A: Apple\n");
    printf("O: Orange\n");
    printf("P: Pear\n");
    printf("*****************\n\n");

    
    printf("***************\n");
    printf("*** MyStore ***\n");
    printf("***************\n\n");
    
    printf("**** SHOPKEEPER PANEL ****\n");

    printf("Welcome to the store. Please enter the prices for the following products: \n");
    printf("Please enter the price for the Orange: \x9C");
    scanf_s("%d", &orangePrice);
    printf("Please enter the price for the Apple: \x9C");
    scanf_s("%d", &applePrice); 
    printf("Please enter the price for the Pear: \x9C");
    scanf_s("%d", &pearPrice);  
    printf("\n\n");

    displayMenu(applePrice, orangePrice, pearPrice);
    
    printf("**** Customer menu ****\n");
    printf("Please enter your budget: \x9C");
    scanf_s("%d", &budget);
    printf("Please enter the item you would like to purchase using the item Prefix: ");
    scanf_s(" %c", &purchase, 1);
    printf("\n\n");

    displayMenu(applePrice, orangePrice, pearPrice);

    withinBudget(budget, purchase, applePrice, orangePrice, pearPrice);

    purchaseItem(budget, purchase, applePrice, orangePrice, pearPrice);

    return 0;
};

int getItemPrice(char itemPrefix, int applePrice, int orangePrice, int pearPrice)
{

    if (itemPrefix == 'A') {
        return applePrice; 
    }
    else if (itemPrefix == 'O') {
        return orangePrice; 
    }
    else if (itemPrefix == 'P') {
        return pearPrice;
    }
    else {
        return -1;  
    }

    return getItemPrice;
}

displayMenu(int applePrice, int orangePrice, int pearPrice)
{
    
    printf("**** Shop Menu ****\n");
    printf("Item:\t\tPrice\n");
    printf("A:\t\t\x9C%d\n", applePrice);   
    printf("O:\t\t\x9C%d\n", orangePrice);  
    printf("P:\t\t\x9C%d\n", pearPrice);    
    printf("\n\n");
    return 0;
}
withinBudget(int budget, char purchase, int applePrice, int orangePrice, int pearPrice)
{
    int getItemPrice(itemPrefix, applePrice, orangePrice, pearPrice);

    if (purchase == 'A') {
        return applePrice;
    }
    else if (purchase == 'O') {
        return orangePrice; 
    }
    else if (purchase == 'P') {
        return pearPrice;
    }
    else {
        return -1;
    }
}
purchaseItem(int budget, char purchase, int applePrice, int orangePrice, int pearPrice)
{

    int items ;
    int calculation;

    if (purchase == 'A') {
        items = applePrice; 
    }
    else if (purchase == 'O') {
        items = orangePrice;
    }
    else if (purchase == 'P') {
        items = pearPrice;  
    }
    else {
        items = -1; 
    }

    if (items != -1) {
        calculation = budget - items;
        if (calculation >= 0) { 
            printf("Purchase was a success!\n");
            printf("Purchase details\n");
            printf("----------------------\n");
            printf("Item: %c\n", purchase);
            printf("Price: \x9C%d\n", items);
            printf("Remaining budget: \x9C%d\n\n", calculation);
            printf("Thanks for shopping with us!");
        }
        else {  
            items = -1;
        }
    }
    if (items == -1) {  
        printf("Purchase FAILED!\n");
        printf("Low budget or invalid item!\n\n");
        printf("Thanks for shopping with us!");
    }
;

我已经尝试在程序主体以及购买项目函数内部编写 while 循环。在这一点上我完全一无所知,我真的不知道我该怎么做才能让它发挥作用。或者我没有做的事情会帮助我让这个程序按我的意愿工作。

任何帮助,将不胜感激。以及关于我可以做些什么来改进它的任何提示。

标签: c

解决方案


您可以像这样实现循环逻辑:

int main(void){
    ... // Your definitons
    bool proceed = true;

    while(proceed){
        ... // All your code here

        bool isValid = false; // Check the answer character's validity
        while(!isValid){
            printf("Would you like to purchase anything else? [Y / N]");
            char answer = getchar();
            putchar('\n'); // Add a line feed
            if (answer == 'Y' || answer == 'y'){
                proceed = true;
                isValid = true;
            }
            else if (answer == 'N' || answer == 'n'){
                proceed = false;
                isValid = true;
            }
            else{
                // Entered an unexpected character, force the user to enter a valid character
                printf("Please enter a valid answer\n");
                isValid = false;
            }
        }
    }
    return 0;
}

推荐阅读