function to get number of times string is inputed in lop

Trying to create a function that prints slips for buyers for the amount of icecream bought during the week

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
#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
using namespace std;

class iceCreamTruck
{
private:
	static const size_t NR_OF_BUYERS=3;
	string buyersN[NR_OF_BUYERS];
	string dOWeek[NR_OF_BUYERS];
        int noIcecream[NR_OF_BUYERS];
public:

	void getBuyersDetails()
	{
	for (size_t count = 0; count<NR_OF_BUYERS; ++count)
{
cout<<"Names of the buyer: ";
cin>>buyersN[count];
cout<<"Day of week buying:"
cin>>dOWeek[count];
cout<<"Number of icecream at R3 each";
cin>>noIcecream[count];
}
}
//I get lost here
void slips()
 {
   cout<<"-----------------"<<<<"SLIP"<<"--------------"<<endl;
 }
 for (size_t count=0; count<NR_OF_BUYERS; ++count)
 }
};

int main()
 {
   iceCreamTruck e1;
   e1.getBuyersDetails();
   e1.slips();
  }
   


OUTPUT:
Lets assume Thomas bouht 1 icecream Monday and Wednesday aT R3 each. And Luke bought 1 n Tuesday. Slip shoud look like below

--------------Slip -------------
Name: Thomas
Amount of ice cream bought: 2 @ R3 each Total: R 6
Day of Week: Monday , Wednesday
****************************************
Name: Luke
Amount of ice cream bought: 1 @ R3 each Total: R 3
Day of Week: Tuesday

Last edited on
Your input is wrong:
1
2
3
4
5
6
7
8
9
10
11
12
void getBuyersDetails()
{
   for (size_t count = 0; count<NR_OF_BUYERS; ++count)
  {
    cout<<"Names of the buyer: ";
    cin>>buyersN[NR_OF_BUYERS];
    cout<<"Day of week buying:"
    cin>>dOWeek[NR_OF_BUYERS];
    cout<<"Number of icecream at R3 each";
    cin>>noIcecream[NR_OF_BUYERS];
  }
}

Since NR_OF_BUYERS is a constant it will always be 3 so you write all the data at array index[3] which does not exist.
I need to get the data that i wrote in array for buyersN, check hw many times the buyer appears in the array of 3. How many iceCream they bought and on which day. Maybe i did the getBuyersDetails() function wrong. How would you do it
Instead of NR_OF_BUYERS use count as the array index.
After you got the input print out the arrays to see if the input is right.
Thanks Thomas, I've edited my code on top. Can you now assist me with the function for the slip
Crude fix:

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
#include <iostream>
#include <string>
#include <iomanip>

class iceCreamTruck
{
    private:
        static const size_t NR_OF_BUYERS = 3;

        std::string buyersN[NR_OF_BUYERS];
        std::string dOWeek[NR_OF_BUYERS];
        int noIcecream[NR_OF_BUYERS] = {0} ;

        static constexpr double price = 3.00 ;

    public:

        void getBuyersDetails()
        {
            for( size_t count = 0; count < NR_OF_BUYERS; ++count )
            {
                std::cout << "names of the buyer: ";
                std::cin >> buyersN[count] ;

                std::cout << "Day of week: " ;
                std::cin >> dOWeek[count];

                std::cout << "Number of icecream at R3 each: ";
                std::cin >> noIcecream[count];
            }
        }

        //I get lost here
        void slips()
         {
             std::cout<<"----------------- SLIP --------------\n\n" ;

             for( size_t count = 0; count < NR_OF_BUYERS; ++count )
             {
                 const std::string& name = buyersN[count] ;

                 if( !name.empty() ) // if this is a name that was not already processed
                 {
                     std::cout << "Name: " << name << '\n' ;

                     int num_ice_creams = noIcecream[count] ;
                     // iterate through the array to see if this name appears again
                     for( size_t i = count+1; i < NR_OF_BUYERS; ++i ) if( buyersN[i] == name )
                        num_ice_creams += noIcecream[i] ;

                     std::cout << "number of ice_creams: " << num_ice_creams << " total price: "
                               << std::fixed << std::setprecision(2) << num_ice_creams * price << '\n' ;

                     std::cout << "day of week: " << dOWeek[count] ;
                     // iterate through the array to see if this name appears again
                     for( size_t i = count+1; i < NR_OF_BUYERS; ++i ) if( buyersN[i] == name )
                     {
                          std::cout << ' ' << dOWeek[i] ;
                          buyersN[i] = "" ; // we are done with this name, we don't want to process it again
                     }

                     std::cout << "\n\n" ;
                 }
             }
         }
};

int main()
{
   iceCreamTruck e1;
   e1.getBuyersDetails();
   e1.slips();
}
Thanks it worked
Topic archived. No new replies allowed.