Structures, loops and how not to repeat typing the same code

First of all thanks for taking the time and trying to help.

I have a structure for 4 divisions, and can't figure out how to loop through it.
So I ended up writing everything for times.
If I had 500 divisions I wouldn't be able to get away with it!
How can I have the structure in array form so I can fill it via looping?

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
/*Headers*/
#include <iostream>//needed 4 input/output
#include <iomanip>//needed to round
#include <string>//needed 4 strings
using namespace std;//global namespace to avoid name clash

struct division
{
	string divisionName;
	double Q1Sales;
	double Q2Sales;
	double Q3Sales;
	double Q4Sales;
};
//***Function Prototype
void displayValues(division);
//***C++ starting point
int main()
{	
	division d1,d2,d3,d4;

	d1.divisionName = "North";
	cout << "Enter sales for quarter 1: "<<endl;
	cin >> d1.Q1Sales;
	cout << "Enter sales for quarter 2: "<<endl;
	cin >> d1.Q2Sales;
	cout << "Enter sales for quarter 3: "<<endl;
	cin >> d1.Q3Sales;
	cout << "Enter sales for quarter 4: "<<endl;
	cin >> d1.Q4Sales;
	cout << endl;
	
	d2.divisionName = "East";
	cout << "Enter sales for quarter 1: "<<endl;
	cin >> d2.Q1Sales;
	cout << "Enter sales for quarter 2: "<<endl;
	cin >> d2.Q2Sales;
	cout << "Enter sales for quarter 3: "<<endl;
	cin >> d2.Q3Sales;
	cout << "Enter sales for quarter 4: "<<endl;
	cin >> d2.Q4Sales;
	cout << endl;

	d3.divisionName = "South";
	cout << "Enter sales for quarter 1: "<<endl;
	cin >> d3.Q1Sales;
	cout << "Enter sales for quarter 2: "<<endl;
	cin >> d3.Q2Sales;
	cout << "Enter sales for quarter 3: "<<endl;
	cin >> d3.Q3Sales;
	cout << "Enter sales for quarter 4: "<<endl;
	cin >> d3.Q4Sales;
	cout << endl;
	
	d4.divisionName = "West";
	cout << "Enter sales for quarter 1: "<<endl;
	cin >> d4.Q1Sales;
	cout << "Enter sales for quarter 2: "<<endl;
	cin >> d4.Q2Sales;
	cout << "Enter sales for quarter 3: "<<endl;
	cin >> d4.Q3Sales;
	cout << "Enter sales for quarter 4: "<<endl;
	cin >> d4.Q4Sales;
	cout << endl;
	
	displayValues(d1);
	displayValues(d2);
	displayValues(d3);
	displayValues(d4);

	system("PAUSE");//press any key
	return 0;//exit
}
void displayValues(division d)
{
	cout << "Division Name: " << d.divisionName<<endl;
	cout << "First quarter sales: " << d.Q1Sales<<endl;
	cout << "Second quarter sales: " << d.Q2Sales<<endl;
	cout << "Third quarter sales: " << d.Q3Sales<<endl;
	cout << "Fourth quarter sales: " << d.Q4Sales<<endl;
	cout << endl;
}


Thanks in advance
Qriz
You could create a function to populate a division structure.
For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
division constructDivision(const std::string &name)
{
    division ret;
    ret.divisionName = name;
    cout << "Enter sales for quarter 1: " << endl;
    cin >> ret.Q1Sales;
    cout << "Enter sales for quarter 2: " << endl;
    cin >> ret.Q2Sales;
    cout << "Enter sales for quarter 3: " << endl;
    cin >> ret.Q3Sales;
    cout << "Enter sales for quarter 4: " << endl; 
    cin >> ret.Q4Sales;
    cout << endl;
    
    return ret; 
    
}


which would reduce your main function to
1
2
3
4
5
6
division d1, d2, d3, d4;

d1 = constructDivision("North");
d2 = constructDivision("East");
d3 = constructDivision("South");
d4 = constructDivision("West");


EDIT: Spelling
Last edited on
Thanks a lot Thumper, that worked perfect!

