使用 C++ 编写收据程序。我有点墨守成规。我们需要使用一个函数在调用后打印出发票的“收据”。我的问题之一是我的发票功能很难读取我的值。这是因为我需要另一个函数来为我的输入选择一个字符串值(即我需要将字符串附加到特定的整数)。需要帮助来打印我的函数。
//what I'm using to choose items from my invoice
int getIntValue/getInputValues/getDoubleValues (string toInput){
cout << "Please enter " << toInput <<": ";
double/int value;
cin >> value;
return value;
}
string itemChoice(int choice) {
return choice == 1 ? "Bread" :
choice == 2 ? "Milk" :
choice == 3 ? "Soap" :
choice == 4 ? "Eggs" :
choice == 5 ? "Deodorant" :
"Invalid Item: Select Again";
}
// what I'm using to print out my invoice values
void Invoice(int choice, string item, double price, int age)
{
cout << "-----------------------------------\n";
cout << "\n" << "Invoice" << "\n";
//can use \n as a tab
cout << choiceOfItem(choice) <<":" << "\n" <<"$"<< price << endl;
cout << "Tax:" << "\n" <<"$"<< price * flatTaxRate << endl;
cout << "Total:" << "\n"<< "$" << price + (price * flatTaxRate) << endl;
cout << "-----------------------------------\n";
}
int main()
{
const int MIN_VALID = 1;
const int MAX_VALID = 10;
cout << "What would you like to buy?" << endl;
for (int choice = MIN_VALID; choice <=MAX_VALID; ++choice)
{
cout << choice << ". " << choiceOfItem(choice)<< endl;
}
cout << endl;
int choice = getIntValue("your choice");
while (choice < MIN_VALID || choice > MAX_VALID)
{
cout << "Sorry, " << choice << " wasn't a valid choice" << endl;
cout << "Please try again:" << endl;
choice = getIntValue("your choice");
}
double price = getDoubleValues("price");
int age = getInputValues("age");
cout << " " << endl;
cout << customerInvoice(choice, price, age) << endl;
return 0;
}
当我尝试输入 customerInvoice 时,我遇到了一个错误: main.cpp:在函数“int main()”中: main.cpp:137:37: 错误:无法将“price”从“double”转换为“std::string”{又名“std::__cxx11::basic_string”} 137 |计算<< customerInvoice(choice, price, age) << endl; | ^~~~~ | | | double
我的代码中显然还有其他内容,但我只是展示了我认为与打印收据相关的内容。在这方面我希望得到一些帮助。