Help with bool

A vending machine serves chips, fruit, nuts, juice, water, and coffee. The machine owner wants a daily report indicating what items sold that day. Given boolean values (1 or 0) indicating whether or not at least one of each item was sold, output a list for the owner. If all three snacks were sold, output "All snacks" instead of individual snacks. Likewise, output "All drinks" if appropriate. For coding simplicity, output a space after every item, even the last item.

//The code below is probably wrong, but this is all I have got.
int main() {

bool chipsSold, fruitSold, nutsSold; // Snack items
bool juiceSold, waterSold, coffeeSold; // Drink items

cin >> chipsSold;
cin >> fruitSold;
cin >> nutsSold;
cin >> juiceSold;
cin >> waterSold;
cin >> coffeeSold;

if(chipsSold == 1)
{
cout<<"Chips ";
}
else
{
return 0;
}

if(fruitSold == 1)
{
cout<<"Fruit ";
}
else
{
return 0;
}
if(nutsSold==1)
{
cout<<"Sold ";
}
else
{
return 0;
}
return 0;
}
Your headed in the right direction but why are you returning 0 if you don't sell any chips? why would you want to end the program if you sell chips or fruit or nuts?

Start by removing all your else statements. So far you're good if they are not all ordered then the program does as you want but you need to figure out how if all the snacks are ordered to output "All snacks". Hint: You will be using nested if statements. The drinks are all the same idea as the snacks.
Last edited on
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
#include <iostream>

int main()
{
    bool chipsSold = false, fruitSold = false, nutsSold = false; // Snack items
    bool juiceSold = false, waterSold = false, coffeeSold = false; // Drink items

    bool fQuit = false;
    while (!fQuit)
    {
        std::cout << "select item: 1. chips 2. fruit 3. nuts 4. juice 5. water 6. coffee 7. quit \n";
        int choice{};
        std::cin >> choice;
        std::cin.ignore();
        switch (choice)
        {
            case 1:
                if(chipsSold == false)chipsSold = true; break;
            case 2:
                if(fruitSold == false)fruitSold = true; break;
            case 3:
                if(nutsSold == false)nutsSold = true; break;
            case 4:
                if(juiceSold == false)juiceSold = true; break;
            case 5:
                if(waterSold == false)waterSold = true; break;
            case 6:
                if(coffeeSold == false)coffeeSold = true; break;
            case 7:
                fQuit = true; break;
            default:
                std::cout << "wrong entry, try again \n"; break;
        }
    }
    if (chipsSold && fruitSold && nutsSold)std::cout << "All snacks \n";
    if (juiceSold && waterSold && coffeeSold)std::cout << "All drinks \n";
}
Topic archived. No new replies allowed.