Now i want to make a file that i can import into the program. In the file are different products with all an explanation of the price (priceProduct in the above code) quantity of the product and that kind of things, so that i only have to write the universal code above once and it can be used for all the products if.
I think i have to do something with classes, but i'm not sure. I even don't know if this is possible. If it is: could you explain how to do it to.
This indeed possible, and it can be made easier with structured data. I'll explain.
I. Organizing your dataFirst we'd like to know what kind of stuff we'd like to know about our product. You mentioned you'd like to have the name, price, and quantity of the product. So, we'll start there.
struct Product {
string name;
double price;
int qty;
};
Here, we declare a structure called "Product" as having three members: Name, price, and quantity. Any object of type "Product" will have these three properties. So how do we create and use a "Product"? Well, like any another variable, really:
Product milk;
milk.name = "milk";
milk.price = 2.75;
milk.qty = 8;
// or
// Product milk = { "milk", 2.75, 8 };
cout << "Product Name:\t" << milk.name << endl
<< "Product Price:\t" << milk.price << endl
<< "Product Quantity:\t" << milk.qty << endl;
Now we need a way to store all of the products together.
II. Managing the data genericallyHere we can use arrays. We could use std::vector for a dynamically sized array, but that is outside of the scope of this post. We'll use a static array instead:
const int MAX_PRODUCTS = 100;
Product products[MAX_PRODUCTS];
int numProducts = 0;
Now, we can add and manage the products programmatically:
// Populate the array of products
numProducts = 0;
products[numProducts++] = Product { "Milk", 2.75, 8 };
products[numProducts++] = Product { "Cookies", 4.60, 4 };
products[numProducts++] = Product { "Bread", 3.50, 12 };
products[numProducts++] = Product { "Eggs", 4.25, 16 };
// Display the array's contents.
for ( int idx = 0; idx < numProducts; ++idx ) {
cout << "Product Name:\t" << products[idx].name << endl
<< "Product Price:\t" << products[idx].price << endl
<< "Product Quantity:\t" << products[idx].qty << endl
<< endl;
}
With the array, we will no longer be able to refer to products by variable name (such as "milk" in the previous example). We'll either need to use pointers or indexes. Here's a sample using an index to find a product in the array:
string queryName;
cout << "Enter product name: ";
getline(cin, queryName);
int idx = 0;
for ( ; idx < numProducts; ++idx ) {
if ( products[idx].name == queryName )
break;
}
if ( idx < numProducts ) {
// do something with products[idx]
} else {
cout << "Unable to find \"" << queryName << "\"" << endl;
}
III. Structuring your fileNow, let's get to dynamically loading the products. Let's first create a file and create it with tab-separated values:
Milk 2.75 8
Cookies 4.60 4
Bread 3.50 12
Eggs 4.25 16
Next, let's place this file somewhere where your program can see it. Check your project settings for the working directory of your program. Place the file there.
// include fstream, sstream
ifstream fs("products.txt");
if ( !fs ) {
cout << "Unable to open file";
return;
}
string line;
numProducts = 0;
while ( getline(fs,line) ) { // Read one line at a time
istringstream is(line); // Temporary stream for the line
Product temp;
if ( getline(is,temp.name,'\t') && is >> temp.price >> temp.qty ) {
products[numProducts++] = temp;
} else
; // Error reading the line
}
So there you go. Hopefully it should be clear how this should work.