Question

Is there anyway you can store data in a c++ console application, transfer it to a different variable, then reuse the variable?

Why I want this: I want to have the user input order details for an order then for the program to be able to store them so the program can recreate another order.

Current Code:

int quant;
int cost;
int selling;
int profit;
    NewOrder:
cout <<""<< endl;
cout << "Enter an Order Number: " << flush;
getline(cin, ord);

cout << "Enter a Product ID: " << flush;
getline(cin, Prod);

cout << "Enter a Quantity: " << flush;
cin>>quant;
          Pricing:
cout << "Enter a cost per unit: " <<char(156) << flush;
cin >> cost;

cout << "Enter a selling price per unit: " <<char(156) << flush;
cin >> selling;

Sleep(2000);
cout << endl;
cout << "Your order Details are:" << endl;
cout << "Order Number: " << ord << endl;
cout << "Product: " <<Prod << endl;
cout << "Quantity:" << quant << endl;
cout << "Total Cost: " <<char(156) << (quant*cost) << endl;
cout << "Total Selling Price: " <<char(156)<< (quant*selling) << endl;

profit = ((quant*selling) - (quant*cost)); //Assigning a value to profit.

cout << "Total Profit: " <<char(156)<< profit << endl;
if (profit < 0) { 
    cout << "You have a negative profit. Please change your pricing." << endl;
    Sleep(3000);
    goto Pricing; } 

Currently, it lets the user input the details to one order and then displays them. I want to have it so the program can enter more then one order and going by order number can recall them. Can I use the programs memory to do this or will need set it to a SQL DB?

If so, how do I setup the SQL connection?

If I can do it within the memory, how? I have been looking around and I cant create and declare variables during runtime.

Was it helpful?

Solution

You could keep track of all the orders using a vector:

struct Product {
    //Add additional fields you need here
    int quant;
    int cost;
    int selling;
    int profit;
};

int main() {
    std::vector<Product> products;

    while (stillAddingProducts) {
        //Get all the data from the user
        Product p;
        p.quant = 10; //example you should get this from the user

        //Insert the product based on the information received
        products.push_back(p);
    }

    //Perhaps iterate through all products and display information
    for (const auto& e : products) {
        std::cout << "Quantity: " << e.quant << std::endl;
    }
}

With this you will have a 'fresh' Product object every iteration. On a sidenote, try to avoid using goto in your code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top