Needing help with my loop

I was wondering how I could fix this loop to keep asking the user to input a number after the wrong number. I also am trying to figure out what I broke.
Thank you for all the help and I hope you have a happy Easter!

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
  #include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
    string product = ""; // this variable is used to store the product bought
    int input; // to store user input
    cout << "What would you like to buy?" << endl;
    cout << "1. Bread:" << endl;
    cout << "2. Milk:" << endl;
    cout << "3. Soap:" << endl;
    cout << "4. Eggs:" << endl;
    cout << "5. Deodorant:" << endl;
    cout << "6. Juice:" << endl;
    cout << "7. Chips:" << endl;
    cout << "8. Forks:" << endl;
    cout << "9. Spoons:" << endl;
    cout << "10. Cups:" << endl;
    cout << "Please enter your choice: ";
    cin >> input; // getting user input
    
if (input < 1 || input > 10)
    {
        cout << "Sorry, "<<input<<" wasn’t a valid choice" << endl;
        cout << "Please enter choice: ";
        cin >> input;
    }
    
    {
        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 != "")
    {
        float 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;
        float tax = 0;
        if (product == "Soap","Deodorant") // If product is not grocery
        {
            tax = 0.08 * price; // Calculating tax
        }
        
        float discount = 0;
        if (age >= 60) // If age is greater than 60
        {
            discount = 0.05 * price; // Calculating discount
        }
        cout << "Invoice " << endl;
        cout << "-----------" << endl;
        cout << product << " price: $" << fixed << setprecision(2) << price << endl;
        cout << "Tax: $" <<fixed<<setprecision(2)<<tax <<endl;
        if (discount > 0)  // If discount greater than 0 
        {
            cout << "Discount: $-"<<fixed << setprecision(2) << discount << endl;
        }
        float total = price + tax - discount; // Calculating total price
        cout << "Total: $" << fixed << setprecision(2)<<total << endl; // Displaying total price
    }
}
Your program does not contain any loops.

What does condition in line 81 do?


A lookup table would shorten your code.
1
2
3
vector<string> types[] { "", "Bread", "Milk" };
for ( int i=1; i<types.size(); ++i ) cout << i << ". " << types[i] << '\n';
if ( 0<input and input<types.size() ) product = types[input];
keep asking the user to input a number after the wrong number


Consider which only allows valid input:

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
#include <iostream>
#include <string>
#include <iomanip>
#include <limits>
using namespace std;

int getInt(const std::string& prm)
{
	int i {};

	while ((std::cout << prm) && (!(std::cin >> i) || std::cin.peek() != '\n')) {
		std::cout << "Not an integer\n";
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	return i;
}

double getNum(const std::string& prm)
{
	double i {};

	while ((std::cout << prm) && (!(std::cin >> i) || std::cin.peek() != '\n')) {
		std::cout << "Not a number\n";
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	return i;
}

int main()
{
	constexpr const char* const products[] {"Bread", "Milk", "Soap", "Eggs", "Deodorant", "Juice", "Chips", "Forks", "Spoons", "Cups"};

	cout << "What would you like to buy?\n";

	for (size_t no = 0; const auto & p : products)
		std::cout << std::setw(2) << ++no << "  " << p << '\n';

	int input {};

	while ((input = getInt("Please enter your choice: ")) < 1 || input > 10)
		cout << "Sorry, " << input << " isn't a valid choice\n";

	const string product {products[input - 1]};
	const auto price {getNum("Please enter price for " + product + ": $")};
	const auto age {getInt("Please enter your age: ")};
	const auto tax {product == "Soap" || product == "Deodorant" ? 0.08 * price : 0.0};
	const auto discount {age >= 60 ? 0.05 * price : 0.0};

	cout << "Invoice\n";
	cout << "-----------\n";
	cout << product << " price: $" << fixed << setprecision(2) << price << '\n';
	cout << "Tax: $" << fixed << setprecision(2) << tax << '\n';

	if (discount > 0)
		cout << "Discount: $-" << fixed << setprecision(2) << discount << '\n';

	const auto total {price + tax - discount};

	cout << "Total: $" << fixed << setprecision(2) << total << '\n';
}


Hello akeilo,

Here is something to think about:
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
#include <iostream>
#include <iomanip>
#include <limits>  // <--- Added.
#include <string>

using namespace std;

int main()
{
    constexpr int MINCHOICE{ 1 }, MAXCHOICE{ 10 };  // <--- Change "MAXCHOICE" if menu choices change.
    //constexpr double prices[]{ 0.0, 1.50, 2.50 };  // <--- Why are you letting the user enter a price?

    string product; // this variable is used to store the product bought. Empty when defined. Does not need initialized.
    int input; // to store user input

    std::cout << std::fixed << std::setprecision(2);  // <--- Only needs done once. I usually put this at the top.

    cout <<
        "\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";
                          // <--- Consider 11 Exit.

    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";

            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
        }
        else if (input < MINCHOICE || input > MAXCHOICE)
        {
            cerr << "\n     Sorry, " << input << " wasn't a valid choice\n\n";
        }
    }

    //{  // <--- You do not need the {}s.
    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;

        double tax = 0;

        if (product == "Soap", "Deodorant") // If product is not grocery
        {
            tax = 0.08 * price; // Calculating tax
        }

        double discount = 0;

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

        cout << "Invoice " << endl;
        cout << "-----------" << endl;
        cout << product << " price: $" << fixed << setprecision(2) << price << endl;
        cout << "Tax: $" << fixed << setprecision(2) << tax << endl;

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

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

        cout << "Total: $" << fixed << setprecision(2) << total << endl; // Displaying total price
    }
    //}
}

This is a start.

There are some other things I have noticed. I am think of something like constexpr double SALESTAX{ 0.08 };. You are letting the user enter a price. This should be part of the program and just used. That would be line 11.

I was wondering if you have learned about "switch/case" yet?

Your program only runs once allowing only 1 item to be chosen. Eventually this would be better in a loop allowing multiple choices.

Prefer using "double"s over "floats" These days a number like "0.08" is considered a double and trying to stuff that, or a calculation, into a float will have data loss.

Another thing is to prefer using the new line, (\n), over "endl". "endl" is a function that takes time to process. The more you have the longer it takes the program to run.

Line 19 demonstrates that you do not need a "cout" for every line. In this case what looks like 12 individual strings is considered 1 big string by the IDE and compiler. One advantage is that it looks more like what will be displayed on the screen, so you have a better idea of what it will look like.

The biggest change starts at line 33. This is designed to check for a non numeric input and out of the valid range.

See what you think.

Andy
Topic archived. No new replies allowed.