如何将while循环中输入的不同值分开?[closed]

k4emjkb1  于 2022-12-02  发布在  其他
关注(0)|答案(1)|浏览(123)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题吗?**通过editing this post添加详细信息并阐明问题。

4小时前关门了。
Improve this question
因此,对于上下文,我们的老师要求我们创建一个排序系统,它可以接受多达3个订单,每个订单有3个输入,主、侧、我使用了一个函数“void inputOrder()”并执行while循环,然后将命令系统放入该函数中,以便在main中调用它,并使其不断重复,直到发出3个命令。我如何得到每个订单的这些投入的价格?例如,订单1的成本为5. 65美元,将3个投入加起来,订单2的成本为7美元,等等,我如何能够做到这一点?
我有一个想法,创建一个函数,将采取小计,但我目前的代码是有点低效,所以我有一个艰难的时间试图使它发生。

void inputOrder()
{
    int mainOrder, side, drink, modify, cont;
    int order = 1;
    
    do{ printf("\n\nOrder %d: \n", order);
    printf("\tMain: \t");
    scanf("%d", &mainOrder);
    
    if(mainOrder <= 4)
    {
    switch(mainOrder)
    {
    case 0:
        printf("\tNone");
        break;
    case 1:
        printf("\tChicken");
        break;
    case 2:
        printf("\tPork");
        break;
    case 3:
        printf("\tFish");
        break;
    case 4:
        printf("\tBeef");
    }
    }   
    
    else
    {
    printf("Invalid input resetting order.");
    inputOrder();
    }   

    printf("\n\tSide: \t");
    scanf("%d", &side);
    
    if(side <= 4)
    {
    switch(side)
    {
    case 0:
        printf("\tNone");
        break;
    case 1:
        printf("\tSteamed Rice");
        break;
    case 2:
        printf("\tShredded Corn");
        break;
    case 3:
        printf("\tMashed Potatoes");
        break;
    case 4:
        printf("\tSteamed Vegetables"); 
    }
    }
    else 
    {
    printf("Invalid input resetting order.");
    inputOrder();
    }

    printf("\n\tDrink: \t");
    scanf("%d", &drink);
    
    if (drink <= 4)
    {
    switch(drink)
    {
    case 0:
        printf("\tNone");
        break;
    case 1:
        printf("\tMineral Water");
        break;
    case 2:
        printf("\tIced Tea");
        break;
    case 3:
        printf("\tSoda");
        break;
    case 4:
        printf("\tFruit Juice");
    }
    
    }
    
    else 
    {
    printf("Invalid input resetting order.");
    inputOrder();   
    }
    
    printf("\nIs this meal set order correct? (1 - Yes / 2 - No): ");
    scanf("%d", &modify);   
    
    if (modify == 1)
    order++;
    
    if (modify == 2)
    {
    printf(" \nChange order or cancel? (1 - Change order / 2 - Cancel): ");
    scanf("%d", &cont);
    if (cont == 1)
    order = order;
    if (cont == 2)
    inputOrder();
    }   
    }while (order < 4); 
}
oug3syen

oug3syen1#

你应该用struct来做类似的事情。用struct你可以很容易地做这样的事情。如果你不知道什么是struct或者它们是如何工作的,this解释得很好。
为了让你更清楚地了解它,我们用一个结构体把几个相关的变量组合在一起。这有点像其他编程语言中的类,但大多数情况下只保存变量。

// Item struct for a food item like a cola for example
// With this struct you store a cost and a name to Item in the same location
// If you now instantiate(create a struct) like this: struct myItem item
// You can access the struct like so: myItem.cost
// And change the value of the variable like this: myItem.cost = 5
struct Item {
    int cost;
    string name; // this is optional just for show
}

// an order struct for an order. Example: a pizza as a  
// main, a cola as a drink and a salad as side
struct Order {
    struct Item main[]; // an array to store all mains
    struct Item sides[]; // an array to store all sides
    struct Item drinks[]; // an array to store all drinks
}

void inputOrder(Order order) {
    int mainPrice = 0;
    int sidesPrice = 0;
    int drinksPrice = 0;
    int orderPrice = 0;
    
    // Loop over all mains and add their cost together
    for(int i = 0;i < size(order.main);i++)
    {
        mainPrice += order.main[i].cost;
    }

    // Loop over all sides and add their cost together
    for(int i = 0;i < size(order.sides);i++)
    {
        sidesPrice += order.sides[i].cost;
    }
    // Loop over all drinks and add their cost together
    for(int i = 0;i < size(order.drinks);i++)
    {
        drinksPrice += order.drinks[i].cost;
    }

    orderPrice = mainPrice + sidesPrice + drinksPrice;
    
}

相关问题