【发布时间】:2020-02-28 19:15:44
【问题描述】:
我应该使用一个并行数组来根据添加的插件显示一杯咖啡的量。原来的一杯咖啡是2美元。我主要对如何输出正确的结果感到困惑。目前,它将输出“Order total is2”。我错过了什么?
// JumpinJava.cpp - This program looks up and prints the names and prices of coffee orders.
// Input: Interactive
// Output: Name and price of coffee orders or error message if add-in is not found
#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
// Get user input
cout << "Enter coffee add-in or XXX to quit: ";
cin >> addIn;
// Write the rest of the program here.
for(int i = 0; i < NUM_ITEMS; i++){
if (addIns[i] == (addIn))
foundIt = true;
if (foundIt)
{
x = orderTotal + addInPrices[i];
cout << "Order Total is" << x << endl;
}
else cout <<"Sorry, we do not carry that."<< endl;
}
return 0;
} // End of main()
【问题讨论】:
-
您是否尝试使用调试器逐行执行代码?
-
x是一个int,它将截断双精度数。我怀疑这是故意的? (您的评论也说“流量控制变量”,这似乎也不准确)。推荐阅读:How to debug small programs -
问题是它正在显示输出。所以我没有收到任何错误。我相信是输出搞砸了,我只是不知道该怎么说
-
并行数组在哪里?
-
Double to int 截断不是错误(尽管一些编译器对此发出警告)。所以是的,你会得到输出。 for example
标签: c++ parallel-arrays