help with Implementing class Item

Pages: 1234
@AbstractionAnon thank you I tested it and its working perfectly.

Can someone help me out with the counter?
on L80 I will ask user how many objects they want and with their input I will print out the items accordingly..

should I create a new class for the counter or can I just add it to "class Fruit"?
Looking for feedback on this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Counter {
public:
  Counter() { ++count; }
  Counter(const Counter&) { ++count; }

  static size_t howMany()
  { return count; }

private:
  static size_t count;
};

template<typename T>
size_t Counter<T>::count = 0;   



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
// class
class Fruit
{
    //access specifier
public:
    //default constructor
    Fruit() {}
    // non-default constructor
    Fruit(
          const std::string& fruitName, int quantity, double weight,
          float price, bool ispurchased) :
    name(fruitName), is_purchased(ispurchased)
    {
        set_Quantity(quantity);
        set_Weight(weight);
        set_Price(price);
    }
    
    // name - sets fruit name
    void set_Name(const std::string& x) { name = x; }
    auto get_Name() const { return name; }
    // quantity - has a 0-10 limit on quantity amount
    void set_Quantity(int q)
    {    if (q < 0 || q > 10)
            {
               quantity = 0;
            }
            else
            {
                quantity = q;
            }
    }
    auto get_Quantity() const { return quantity; }
    // weight
    void set_Weight(double w) { if (w > 0) weight = w; }
    auto get_Weight() const { return weight; }
    // price
    void set_Price(float p) { if (p >= 0) price = p; }
    auto get_Price() const { return price; }
    // is it purchased?
    void set_Is_purchased(bool i) { is_purchased = i; }
    auto get_Is_purchased() const { return is_purchased; }
    
    void print()
    {
        cout
        << "Name: " << setw(10) << left << name
        << "Quantity: " << setw(3) << left << quantity << ' '
        << "Weight: " << setw(4) << left << weight << ' '
        << "  Price: $" << setw(3) << left << price << ' '
        << "Did you purchase? "  << setw(6) << right << boolalpha
        << is_purchased << '\n';
    }
    
private:
    string name;
    int quantity {};
    double weight {};
    float price {};
    bool is_purchased {};
};

int main()
{
    cout
    << "This program creates at least 5 separate, co-existing instances of a\n"
    << "custom designed object. Information about each object is displayed.\n"
    << "Each constructor, setter and getter is tested at least once.\n\n";
    
    /*
     Modify the program to ask: "How many objects do you want?". Loop as many times specified by the user
     (5 is enough for testing) to create that many objects. You can use 5 individual variables,
     or you can use an array or vector.
     */
    cout << "How many objects do you want?\n"<< endl;
    //
    
    cout<<"You created 5 objects:\n# 1 of 5 objects: "<< endl;
    // fruit 1 - set's method
    Fruit fruit1;
    fruit1.set_Name("Apple ");
    fruit1.set_Quantity(4);
    fruit1.set_Weight(4.56);
    fruit1.set_Price(4);
    fruit1.set_Is_purchased(true);
    fruit1.print(); // <----
    // fruit 2 - construuctor method
    cout<<"\n# 2 of 5 objects: "<< endl;
    Fruit fruit2("Orange", 7, 2.65, 5, true);
    fruit2.print(); // <--
    // fruit 3 - construuctor method
    cout<<"\n# 3 of 5 objects: "<< endl;
    Fruit fruit3("Banana", 4, 2.34, 2, false);
    fruit3.print(); // <--
    // fruit 4 - construuctor method
    cout<<"\n# 4 of 5 objects: "<< endl;
    Fruit fruit4("Avocado", 16, 2.59, -6, false);
    fruit4.print(); // <--
    // fruit 5 - construuctor method
    cout<<"\n# 5 of 5 objects: "<< endl;
    Fruit fruit5("Grapes", -7, -2.65, 5, false);
    fruit5.print(); // <--
    cout
    << "\nThis ends the class design, implementation, test program. Goodbye!"<< endl;

    return EXIT_SUCCESS;
}   // end of main



current output:

This program creates at least 5 separate, co-existing instances of a
custom designed object. Information about each object is displayed.
Each constructor, setter and getter is tested at least once.

