0

尝试创建一个程序,该程序采用咖啡风味插件并使用数组检查它是否有效。
如果有效,它使用数组索引来收集价格信息。

我设法编写了下面的代码,但它只适用于 1 次迭代。
如何更改它以便用户可以输入:Cream and cinnamon 并输出每个加载项的总和以及一杯咖啡的总价格?一杯咖啡的起价为 2.00 美元

#include <iostream>
#include <string>

using namespace std;

int main()
{
    // Declare variables.
    string addIn;               // Add-in ordered
    const int NUM_ITEMS = 5;    // Named constant

    // Initialized array of add-ins
    string addIns[] = { "Cream", "Cinnamon", "Chocolate", "Amaretto", "Whiskey" };
    // Initialized array of add-in prices
    double addInPrices[] = { .89, .25, .59, 1.50, 1.75 };

    bool foundIt = false;        // Flag variable
    int x;                       // Loop control variable
    double orderTotal = 2.00;    // All orders start with a 2.00 charge
    string QUIT = "XXX";

    // Get user input
    cout << "Enter coffee add-in or XXX to quit: ";
    cin >> addIn;

    // Write the rest of the program here.
    for (x = 0; x < NUM_ITEMS && foundIt == false && addIn != QUIT; x++) {
        if (addIn == addIns[x]) {
            foundIt = true;
            x--;
        }
    }
    if (foundIt == true) {
        cout << addIns[x] << " $" << addInPrices[x] <<endl;
        cout << "$" << orderTotal + addInPrices[x] <<endl;
    }
    else {
        cout << "Sorry, we do not carry that." <<endl;
        cout << "Order total is $ " << orderTotal <<endl;
    }

    return 0;
}
4

3 回答 3

1

不要使用并行数组——你会搞砸维护它们。

更好的选择:

struct为您的加载项创建一个:

struct Addin {
  std::string name;
  double price;
}

并使用这些结构的一个数组(或者更好的一个std::vector)。

另一种选择是使用map

std::map<std::string, double> addIns = {
    {"Cream", .89},
    {"Cinnamon", .25},
    // etc.
};

然后你不需要循环,只需查找

auto item = addIns.find(addIn);
if(item != addIns.end() {
  // do your math here
}
于 2021-09-23T03:45:30.417 回答
0

您正在寻找的结构是 while 或 do/while 循环。为了能够输入“空”行,请使用 std::getline from。

您的程序结构将如下所示: 如您所见,我有将布尔表达式更改为函数(谓词)的习惯。这使代码更具可读性,并且谓词可在其他代码位中重用。

#include <iostream>
#include <string>

bool is_quit(const std::string& input)
{
    return input.length() > 0;
}

bool is_valid_input(const std::string& input)
{
    return true; // your own check
}

int main()
{
    bool quit = false;
    std::string input;

    do
    {
        std::cout << "give input : ";
        std::getline(std::cin, input);

        quit = is_quit(input);
    
        if (is_valid_input(input) && !quit)
        {
            std::cout << "ok" << std::endl;
        }

    } while (!quit);

    return 0;
}
于 2021-09-23T03:49:52.880 回答
0

您的程序被编写为获得单个输出。对于多个输出,必须有循环,并且not found条件也必须重写。

试试这个

#include <iostream>
#include <string>

using namespace std;

int main()
{
    // Declare variables.
             
    const int NUM_ITEMS = 5;        // Named constant
    string addIn[NUM_ITEMS];        // Add-in ordered

    // Initialized array of add-ins
    string addIns[] = { "Cream", "Cinnamon", "Chocolate", "Amaretto", "Whiskey" };
    // Initialized array of add-in prices
    double addInPrices[] = { .89, .25, .59, 1.50, 1.75 };

    //bool foundIt = false;        // Flag variable
    int x, i, j;                       // Loop control variable
    double orderTotal = 2.00;    // All orders start with a 2.00 charge
    string QUIT = "XXX";

    // Get user input
    cout << "Enter coffee add-ins followed by XXX to quit: ";
    for(i=0; i<NUM_ITEMS; i++) {
        cin >> addIn[i];
        if(addIn[i] == QUIT) {
            i++;
            break;
        }
    }

    int foundIt[i];

    // Write the rest of the program here.
    
    for(j=0; j<i; j++) {
        foundIt[j] = -1;
        for(x = 0; x<NUM_ITEMS && foundIt[j] == -1 && addIn[j] != QUIT; x++) {
            if (addIn[j] == addIns[x]) {
                foundIt[j] = x;
            }
        }
    }
    
    for(j=0; j<i-1; j++) {
        cout << addIn[j];
        if(foundIt[j] != -1) {
            cout << " $" << addInPrices[foundIt[j]] << endl;
            orderTotal = orderTotal + addInPrices[foundIt[j]];
        }
        else {
            cout << " - Sorry, we do not carry that." <<endl;
        }
    }

    cout << "$" << orderTotal <<endl;
    
    return 0;
}

样本输出

Enter coffee add-ins followed by XXX to quit: Cream Cinnamon XXX
Cream $0.89
Cinnamon $0.25
$3.14
Enter coffee add-ins followed by XXX to quit: Cream Onion XXX
Cream $0.89
Onion - Sorry, we do not carry that.
$2.89

我所做的是用大小而不是变量制作addInsrings数组。NUM_ITEMS此外,foundIt还制作了一个整数数组,以跟踪在addIns数组中找到项目的索引以及-1如果未找到。

To only access the items that user has entered in addIn, your QUIT has been made the termination condition in that array.

于 2021-09-23T04:14:31.693 回答