Needing help with functions.

I was wondering what the best way would be for me to put my code into functions would be. I'm still trying to understand how they work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>
#include <iomanip>
#include <limits> 
#include <string>

using namespace std;

int main()
{
    constexpr int MINCHOICE{ 1 }, MAXCHOICE{ 10 };  //Minimum and maximum choices

    string product; //Hold product
    int input; // To store user input

    std::cout << std::fixed << std::setprecision(2); //Fixed, setprecision

    cout << //User choices
        "\n"
        " What would you like to buy?\n"
        "   1. Bread:\n"
        "   2. Milk:\n"
        "   3. Soap:\n"
        "   4. Eggs:\n"
        "   5. Deodorant:\n"
        "   6. Juice:\n"
        "   7. Chips:\n"
        "   8. Forks:\n"
        "   9. Spoons:\n"
        "  10. Cups:\n";

    while (std::cout << "  Please enter your choice: " && !(cin >> input) || input < MINCHOICE || input > MAXCHOICE)  //Getting user input
    {
        if (!std::cin)
        {
            std::cerr << "\n     Invalid input!\n\n"; //Error output

            std::cin.clear(); //Clear input
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //Error loop
        }
        else if (input < MINCHOICE || input > MAXCHOICE)
        {
            cerr << "\n     Sorry, " << input << " wasn't a valid choice\n\n"; //Error output
        }
    }

    //Setting number input to product
    if (input == 1) 
    {
    product = "Bread";
    }
    else if (input == 2)
    {
    product = "Milk";
    }
    else if (input == 3)
    {
    product = "Soap";
    }
    else if (input == 4)
    {
    product = "Eggs";
    }
    else if (input == 5)
    {
    product = "Deodorant";
    }
    else if (input == 6)
    {
    product = "Juice";
    }
    else if (input == 7)
    {
    product = "Chips";
    }
    else if (input == 8)
    {
    product = "Forks";
    }
    else if (input == 9)
    {
    product = "Spoons";
    }
    else if (input == 10)
    {
    product = "Cups";
    }

    if (product != "")
    {
        double price; // To store price of product
        int age; // To store age of customer

        cout << "Please enter price for " << product << ": $"; // Asking the user for price
        cin >> price;

        cout << "Please enter your age: "; // Asking the user for age
        cin >> age;

        const double SALESTAX = 0.08; //hold tax
        double totalTAX = 0.00; 
        const double noTAX = 0.00;
        
        if (product == "Soap" || product == "Deodorant" || product == "Forks" || product == "Spoons" || product == "Cups") // If product is not grocery
        {
            totalTAX = SALESTAX * price; // Calculating tax
        }

        double discount = 0; //Hold discount

        if (age >= 60) // If age is greater than 60
        {
            discount = 0.05 * price; // Calculating discount 
        }

        cout << "Invoice " << endl;
        cout << "-----------" << endl;
        cout << product << " price: $" << price << endl; //Product price
        cout << "Tax: $" << totalTAX << endl; //Tax output

        if (discount > 0)  // If discount greater than 0 //Discount check
        {
            cout << "Discount: $-" << discount << endl; //Displaying discount
        }

        double total = price + totalTAX - discount; // Calculating total price

        cout << "Total: $" << total << endl; // Displaying total price
        cout << "-----------" << endl;
    }
}
@OP
Your problem is directly analagous to this one and the ways of arranging data and using functions:
http://www.cplusplus.com/forum/beginner/277979/#msg1200020
Hello spicyz,

I must say that code looks very familiar.

As a start I would take lines 17 - 44 and create a function "MainMenu". Then I pute the code in a do/while loop and only return a valid choice. If you want the practice you can pass " MINCHOICE" and " MAXCHOICE" to the function or define them in the function.

You could put the if/else if statements in a function. If you have covered "switch/case" that would be a better choice and it would shorten the lines of code needed.

For line 88 you could use: if (!product.empty()). This returns a bool type that the if condition can easily use. http://www.cplusplus.com/reference/string/string/empty/

Parts of code that calculate tend to be a good choice for a function, but may not always be the best idea.

The part that prints the invoice could be put in a function.

Andy
One way is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>

using namespace std;

constexpr int MINCHOICE {1}, MAXCHOICE {10};  //Minimum and maximum choices
const string names[] {"Bread", "Milk", "Soap", "Eggs", "Deodorant" "Juice", "Chips", "Forks", "Spoons", "Cups"};

void header() {
	cout <<"\n What would you like to buy?\n";

	for (size_t itm {}; const auto& nam : names)
		cout << ++itm << ". " << nam << '\n';
}

int getChoice()
{
	int input {};

	while (std::cout << "  Please enter your choice: " && !(cin >> input) || input < MINCHOICE || input > MAXCHOICE)  //Getting user input
		if (!std::cin) {
			std::cerr << "\n     Invalid input!\n\n"; //Error output

			std::cin.clear(); //Clear input
			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //Error loop
		} else
			if (input < MINCHOICE || input > MAXCHOICE)
				cerr << "\n     Sorry, " << input << " wasn't a valid choice\n\n"; //Error output

	return input;
}

string getProd(int prodno) {
	return names[prodno - 1];
}

double getPrice(const string& product) {
	double price {};

	cout << "Please enter price for " << product << ": $"; // Asking the user for price
	cin >> price;

	return price;
}

int getAge() {
	int age {};

	cout << "Please enter your age: "; // Asking the user for age
	cin >> age;

	return age;
}

void invoice(const string& product, double price, int age) {
	const double SALESTAX {0.08}; //hold tax
	const double DISC {0.05};	// hold discount

	double totalTAX {}, discount {};

	if (product == "Soap" || product == "Deodorant" || product == "Forks" || product == "Spoons" || product == "Cups") // If product is not grocery
		totalTAX = SALESTAX * price; // Calculating tax

	if (age >= 60) // If age is greater than 60
		discount = DISC * price; // Calculating discount

	cout << std::fixed << std::setprecision(2); //Fixed, setprecision

	cout << "Invoice \n";
	cout << "-----------\n";
	cout << product << " price: $" << price << '\n'; //Product price
	cout << "Tax: $" << totalTAX << '\n'; //Tax output

	if (discount > 0)  // If discount greater than 0 //Discount check
		cout << "Discount: $-" << discount << '\n'; //Displaying discount

	cout << "Total: $" << (price + totalTAX - discount) << '\n'; // Displaying total price
	cout << "-----------" << '\n';

}

int main()
{
	header();

	const auto product {getProd(getChoice())};
	const auto price {getPrice(product)};
	const auto age {getAge()};

	invoice(product, price, age);
}

Hello spicyz,

I found why this looks so familiar.

If this is a school assignment it would help if you post the directions. Someone may see something that you missed.

As is the program may work, but it is not the best way to go about it.

Andy
Topic archived. No new replies allowed.