So I am writing a cash register program, person buys items and the number calculates. But I am stuck at looping, I want the user to pick to pick all his/her items first then after all the program is done I want the program to ask if the user wants to runt he program again. I am really stuck at these points. Picking items and looping. so far I got.

#include <iostream>

using namespace std;

int main()
{
    char Jeans = 60, a ;
    char WhiteTshirt=10;
    char Socks= 15;
    char Sneakers=80;
    char Checkout;
    int  Choice;
    int  total;

    int  b;
    int  c;
    int  d;


    cout <<"What would you like to purchase? \n";
    cout <<"1. Jeans            $60.00 \n";
    cout <<"2. White T-shirt    $10.00 \n";
    cout <<"3. Socks            $15.00 \n";
    cout <<"4. Sneakers         $80.00 \n";
    cout <<"5. Checkout                \n";
    cout <<"What would you like to do? \n";
    cin >> Choice;

{


    if(Choice == 1)
    {
         cout <<"How many Jeans would you like to purchase? \n";
         cin >> a;
         cout <<"Purchases = " << a << endl;
         total = Jeans * a;
         cout <<"Price =$" << total << endl;
    }
    else if(Choice == 2)
{
        cout <<"How many White T-shirts would you like to purchase? \n";
        cin >> b;
        total = WhiteTshirt * b;
        cout <<"Price =$" << total << endl;
}
    else if(Choice == 3)
{
        cout <<"How many Socks would you like to purchase? \n";
        cin >> c;
        total = Socks * c;
        cout <<"Price =$" << total << endl;
}
    else if(Choice == 4)
{
        cout <<"How many Sneakers would you like to purchse? \n";
        cin >> d;
        total = Sneakers * d;
        cout <<"Price =$" << total << endl;

}
    else if(Choice == 5)
{
        cout <<"Would you like to Checkout? \n";
        cin >> Checkout;
        total = 1 + 2 + 3 + 4;
        cout <<"Your total comes to =$" << total << endl;
}




}

    return 0;
}

So how do I make the user pick all his/her items firt before calculating, and looping program asking the user if he/she wants to run it again.

Recommended Answers

All 6 Replies

Add a choice "6. To exit" then add a while loop encompassing all the code:

While (choice != 6)
{
     // Code here
}

This gets checked at the begining and end of every loop checking the variable choice is not equal to 6 otherwise it will keep going.

Also try using at the top:

#define Socks 15
#define Underwear 10
#define Glasses 5

...instead for all your items. Then you can perform mathmatical functions on them

Also try using at the top:

#define Socks 15
#define Underwear 10
#define Glasses 5
...instead for all your items. Then you can perform mathmatical functions on them

Don't do this.

It's fine to do maths on char values (as long as you don't assign a value that overflows the char and fail to notice it). When you #define values like this you loose all type and scope information and you don't really gain anything.

Try this.

int main()
start:
...
}
cout << "Want to buy something else? Yes/No"<< endl;
string choice;
cin>>choice;
if(choice == "Yes"){
goto start;
}else{
return 0;
}
commented: Horrible advice. -3

Thanks for the information. really useful for me.

@Raisefamus
No need for a goto statement to simulate a loop, and here is the explanation.

@Feal456
You could use a while loop as mentioned before.
You could use also a for loop:

string Choice;
for (;;){
    cout<<"Choose: ";
    cin>>Choice;
    //if-else if conditions;
    else if (Choice == "6") break;
    else cout<<"Invalid choise."<<endl;
}

Here is another solution in which the articles are set once in fillProducts, the rest remains untouched if the number of products is changed. Also, fillProducs can have any other logic inside (e.g., reading from a file/database/...) as long as it provides the vector.

#include <iostream>
#include <vector>
#include <string>

using namespace std;

struct Product {
    string name;
    double price;
    int amount;
};

vector<Product> fillProducts()
{
    vector<Product> products;

    Product product;
    product.amount = 0;

    product.name = "Jeans";
    product.price = 60.00;
    products.push_back(product);
    product.name = "White T-shirt";
    product.price = 10.00;
    products.push_back(product);
    product.name = "Socks";
    product.price = 15.00;
    products.push_back(product);
    product.name = "Sneakers";
    product.price = 80.00;
    products.push_back(product);

    return products;
}

void printMenu(vector<Product> products)
{
    int i = 0;
    cout << "Choose your option:" << endl;
    for(i=0; i<products.size(); i++)
    {
        cout << i+1 << ". " << products[i].name << "   $"
             << products[i].price << endl;
    };
    cout << i+1 << ". Checkout" << endl;
    cout << i+2 << ". New procedure" << endl;
    cout << i+3 << ". Exit" << endl;
}

double getTotal(vector<Product> products)
{
    double total = 0.0;
    vector<Product>::const_iterator cii;
    for(cii=products.begin(); cii!=products.end(); cii++)
    {
        total += (*cii).price * (*cii).amount;
    };

    return total;
}

void newProcedure(vector<Product> &products)
{
    for(int i=0; i<products.size(); i++)
    {
        products[i].amount = 0;
    };
}

int main()
{
    int choice = 1;
    vector<Product> products = fillProducts();
    int number_of_products = products.size();

    while (choice > 0 && choice <= number_of_products+2)
    {
        printMenu(products);
        cout << "Your choice is: ";
        cin >> choice;
        if (choice <= number_of_products)
        {
            cout << "How many " << products[choice-1].name
                 <<" would you like to purchase? ";
            cin >> products[choice-1].amount;
            cout << "Purchases = " << products[choice-1].amount << endl;
            cout << "Price = $"
                 << products[choice-1].amount * products[choice-1].price
                 << endl;
        } else if (choice == number_of_products+1) {
            cout << "Checkout price = $" << getTotal(products) << endl;
            newProcedure(products);
        } else if (choice == number_of_products+2) {
            cout << "New procedure was chosen." << endl;
            newProcedure(products);
        };
        cout << endl;
    };

    cout << "Thank you for shopping with us! Bye!" << endl << endl;
    return 0;
}

This is but a simple solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.