How many objects do you want?

You created 5 objects:
# 1 of 5 objects:
Name: Apple Quantity: 4 Weight: 4.56 Price: $4 Did you purchase? true

# 2 of 5 objects:
Name: Orange Quantity: 7 Weight: 2.65 Price: $5 Did you purchase? true

# 3 of 5 objects:
Name: Banana Quantity: 4 Weight: 2.34 Price: $2 Did you purchase? false

# 4 of 5 objects:
Name: Avocado Quantity: 0 Weight: 2.59 Price: $0 Did you purchase? false

# 5 of 5 objects:
Name: Grapes Quantity: 0 Weight: 0 Price: $5 Did you purchase? false

This ends the class design, implementation, test program. Goodbye!
Program ended with exit code: 0
Last edited on
@OP
You don't actually need a counter/class at all if you use a container like a <vector> or a C-style array among others such as a <map> (Oops! Didn't read the comments closely - but great minds think alike)

A couple of points about the error raised for out-of-range values:
1. An error message is a good idea for notifying assertions regarding invalid data
2. Under 'design-by-contract' methodology it is the user's (caller's) responsibility to get their side of the input right. That means an object is not created if the input doesn't meet the requirement of the class. I'll give you a demo of how that can work later - it's not difficult.
3. Without flogging a dead horse the destructor is important and regardless of opinions to the contrary leaving it out is a no-brainer reusability mistake. Your teacher should know better. A simple ~Fruit(){}; is all that's required, ie without the message. It's up to you.

BTW Please fix up my typo in your program its 'constructor', not 'construuctor'
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;
// class
class Fruit
{
    //access specifier
public:
    //default constructor
    Fruit() {}
    // non-default constructor
    Fruit(
          const std::string& fruitName, int quantity, double weight,
          float price, bool ispurchased) :
    name(fruitName), is_purchased(ispurchased)
    {
        set_Quantity(quantity);
        set_Weight(weight);
        set_Price(price);
    }
    
    ~Fruit(){}
    
    // name - sets fruit name
    void set_Name(const std::string& x) { name = x; }
    auto get_Name() const { return name; }
    // quantity - has a 0-10 limit on quantity amount
    void set_Quantity(int q)
    {    if (q < 0 || q > 10)
            {
               quantity = 0;
                cout << "****** ERROR: Invalid quantity - try again\n";
            }
            else
            {
                quantity = q;
            }
    }
    auto get_Quantity() const { return quantity; }
    // weight
    void set_Weight(double w) { if (w > 0) weight = w; }
    auto get_Weight() const { return weight; }
    // price
    void set_Price(float p) { if (p >= 0) price = p; }
    auto get_Price() const { return price; }
    // is it purchased?
    void set_Is_purchased(bool i) { is_purchased = i; }
    auto get_Is_purchased() const { return is_purchased; }
    
    void print()
    {
        cout
        << "Name: " << setw(10) << left << name
        << "Quantity: " << setw(3) << left << quantity << ' '
        << "Weight: " << setw(4) << left << weight << ' '
        << "  Price: $" << setw(3) << left << price << ' '
        << "Did you purchase? "  << setw(6) << right << boolalpha
        << is_purchased << '\n';
    }
    
private:
    string name;
    int quantity {};
    double weight {};
    float price {};
    bool is_purchased {};
};

