Please open and help me!! Struggling with inputs.

I almost have this complete but I am not sure how to change my input collection. Directions are to: You have to change your input collection to get 4 values (quarterly sales) before calling that one function that stores them in the array.

Any help is appreciated, Thanks!!

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

 
#include <iomanip>
#include <cmath>
#include <iostream>
using namespace std;

class DivSales      // Class name
{
private:            // Private members
	static float corpSales;       
	float divSales[4]; //stores quarterly sales per division

public:
 // Constructor initialization
	DivSales() 
	{
		divSales[4] = {0.0,0.0,0.0,0.0};
	}

	// Member function passing parameter                            
	void addDivSales (float s1, float s2, float s3, float s4)          
	{	// Equation operations
		divSales[0] = s1;
		divSales[1] = s2;
		divSales[2] = s3;
		divSales[3] = s4;
		corpSales += s1 + s2 + s3 + s4;
	}       
	// Return quarterly values to display
	float getDivisionSales (int qtr) 
	{
		return divSales[qtr];
	}  
	// Return corporate values to display
	static float getCorpSales () 
	{
		return corpSales;
	}     
};

//initializing static data member
float DivSales::corpSales = 0.0;


// Main

int main ()
{
	DivSales divisions [6];     // Object array class

	int count;

	for (count = 0; count < 6; count ++)     //loop for prompting user for values

	{

		float sales = 0;  // Initialize variables
		cout << "Enter the sales for each division ";     // Get user inputs 
		cout << (count + 1) << " : ";       // Display loop divisions

		while (sales < 1)                      // Validates that inputs are greater than 1
		{
			cin >> sales;
			divisions[count].addDivSales(sales);      // Passes sale to addDivsales to complete the operation

			if (sales < 1)
				cout << "Enter a positive number: \n";    // Validates that inputs are greater than 1
		}
	}
	cout << setprecision(2);
	cout << showpoint << fixed;
	cout << "\nHere are the division sales: \n";

	for (count = 0; count < 6; count++)      // Loops to display user inputs on sales
	{
		cout << "Division " << (count + 1) << " $ ";
		cout << divisions[count].getDivisionSales() << endl;   
                                                      
	}
	cout << "Total corporate sales:\t$ ";             
	cout << divisions[0].getCorpSales() << endl;        // Displays total corporate sales 

	return 0;
} 
Last edited on
closed account (48T7M4Gy)
a) line 12: float divSales[4] = { 0 }; //stores quarterly sales per division
b) delete line 18
c) line 65: the logic of this is wrong. You need to collect the four sales values and send them all at once via the function addDivSales i.e. s1, s2, s3 and s4

void addDivSales (float s1, float s2, float s3, float s4)

Last edited on
Thank you..
In line 12 I get the error "array bound cannot be deduced from an in class initializer"
I am also still getting an error in line 65 saying that "qtr" is an undeclared identifier.


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

class DivSales      // Class name
{
private:            // Private members
    static float corpSales;
    float divSales[4]; //stores quarterly sales per division
    
public:
    // Constructor initialization
    DivSales()
    {
        divSales[4] = {0};
    }
    
    // Member function passing parameter
    void addDivSales (float s1, float s2, float s3, float s4)
    {	// Equation operations
        divSales[0] = s1;
        divSales[1] = s2;
        divSales[2] = s3;
        divSales[3] = s4;
        corpSales += s1 + s2 + s3 + s4;
    }
    // Return quarterly values to display
    float getDivisionSales (int qtr)
    {
        return divSales[qtr];
    }
    // Return corporate values to display
    static float getCorpSales ()
    {
        return corpSales;
    }
};

//initializing static data member
float DivSales::corpSales = 0.0;


// Main

int main ()
{
    DivSales divisions [6];     // Object array class
    
    int count;
    
    for (count = 0; count < 6; count ++)     //loop for prompting user for values
        
    {
        
        float sales = 0;  // Initialize variables
        cout << "Enter the sales for each division ";     // Get user inputs
        cout << (count + 1) << " : ";       // Display loop divisions
        
        while (sales < 1)                      // Validates that inputs are greater than 1
        {
            cin >> sales;
            void addDivSales (float s1, float s2, float s3, float s4); // Passes sale to addDivsales to complete the operation
            
            if (sales < 1)
                cout << "Enter a positive number: \n";    // Validates that inputs are greater than 1
        }
    }
    cout << setprecision(2);
    cout << showpoint << fixed;
    cout << "\nHere are the division sales: \n";
    
    for (count = 0; count < 6; count++)      // Loops to display user inputs on sales
    {
        cout << "Division " << (count + 1) << " $ ";
        cout << divisions[count].getDivisionSales(qtr) << endl;
        
    }
    cout << "Total corporate sales:\t$ ";             
    cout << divisions[0].getCorpSales() << endl;        // Displays total corporate sales 
    
    return 0;
}
closed account (48T7M4Gy)
you need to replace line 12 with what I wrote :)
closed account (48T7M4Gy)
Sorry replace with this my bad : float divSales[4] = { 0 };
Ahh that worked! I am still confused on line 65 and how to change the collection to 4 inputs. Sorry I am really slow at learning this.
Wait, my error is not line 65, it is line 78 of the first code and line 76 on the second time I sent it.
closed account (48T7M4Gy)
Have a think about it but I reckon you need an array of sales in main, it would have 4 elements that you collect the data on in a loop and then send to addDivSales( float[] sales) instead of 4 separate values. See if it makes sense.
closed account (48T7M4Gy)
If you do that you will need to modify addDivSales to something like:
1
2
3
4
5
6
7
8
void addDivSales(float sales[])
	{
		for (int i = 0; i < 4; i++)
		{
			divSales[i] = sales[i];
			corpSales += divSales[i];
		}
	}
closed account (48T7M4Gy)
And main would be in part:
1
2
3
4
5
6
7
8
9
int index = 0;
		while (index < 4)
		{
			while (cin >> tempSales[index] && tempSales[index] < 0)
				cout << "Enter a positive number: \n";
			index++;
		}

		divisions[divNo].addDivSales(tempSales);
Okay, I will do that. thank you very much!
closed account (48T7M4Gy)
Cheers, you still have to make a few changes elsewhere to make it compatible with this change. Yell out if you have any queries. Cheers :)
Topic archived. No new replies allowed.