-3

Trying to wrap up my invoice program. I'm trying to get my customerInvoice function to properly display. However whenever I try to use it, I get an error. For some reason this function is having a difficult time displaying, and I'm not sure what the complication is. I can enter my values in just fine, none of the other variables are having a hang-up, I'm just getting a bit stalled on this part as its not properly displaying the message inside the customerInvoice function.

I also want my invoice to recognize the senior discount: if the customers age is over 60, then they get the 0.05 multiplier discount on their purchase.

#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;
}


  • In `main()`, replace this `customerInvoice` with `customerInvoice(choice, price, age);` i.e. pass the arguments to `customerInvoice()` function. – H.S. Mar 26 '23 at 03:11
  • 3
    *For some reason this function is having a difficult time displaying* -- You need to tell us exactly what "a difficult time displaying" means. The program is doing exactly as you wrote it. – PaulMcKenzie Mar 26 '23 at 03:11
  • 1
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Mar 26 '23 at 03:24
  • 1
    Welcome to Stack Overflow. "However whenever I try to use it, I get an error." **What error**? **What do you mean by** "try to use it"? Is it when you are **compiling** the program, or **running** it? "For some reason this function is having a difficult time displaying, and I'm not sure what the complication is." **What does that mean**? **What happens** when you "try to use it" (as you say), and **how is that different** from what you expect to happen? – Karl Knechtel Mar 26 '23 at 03:50
  • Also, please try to think clearly about the problem, and then describe clearly. For example, you aren't trying to "display the function"; rather, the *code in the function* is written to display *some information*. Right? So. **Does the function get called**? (Do you know how to check?) If it gets called, **what happens when it is called**? Is **anything** displayed? Is it the wrong thing? Just what? – Karl Knechtel Mar 26 '23 at 03:51
  • In your own words, where the code says `customerInvoice;`, exactly what do you think this should mean? Why should it mean that? For example, do you think this should cause the function to run? Why? (Hint: elsewhere in the code, where you write `getIntValue("your choice");`, **why did you (correctly) not** just write `getIntValue`?) – Karl Knechtel Mar 26 '23 at 03:52
  • As on [a question you deleted](https://stackoverflow.com/questions/75845432/), the code in this question fails to be a [mre]. Your code attempts to show printing invoices instead of of focusing on demonstrating the error. Please focus your code so that there is a chance of it helping someone else in the future (and to make it easier on the people **volunteering** to help you). – JaMiT Mar 26 '23 at 05:34

2 Answers2

2

As noted in comments, the following in main does not equate to calling a function:

customerInvoice;

Rather you want:

customerInvoice(choice, price, age);
Chris
  • 26,361
  • 5
  • 21
  • 42
2

In the main() function, you have this statement:

customerInvoice;

customerInvoice is name of function and a function name is an expression, here it's result is unused. When compiling your program, compiler is giving this warning:

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

Instead, call customerInvoice() function and pass the appropriate arguments to it:

customerInvoice(choice, price, age);

> I also want my invoice to recognize the senior discount: if the customers age is over 60, then they get the 0.05 multiplier discount on their purchase.

Looks like you need to call customerAge() instead of seniorCustomer() function in customerInvoice() function. Moreover, there is no seniorCustomer() function in your program.
It would be much better if you rename your customerAge() function to seniorCustomer() as it is returning whether customer is a senior customer or not.

H.S.
  • 11,654
  • 2
  • 15
  • 32