Jul 27, 2013 at 1:01am UTC
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 Jul 27, 2013 at 1:02am UTC
Jul 27, 2013 at 3:15am UTC
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.
Jul 27, 2013 at 3:22am UTC
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!
Jul 27, 2013 at 5:53am UTC
I concur with Thumper. This thread restores my faith in humanity. Bravo, qriz75.
Jul 27, 2013 at 6:29am UTC
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 Jul 27, 2013 at 6:30am UTC