Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <time.h>
- using namespace std;
- void safeInt(int, int*);
- void safeDouble(double, double&);
- struct Product {
- string name;
- double price;
- int amount;
- };
- Product readProduct();
- int main() {
- const int AANTAL = 2;
- Product example[AANTAL];
- for (int i = 0; i < AANTAL; i++) {
- example[i] = readProduct();
- }
- return 0;
- }
- void safeInt(int min , int* aantal) {
- while (!(cin >> *aantal) || *aantal < min) {
- cin.clear();
- cin.ignore(INT_MAX, '\n');
- cout << "\nWrong input";
- }
- cin.ignore(INT_MAX, 'n');
- }
- void safeDouble(double min, double& price) {
- while (!(cin >> price) || price < min) {
- cin.clear();
- cin.ignore(INT_MAX, '\n');
- cout << "\nWrong input";
- }
- cin.ignore(INT_MAX, '\n');
- }
- Product readProduct() {
- Product a;
- cout << "Give the name of your product: ";
- do {
- cin >> a.name;
- } while (a.name == "");
- cout << "\nGive the price of your product: ";
- safeDouble(0, a.price);
- cout << "How much of these products do you own?";
- safeInt(0, &a.amount);
- return a;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement