Someone anyone pls help

I am trying to create a function with an array of names inserted and creating a slip function. Lets assume Day1 there are 2 buyers (Zee and Zoe) and Day2(Eddie and Zoe), Day3(Mike and Jo). Meaning there will be 5 slips for Zee, Zoe and Eddie showing the number bought and total charge.

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

class iceCream
{
    private:
        static const size_t DAYS_BUYING = 3;
        string noBuyers[DAYS_BUYING];
        string buyersN[DAYS_BUYING];
        string dOWeek[DAYS_BUYING];
        int noIcecream[DAYS_BUYING] = {0} ;

        

    public:

        void getBuyersDetails()
        {
            for( size_t count = 0; count <DAYS_BUYING ; ++count )
            {
       //Not sure if im correct by creating another array for names
          
	if 
	(noBuyers[count]>1)
	{cout<<"Names of the buyers: ";
	 getline(cin, name[count]);}
	else if
		(noBuyers[count]==1)
	{cout<<"Name of buyer: ";
	getline(cin, name[count]);}
	else	
	{cout<<endl;}
}
            
           cout << "Day of week: " ;
           getline(cin, dOWeek[count]);

            cout << "Number of icecream at R3 each: ";
            cin >> noIcecream[count];
            cin.ignore();
            }
        }

   //Trying to reference the names inputted and getting their details
  void slips()
     {
       std::cout<<"----------------- SLIP --------------\n\n" ;
      int price = 3;
        for( size_t count = 0; count < DAYS_BUYING; ++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] ;
          
       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] ;
                    
      for( size_t i = count+1; i < DAYS_BUYING; ++i ) if( buyersN[i] == name )
                     {
            std::cout << ' ' << dOWeek[i] ;
            buyersN[i] = "" ;          }

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

int main()
{
   iceCream e1;
   e1.getBuyersDetails();
   e1.slips();
}
Last edited on
You really haven't asked a clear question. Not sure what problem you're having.

line 26: What is the purpose of the name array? It goes out of scope when getBuyersDetails() exits. The information in it is not saved.

Lines 11-14: I really think these lines should be a separate struct (or class). You would then have an array of that struct. The object you want to deal with is a slip, not a truck.
Last edited on
I've made changes to the above code.
if more then one name is inserted i want the slip function to pick that up and create individual slips for all that bought the ice cream. Im not good at explaining
You've changed the name of the class from iceCreamTruck, to just iceCream, but you haven't changed the functionality. As I pointed out previously, the object you want to deal with is a Slip, not an iceCream. Frankly, I found your code very hard to follow.

I don't know if you've learned maps yet. Here's a cleaned up version of your code.
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
#include <iostream>
#include <string>
#include <iomanip>
#include <map>
using namespace std;

static const size_t DAYS_BUYING = 3;

class Slip 
{   string name;
    string dOWeek;      
    int noIcecream;

public:    
    Slip ();
    Slip (const string buyername, const string dow, int num);    
    void Print () const;
};
    
class iceCream
{   multimap<string,Slip>     m_slips;
    
    public:
        void getDaysSales();      
        void PrintSlips () const;  
};

//  Get sales for 1 day
void iceCream::getDaysSales()
{   string  dow;
    int     count;
    Slip    slip;
    string  name;
    int     num_sold;
    
    cout << "Day of the Week: ";
    cin >> dow;
	cout << "How many Buyers: ";
	cin >> count;
	for (int i=0; i<count; i++)
	{   cout << "Name of the buyer " << i+1 << "? ";
	    getline(cin, name);
	    cout << "Number of icecream at R3 each sold to " << name << "? ";
	    cin >> num_sold;
	    slip = Slip (name, dow, num_sold);
        pair<string,Slip>  pr (name, slip);
        m_slips.insert (pr);    
    }	        
}

void iceCream::PrintSlips () const
{   multimap<string,Slip>::const_iterator    citer;

    for (citer=m_slips.begin(); citer!=m_slips.end(); citer++)
        citer->second.Print ();
}

Slip::Slip ()
{}
    
Slip::Slip (const string buyername, const string dow, int num_sold)
{   name = buyername;
    dOWeek = dow;
    noIcecream = num_sold;    
}

//  Print one Slip 
void Slip::Print () const
{   cout<<"----------------- SLIP --------------\n\n" ;
    const int price = 3;
  
    cout << "Name: " << name << '\n' ;
    cout << "number of ice_creams: " << noIcecream << endl;
    cout << "total price: " << fixed << setprecision(2) << noIcecream * price << '\n';
    cout << "day of week: " << dOWeek << endl;
}

int main()
{   iceCream e1;

    for (int i=0; i<DAYS_BUYING; i++)
        e1.getDaysSales();      //  Get details for one day
   e1.PrintSlips();
}

Last edited on
Thanks for the code.
I added a line between line 29 and 30 for the 2 days of buying
 
for (size_t number = 0; number<DAYS_BUYING; ++number)


Output is
Day of the week: wednesday
How many buyers: 2
Name of Buyer 1: Eddie
Number of icecrem sold to Eddie: 1
Name of Buyer 2: Sam
Number of icecrem sold to Eddie: 1
*************************
Day of the week: Thursday
How many buyers: 1
Name of Buyer 1: Eddie
Number of icecrem sold to Eddie: 1
**************************
-----------SLIP-------------------------
Instead of printing only 2 slips for Sam and Eddie(adding wednesday and Thurs). It prints 3 slips
Last edited on
Topic archived. No new replies allowed.