在我的 C++ 发票程序上打印出我的“收据”时遇到问题:功能由于某种原因无法正常工作

问题描述 投票:0回答:2

试图结束我的发票程序。我正在尝试让我的 customerInvoice 函数正确显示。但是,每当我尝试使用它时,都会出现错误。出于某种原因,这个功能很难显示,我不确定是什么问题。我可以很好地输入我的值,其他变量都没有挂起,我只是在这部分有点停滞,因为它没有正确显示 customerInvoice 函数中的消息。

我还希望我的发票能够识别老年人折扣:如果客户年龄超过 60 岁,那么他们在购买时可以获得 0.05 倍的折扣。

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;


const float flatTaxRate = 0.08;
const float seniorDiscountRate = 0.05;

int getIntValue (string toInput){
    cout << "Please enter " << toInput <<": ";
    int value;
    cin >> value;
    return value;
}

string choiceOfItem(int choice) {
    return choice == 1 ? "Bread" :
           choice == 2 ? "Milk" :
           choice == 3 ? "Soap" :
           choice == 4 ? "Eggs" :
           choice == 5 ? "Deodorant" :
           choice == 6 ? "Juice" :
           choice == 7 ? "Chips" :
           choice == 8 ? "Forks" :
           choice == 9 ? "Spoons" :
           choice == 10 ? "Cups" :
    "Invalid Item: Select Again";
}

int getIntInputValues (string toInput){
    cout << "Please enter " << toInput <<": ";
    int value;
    cin >> value;
    return value;
}

double getDoubleInputValues (string toInput){
    cout << "Please enter " << toInput <<": ";
    double value;
    cin >> value;
    return value;
}

bool customerAge(int age){
    const int seniorCustomer = 60;
    return age >= seniorCustomer;
}

void customerInvoice(int choice, double price, int age)
{
    cout << "-----------------------------------\n";
    cout << "\n" << "Invoice" << "\n";
    //can use \n as a tab
    
    if(seniorCustomer(age))
    {
        price -= price * seniorDiscountRate;
    }
     
    cout << choiceOfItem(choice) <<":" << "\n" <<"$"<< price << endl;
    cout << "Tax:" << "\n" <<"$"<< price * flatTaxRate << endl;
    cout << "Discount:" << "\n" <<"$"<< "-" << price * seniorDiscountRate << 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 = getDoubleInputValues("price");

    
    int age = getIntInputValues("age");

       
    bool seniorCustomer;
       
    cout << " " << endl;
        
    customerInvoice;
    
    cout << choiceOfItem(choice) << endl;
    cout << price << endl;
    cout << age << endl;
    cout << seniorCustomer;
    
    return 0;
}


c++ function invoice
2个回答
0
投票

如评论中所述,

main
中的以下内容不等同于调用函数:

customerInvoice;

而你想要:

customerInvoice(choice, price, age);

0
投票

main()
函数中,你有这样的语句:

customerInvoice;

customerInvoice
是函数名,函数名是一个表达式,这里它的结果是未使用的。编译程序时,编译器发出此警告:

warning: expression result unused [-Wunused-value]
customerInvoice;
^~~~~~~~~~~~~~~

相反,调用

customerInvoice()
函数并将适当的参数传递给它:

customerInvoice(choice, price, age);

> 我还希望我的发票能够识别高级折扣:如果客户年龄超过 60 岁,那么他们在购买时可以获得 0.05 倍的折扣。

看起来您需要在

customerAge()
函数中调用
seniorCustomer()
而不是
customerInvoice()
函数。而且,您的程序中没有
seniorCustomer()
功能。
如果您将
customerAge()
函数重命名为
seniorCustomer()
会更好,因为无论客户是否是高级客户,它都会返回。

© www.soinside.com 2019 - 2024. All rights reserved.