Here is how I used your suggestion:
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
/*Headers*/
#include <iostream>//needed 4 input/output
#include <iomanip>//needed to round
#include <string>//needed 4 strings
using namespace std;//global namespace to avoid name clash

/*Structures*/
struct division
{
	string divisionName;
	double Q1Sales;
	double Q2Sales;
	double Q3Sales;
	double Q4Sales;
};
//***Function Prototypes
void displayValues(division);
division getValues(const string &name);

//***C++ starting point
int main()
{	
	division d1,d2,d3,d4;
	
	d1 = getValues("North");
	d2 = getValues("East");
	d3 = getValues("South");
	d4 = getValues("West");


	displayValues(d1);
	displayValues(d2);
	displayValues(d3);
	displayValues(d4);

	system("PAUSE");//press any key
	return 0;//exit
}
division getValues(const string &name)
{
	division div;
	div.divisionName = name;
	cout << name << endl;
	cout << "Enter sales for quarter 1: "<<endl;
	cin >> div.Q1Sales;
	cout << "Enter sales for quarter 2: "<<endl;
	cin >> div.Q2Sales;
	cout << "Enter sales for quarter 3: "<<endl;
	cin >> div.Q3Sales;
	cout << "Enter sales for quarter 4: "<<endl;
	cin >> div.Q4Sales;
	cout << endl;
	return div;
}
void displayValues(division d)
{
	cout << "Division Name: " << d.divisionName<<endl;
	cout << "First quarter sales: " << d.Q1Sales<<endl;
	cout << "Second quarter sales: " << d.Q2Sales<<endl;
	cout << "Third quarter sales: " << d.Q3Sales<<endl;
	cout << "Fourth quarter sales: " << d.Q4Sales<<endl;
	cout << endl;
}
I'm just going to take a moment to thank you for being so great at asking questions.

You were super polite
You used code blocks
and you provided me with feedback about how my suggestion worked for you, as well as how you used it.

All in your first two posts. *tears of joy*
I don't see that enough around here.

All the best, man.
I read the pretty awesome "Welcome -- read before posting!" article, before I posted.
:-)

Thanks again I hope I can catch your interest in future posts!
I concur with Thumper. This thread restores my faith in humanity. Bravo, qriz75.
How can I have the structure in array form so I can fill it via looping?


A struct may have a member that is an array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct division
{
    string divisionName;
    double QSales[4] ;
};

division constructDivision(const std::string &name)
{
    division ret;
    ret.divisionName = name;

    for ( unsigned i=0; i<4; ++i )
    {
        cout << "Enter sales for quarter " << i+1 << ":\n" ;
        cin >> ret.QSales[i] ;
    }

    return ret ;    
}
Last edited on
Thanks cire,

that reduces the amount of code even more.
We're down to 55 Lines ;-)

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
/*Headers*/
#include <iostream>//needed 4 input/output
#include <iomanip>//needed to round
#include <string>//needed 4 strings
using namespace std;//global namespace to avoid name clash

/*Structures*/
struct division
{
	string divisionName;
	double QSales[4];	
};
//***Function Prototypes
division getValues(const string &name);
void displayValues(division);
//***C++ starting point
int main()
{	
	division d1,d2,d3,d4;
	
	d1 = getValues("North");
	d2 = getValues("East");
	d3 = getValues("South");
	d4 = getValues("West");

	displayValues(d1);
	displayValues(d2);
	displayValues(d3);
	displayValues(d4);

	system("PAUSE");//press any key
	return 0;//exit
}
division getValues(const string &name)
{
	division div;
	div.divisionName = name;

	for (unsigned i=0; i < 4; ++i)
	{
		cout << name << endl;
		cout << "Enter sales for quarter " << i+1 << ":\n "<<endl;
		cin >> div.QSales[i];
	}
	return div;
}
void displayValues(division d)
{
	cout << "Division Name: " << d.divisionName<<endl;
	cout << "First quarter sales: " << d.QSales[0]<<endl;
	cout << "Second quarter sales: " << d.QSales[1]<<endl;
	cout << "Third quarter sales: " << d.QSales[2]<<endl;
	cout << "Fourth quarter sales: " << d.QSales[3]<<endl;
	cout << endl;
}
Topic archived. No new replies allowed.