help with Implementing class Item

Pages: 1234
Oct 22, 2021 at 4:33pm
the only thing is that the only cin line should be after user is asked how many objects they want.


???
Oct 22, 2021 at 5:00pm
sorry. the only user input in the program should come after the user is asked "How many objects do you want?" the user is not setting the fruit, quantity, weight, etc.
Last edited on Oct 22, 2021 at 6:30pm
Oct 23, 2021 at 1:26pm
if anyone is available for help....

I am still looking for help on this program, it is about 95% done. I just need help with the counter. the code is posted above, and It shows the error my code has.

When the user is asked how many items they want even if they input a number less than 5, all 5 items are printed. How do I fix this? and how do I print and error message if they go outside the limit of 1-5?
Oct 23, 2021 at 1:33pm
do
{
cin >> counter;
if(counter > limit || counter < 1) complain
}while(counter > limit || counter < 1)


for(int i = 0; i < counter; i++)
{
print something[i];
}
Oct 24, 2021 at 1:49pm
trying to understand vectors here but I'm not sure what I'm doing wrong. its still just printing all 5 objects even when prompted to print less

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
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.*/
     
    constexpr size_t maxObj {5};
    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 input! Please enter a number (1-5)..\n") && cin.ignore(1000, '\n'));
    
    
   // cout<<"You created "<< counter<<" objects:\n# 1 of "
  //  << limit << " objects: "<< endl;
    
    cout << "\nYou created " << no_fruits << " objects:" << endl<<endl;
    cout <<"# 1 of "
    << no_fruits << " objects:\n";

//vector <type> name (size)
    vector<Fruit> fruits(no_fruits);
    
for (size_t f = 0; f < no_fruits; ++f) {    // will only loop once
    // 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 - constructor method
    cout<<"\n# 2 of " << no_fruits<< " objects: "<< endl;
    Fruit fruit2("Orange", 7, 2.65, 5, true);
    fruit2.print(); // <--
    
    
    // fruit 3 - constructor method
    cout<<"\n# 3 of "<< no_fruits<<" objects: "<< endl;
    Fruit fruit3("Banana", 4, 2.34, 2, false);
    fruit3.print(); // <--
    
    
    // fruit 4 - constructor method
   cout<<"\n# 4 of "<< no_fruits<<" objects: "<< endl;
    Fruit fruit4("Avocado", 16, 2.59, -6, false);
    fruit4.print(); // <--
    
    
    // fruit 5 - constructor method
    cout<<"\n# 5 of "<< no_fruits<<" 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 
Last edited on Oct 24, 2021 at 5:29pm
Oct 25, 2021 at 5:57am
This requirements doesn't make sense. When you have 5 individual variables they are created. A vector or array doesn't make sense because the data is completely different. What you can actually do would be 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
for (size_t f = 0; f < no_fruits; ++f) {    // will only loop once
if(f == 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(); // <----
}
else if(f == 1)
{
    // fruit 2 - constructor method
    cout<<"\n# 2 of " << no_fruits<< " objects: "<< endl;
    Fruit fruit2("Orange", 7, 2.65, 5, true);
    fruit2.print(); // <--
}    
else if(f == 2)
{
    // fruit 3 - constructor method
    cout<<"\n# 3 of "<< no_fruits<<" objects: "<< endl;
    Fruit fruit3("Banana", 4, 2.34, 2, false);
    fruit3.print(); // <--

...

}
Not that it make too much sense...
Oct 25, 2021 at 11:12am
ty! that finally worked.
Last edited on Oct 25, 2021 at 6:56pm
Topic archived. No new replies allowed.
Pages: 1234