int main()
{
    cout
    << "This program creates at least 5 separate, co-existing instances of a\n"
    << "custom designed object. Information about each object is displayed.\n"
    << "Each constructor, setter and getter is tested at least once.\n\n";
    
    /*
     Modify the program to ask: "How many objects do you want?". Loop as many times specified by the user
     (5 is enough for testing) to create that many objects. You can use 5 individual variables,
     or you can use an array or vector.
     */
    
    int no_fruits{0};
    
    cout << "How many objects do you want? ";
    cin >> no_fruits;
    
    Fruit* FruitCart = nullptr;
    FruitCart = new Fruit[no_fruits];
    cout << "There is space for " << no_fruits << " fruits in your cart\n";
    
    int COUNTER{0};
    
    // fruit 1 - set's method
    Fruit fruit1;
    fruit1.set_Name("Apple ");
    fruit1.set_Quantity(4);
    fruit1.set_Weight(4.56);
    fruit1.set_Price(4);
    fruit1.set_Is_purchased(true);
    fruit1.print(); // <----
    FruitCart[COUNTER] = fruit1;
    COUNTER++;
    
    // MAKE SURE No. OF ITEMS < SPACE AVAILABLE (cf <vector>)
    
    // fruit 2 - constructor method
    cout<<"\n# 2 of 5 objects: "<< endl;
    Fruit fruit2("Orange", 7, 2.65, 5, true);
    fruit2.print(); // <--
    FruitCart[COUNTER] = fruit2;
    COUNTER++;
    
    // fruit 3 - constructor method
    cout<<"\n# 3 of 5 objects: "<< endl;
    Fruit fruit3("Banana", 4, 2.34, 2, false);
    fruit3.print(); // <--
    FruitCart[COUNTER] = fruit3;
    COUNTER++;
    
    // fruit 4 - constructor method
    cout<<"\n# 4 of 5 objects: "<< endl;
    Fruit fruit4("Avocado", 16, 2.59, -6, false);
    fruit4.print(); // <--
    FruitCart[COUNTER] = fruit4;
    COUNTER++;
    
    // fruit 5 - constructor method
    cout<<"\n# 5 of 5 objects: "<< endl;
    Fruit fruit5("Grapes", -7, -2.65, 5, false);
    fruit5.print(); // <--
    FruitCart[COUNTER] = fruit5;
    COUNTER++;
    
    
    // DISPLAY CART CONTENTS
    for(int i = 0; i < COUNTER; i++)
    {
        cout
        << '\n'
        << "*** ITEM #" << i << " of " << COUNTER << " fruits ...\n";
        FruitCart[i].print();
        
        cout << '\n';
    }
    
    
    // CLEAN UP
    delete[] FruitCart;
    
    cout
    << "\nThis ends the class design, implementation, test program. Goodbye!\n";

    return EXIT_SUCCESS;
}   // end of main 



This program creates at least 5 separate, co-existing instances of a
custom designed object. Information about each object is displayed.
Each constructor, setter and getter is tested at least once.

How many objects do you want? 10
There is space for 10 fruits in your cart
Name: Apple     Quantity: 4   Weight: 4.56   Price: $4   Did you purchase? true     

# 2 of 5 objects: 
Name: Orange    Quantity: 7   Weight: 2.65   Price: $5   Did you purchase? true     

# 3 of 5 objects: 
Name: Banana    Quantity: 4   Weight: 2.34   Price: $2   Did you purchase? false     

# 4 of 5 objects: 
****** ERROR: Invalid quantity - try again
Name: Avocado   Quantity: 0   Weight: 2.59   Price: $0   Did you purchase? false     

# 5 of 5 objects: 
****** ERROR: Invalid quantity - try again
Name: Grapes    Quantity: 0   Weight: 0      Price: $5   Did you purchase? false     

*** ITEM #0 of 5 fruits ...
Name: Apple     Quantity: 4   Weight: 4.56   Price: $4   Did you purchase? true     


*** ITEM #1 of 5 fruits ...
Name: Orange    Quantity: 7   Weight: 2.65   Price: $5   Did you purchase? true     


*** ITEM #2 of 5 fruits ...
Name: Banana    Quantity: 4   Weight: 2.34   Price: $2   Did you purchase? false     


*** ITEM #3 of 5 fruits ...
Name: Avocado   Quantity: 0   Weight: 2.59   Price: $0   Did you purchase? false     


*** ITEM #4 of 5 fruits ...
Name: Grapes    Quantity: 0   Weight: 0      Price: $5   Did you purchase? false     


This ends the class design, implementation, test program. Goodbye!
Program ended with exit code: 0
Without flogging a dead horse the destructor is important and regardless of opinions to the contrary leaving it out is a no-brainer reusability mistake. Your teacher should know better. A simple ~Fruit(){}; is all that's required, ie without the message. It's up to you.

I'll delightfully keep flogging this dead horse. An empty, non-virtual destructor is not recommended, nor necessary. It's a waste of typing.

It can actually be a pessimization on some compilers, at least, on older compilers, and apparently even some modern compilers (for example, implicitly preventing move optimizations).
CppCon video: https://www.youtube.com/watch?v=XvWyLAW_U0Q&t=727s
Another example: https://stackoverflow.com/a/2322314

Hope that clears things up.
Last edited on
@Gonads
Feel free but I don't try and contribute here constructively to argue the point with you, a dope of no consequence who has a demonstrated reputation for piling on with other bullies who come along here with their abuse.

Direct your comments at OP - I have no interest in what you or your pile-on mates have to say about anything.

There are plenty of examples where the counter argument prevails.

By sidetracking with your stupidity you have cleared up nothing.

Have an argument with Microsoft:
https://docs.microsoft.com/en-us/cpp/cpp/destructors-cpp?view=msvc-160
Last edited on
Feel free to actually counter the arguments I stated, at your leisure :)
ocKiNOsIi and KENnIG1591 can see whose arguments sound more reasonable. Have a good one.
Last edited on
Like I indicated Gonads, when it's Microsoft against a passive aggressive dope like you, I know which side is the winning one.

All the other two want is getting their homework done for them.
It wasn't meant to be passive aggressive. It's just that talking with you is like walking on eggshells, so I didn't want you to be upset. Too late for that. The Microsoft article gives reasonable advice that I agree with. Again, an empty, non-virtual destructor is not recommended, and can be a compiler pessimisation.

Edit: I did not report any posts in this thread.
Last edited on
You started the abuse and pile-on with your scabby no-job-prospect mates long ago @Gonads. Like all passive agressive bullies and cowards you don't like a little push back (of the non-STL type). You are a humourless, know nothing coward, a productt of rote learning and no practical judgement.
Like I said I am not interested in anything you have to say or write so address your comments to @OP.
ROFL.
A pink slip is the best shot from the cowards and bullies?
All they're doing is reminding every decent person here yet again of their abusive record. Totally exposed yet again - not one of them has ever strung a line of code together in years of perpetuating their nonsense. All talk and no action.
It wasn't meant to be passive aggressive.
Oh yes it was Gonads. A lot of people here don't but you and I know your have a long history.
@Ganado
You should know it: don't feed the troll...
@Gonads
There is no difference in the number of assembler lines whether my code has a destructor or not. Both produce 927 lines.
@coder777 - so far 8113 posts, not one has been constructive, productive or helpful let alone receiving a green tick - a complete waste of a life. An accumulator of barren posts and negative lame and repetitive comments. Best shot with 777 is to tell someone to put code tags on their posts, as if that matters.
They standby and can't wait for my next home truth.
Just a small point...
someone wrote:
// Setters should NOT change existing valid member values to invalid values.
// If given invalid data in a setter, reject it. Do not change the value.
emphasis added.
So if the value is out of range it should be left alone not set to zero.
^ yes
The default constructor takes care of invalid inputs and its mostly for testing. no need for the error message.

Here's an updated code. How can I get the counter to print out the the items accordingly?

As well as adding an error message if user input is invalid


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
// class
class Fruit
{
    //access specifier
public:
    //default constructor
    Fruit() {}
    // non-default constructor
    Fruit(
          const std::string& fruitName, int quantity, double weight,
          float price, bool ispurchased) :
    name(fruitName), is_purchased(ispurchased)
    {
        set_Quantity(quantity);
        set_Weight(weight);
        set_Price(price);
    }
    
    // name - sets fruit name
    void set_Name(const std::string& x) { name = x; }
    auto get_Name() const { return name; }
    // quantity - has a 0-10 limit on quantity amount
    void set_Quantity(int q)
    {    if (q < 0 || q > 10)
            {
               quantity = 0;
            }
            else
            {
                quantity = q;
            }
    }
    auto get_Quantity() const { return quantity; }
    // weight
    void set_Weight(double w) { if (w > 0) weight = w; }
    auto get_Weight() const { return weight; }
    // price
    void set_Price(float p) { if (p >= 0) price = p; }
    auto get_Price() const { return price; }
    // is it purchased?
    void set_Is_purchased(bool i) { is_purchased = i; }
    auto get_Is_purchased() const { return is_purchased; }
    
    void print()
    {
        cout
        << "Name: " << setw(10) << left << name
        << "Quantity: " << setw(3) << left << quantity << ' '
        << "Weight: " << setw(3) << left << weight << ' '
        << "  Price: $" << setw(3) << left << price << ' '
        << "Did you purchase? "  << setw(6) << right << boolalpha
        << is_purchased << '\n';
    }
private:
    string name;
    int quantity {};
    double weight {};
    float price {};
    bool is_purchased {};
};

int main()
{
    cout
    << "This program creates at least 5 separate, co-existing instances of a\n"
    << "custom designed object. Information about each object is displayed.\n"
    << "Each constructor, setter and getter is tested at least once.\n\n";
    
    /*
     Modify the program to ask: "How many objects do you want?". Loop as many times specified by the user
     (5 is enough for testing) to create that many objects. You can use 5 individual variables,
     or you can use an array or vector.
     */
    
    int no_fruits{0};
        
        cout << "How many objects do you want? (Limit is 5): ";
        cin >> no_fruits;
    
    // add error message if they enter anything about 5
    
        Fruit* FruitCart = nullptr;
        FruitCart = new Fruit[no_fruits];
        int COUNTER{0};
    
    cout<<"You created "<< no_fruits<<" objects:\n# 1 of "
    << no_fruits<< " objects: "<< endl;
    
    
    // fruit 1 - set's method
    Fruit fruit1;
    fruit1.set_Name("Apple ");
    fruit1.set_Quantity(4);
    fruit1.set_Weight(4.56);
    fruit1.set_Price(4);
    fruit1.set_Is_purchased(true);
    fruit1.print(); // <----
    
    FruitCart[COUNTER] = fruit1;
        COUNTER++;
    
    // fruit 2 - constructor method
    cout<<"\n# 2 of " << no_fruits<< " objects: "<< endl;
    Fruit fruit2("Orange", 7, 2.65, 5, true);
    fruit2.print(); // <--
    
    FruitCart[COUNTER] = fruit2;
        COUNTER++;
    
    // fruit 3 - constructor method
    cout<<"\n# 3 of "<< no_fruits<<" objects: "<< endl;
    Fruit fruit3("Banana", 4, 2.34, 2, false);
    fruit3.print(); // <--
    
    FruitCart[COUNTER] = fruit3;
        COUNTER++;
    
    // fruit 4 - constructor method
    cout<<"\n# 4 of "<< no_fruits<<" objects: "<< endl;
    Fruit fruit4("Avocado", 16, 2.59, -6, false);
    fruit4.print(); // <--
    
    FruitCart[COUNTER] = fruit4;
        COUNTER++;
    
    // fruit 5 - constructor method
    cout<<"\n# 5 of "<< no_fruits<<" objects: "<< endl;
    Fruit fruit5("Grapes", -7, -2.65, 5, false);
    fruit5.print(); // <--
    
    FruitCart[COUNTER] = fruit5;
        COUNTER++;
    
    cout
    << "\nThis ends the class design, implementation, test program. Goodbye!"<< endl;

    return EXIT_SUCCESS;
}   // end of main 




This program creates at least 5 separate, co-existing instances of a
custom designed object. Information about each object is displayed.
Each constructor, setter and getter is tested at least once.

How many objects do you want? (Limit is 5): 4
You created 4 objects:
# 1 of 4 objects:
Name: Apple Quantity: 4 Weight: 4.56 Price: $4 Did you purchase? true

# 2 of 4 objects:
Name: Orange Quantity: 7 Weight: 2.65 Price: $5 Did you purchase? true

# 3 of 4 objects:
Name: Banana Quantity: 4 Weight: 2.34 Price: $2 Did you purchase? false

# 4 of 4 objects:
Name: Avocado Quantity: 0 Weight: 2.59 Price: $0 Did you purchase? false

# 5 of 4 objects:
Name: Grapes Quantity: 0 Weight: 0 Price: $5 Did you purchase? false

This ends the class design, implementation, test program. Goodbye!
Program ended with exit code: 0
Last edited on
KENnIG1591, I was talking about the setters. In your code:
28
29
30
31
32
33
34
35
36
37
    void set_Quantity(int q)
    {    if (q < 0 || q > 10)
            {
               quantity = 0;
            }
            else
            {
                quantity = q;
            }
    }

according to the requirements, line 31 should just return and not change the value.
Possibly something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <vector>

using namespace std;
class Fruit {
public:
	Fruit() {}
	Fruit(const std::string& fruitName, int quantity, double weight, float price, bool ispurchased) : name(fruitName), is_purchased(ispurchased) {
		set_Quantity(quantity);
		set_Weight(weight);
		set_Price(price);
	}

	void set_Name(const std::string& x) { name = x; }
	auto get_Name() const { return name; }

	bool set_Quantity(int q) {
		if (q < 1 || q > 10)
			return false;

		quantity = q;
		return true;
	}

	auto get_Quantity() const { return quantity; }

	bool set_Weight(double w) {
		if (w < 1)
			return false;

		weight = w;
		return true;
	}

	auto get_Weight() const { return weight; }

	bool set_Price(float p) {
		if (p < 1)
			return false;

		price = p;
		return true;
	}

	auto get_Price() const { return price; }
	void set_Is_purchased(bool i) { is_purchased = i; }
	auto get_Is_purchased() const { return is_purchased; }

	void print() const {
		cout
			<< "Name: " << setw(10) << left << name
			<< "Quantity: " << setw(3) << left << quantity << ' '
			<< "Weight: " << setw(3) << left << weight << ' '
			<< "  Price: $" << setw(3) << left << price << ' '
			<< "Did you purchase? " << setw(6) << right << boolalpha
			<< is_purchased << '\n';
	}

private:
	string name;
	int quantity {};
	double weight {};
	float price {};
	bool is_purchased {};
};

int main() {
	constexpr size_t maxObj {5};

	cout
		<< "This program creates at least 5 separate, co-existing instances of a\n"
		<< "custom designed object. Information about each object is displayed.\n"
		<< "Each constructor, setter and getter is tested at least once.\n\n";

	size_t no_fruits {};

	do {
		cout << "How many objects do you want? (1 - " << maxObj << "): ";
		cin >> no_fruits;
		cin.clear();
	} while ((!cin || (no_fruits < 1 || no_fruits > maxObj)) && (cout << "Invalid number\n") && cin.ignore(1000, '\n'));

	cout << "You created " << no_fruits << " objects:\n# 1 of "
		<< no_fruits << " objects:\n";

	std::vector<Fruit> fruits(no_fruits);

	for (size_t f = 0; f < no_fruits; ++f) {
		std::string name;
		int qty {};
		double wght {};
		float price {};
		bool pur {};

		std::cout << "Fruit " << f + 1 << '\n';

		std::cout << "Enter name: ";
		std::getline(std::cin >> std::ws, name);
		fruits[f].set_Name(name);

		do {
			std::cout << "Enter quantity: ";
			std::cin >> qty;
			std::cin.clear();
		} while ((!cin || !fruits[f].set_Quantity(qty)) && (cout << "Invalid quantity\n") && cin.ignore(1000, '\n'));

		do {
			std::cout << "Enter weight: ";
			std::cin >> wght;
			std::cin.clear();
		} while ((!cin || !fruits[f].set_Weight(wght)) && (cout << "Invalid weight\n") && cin.ignore(1000, '\n'));

		do {
			std::cout << "Enter price: ";
			std::cin >> price;
			std::cin.clear();
		} while ((!cin || !fruits[f].set_Price(price)) && (cout << "Invalid price\n") && cin.ignore(1000, '\n'));
	}

	for (const auto& f : fruits)
		f.print();
}

@seeplus that counter is working effectively the only thing is that the only cin line should be after user is asked how many objects they want.

This was confusing for me, but I got clarification from the professor and he stated:
There is a need for test data, both valid and invalid; test data is required to test all code: constructors, setters, getters, etc.
To test all the code, including the validation code, you need to provide both valid and invalid data.
Where does this valid and invalid data come from?
As it says, you can ask the user, using an input function, for data, which allows the user / tester to enter data as
desired - valid or invalid - for testing.
And / or, data can be "hard-coded", that is, literal values, which can be valid or invalid.


Pages: 1234