Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <time.h>
  4.  
  5. using namespace std;
  6.  
  7. void safeInt(int, int*);
  8. void safeDouble(double, double&);
  9.  
  10. struct Product {
  11.  
  12. string name;
  13. double price;
  14. int amount;
  15.  
  16.  
  17. };
  18.  
  19. Product readProduct();
  20.  
  21.  
  22. int main() {
  23. const int AANTAL = 2;
  24. Product example[AANTAL];
  25.  
  26. for (int i = 0; i < AANTAL; i++) {
  27. example[i] = readProduct();
  28. }
  29.  
  30.  
  31. return 0;
  32. }
  33.  
  34. void safeInt(int min , int* aantal) {
  35.  
  36. while (!(cin >> *aantal) || *aantal < min) {
  37. cin.clear();
  38. cin.ignore(INT_MAX, '\n');
  39. cout << "\nWrong input";
  40. }
  41. cin.ignore(INT_MAX, 'n');
  42.  
  43. }
  44.  
  45. void safeDouble(double min, double& price) {
  46.  
  47. while (!(cin >> price) || price < min) {
  48. cin.clear();
  49. cin.ignore(INT_MAX, '\n');
  50. cout << "\nWrong input";
  51. }
  52. cin.ignore(INT_MAX, '\n');
  53.  
  54. }
  55.  
  56. Product readProduct() {
  57. Product a;
  58. cout << "Give the name of your product: ";
  59. do {
  60. cin >> a.name;
  61. } while (a.name == "");
  62. cout << "\nGive the price of your product: ";
  63. safeDouble(0, a.price);
  64.  
  65. cout << "How much of these products do you own?";
  66. safeInt(0, &a.amount);
  67.  
  68. return a;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement