Structures Confusion
May 13, 2016 at 10:53pm UTC
Hey guys, I am new to the forum. I'm taking classes for C++ at school.
I am having trouble getting the total and the average for my program. I don't know exactly where I should be putting it, but I have my full code below
(averageQuarterlySales and totalAnnualSales are the variables that need to work)
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
#include <iostream>
using namespace std;
struct CorpData
{
double firstQuarterSales;
double secondQuarterSales;
double thirdQuarterSales;
double fourthQuarterSales;
double totalAnnualSales = firstQuarterSales + secondQuarterSales + thirdQuarterSales + fourthQuarterSales;
double averageQuarterlySales = totalAnnualSales / 4;
};
void PrintInformation(CorpData xCorpData)
{
cout << "First Quarterly Sales: " << xCorpData.firstQuarterSales << endl;
cout << "Second Quarterly Sales: " << xCorpData.secondQuarterSales << endl;
cout << "Third Quarterly Sales: " << xCorpData.thirdQuarterSales << endl;
cout << "Fourth Quarterly Sales: " << xCorpData.fourthQuarterSales << endl;
cout << "Total Annual Sales: " << xCorpData.totalAnnualSales << endl;
cout << "Average Quarterly Sales: " << xCorpData.averageQuarterlySales << endl;
cout << "\n\n\n" << endl;
}
int main()
{
CorpData North;
North.firstQuarterSales = 5;
North.secondQuarterSales = 8;
North.thirdQuarterSales = 2;
North.fourthQuarterSales = 7;
North.totalAnnualSales;
North.averageQuarterlySales;
CorpData South;
South.firstQuarterSales = 1;
South.secondQuarterSales = 3;
South.thirdQuarterSales = 2;
South.fourthQuarterSales = 5;
South.totalAnnualSales;
South.averageQuarterlySales;
CorpData East;
East.firstQuarterSales = 9;
East.secondQuarterSales = 7;
East.thirdQuarterSales = 2;
East.fourthQuarterSales = 1;
East.totalAnnualSales;
East.averageQuarterlySales;
CorpData West;
West.firstQuarterSales = 5;
West.secondQuarterSales = 6;
West.thirdQuarterSales = 5;
West.fourthQuarterSales = 3;
West.totalAnnualSales;
West.averageQuarterlySales;
cout << "North\n" << endl;
PrintInformation(North);
cout << "South\n" << endl;
PrintInformation(South);
cout << "East\n" << endl;
PrintInformation(East);
cout << "West\n" << endl;
PrintInformation(West);
}
Last edited on May 13, 2016 at 10:55pm UTC
May 13, 2016 at 11:07pm UTC
You can find your totals and averages easily enough, for example:
1 2 3 4
North.totalAnnualSales = North.firstQuarterSales +
North.secondQuarterSales +
North.thirdQuarterSales +
North.fourthQuarterSales;
and the average is:
North.averageQuarterlySales = North.totalAnnualSales / 4;
May 13, 2016 at 11:24pm UTC
Thanks! Worked perfectly.
May 13, 2016 at 11:32pm UTC
Line 29 is wrong! You have to specify North.firstQuarterSales and so on!
1 2 3 4
North.totalAnnualSales = North.firstQuarterSales +
North.secondQuarterSales +
North.thirdQuarterSales +
North.fourthQuarterSales;
I pared down your code to show one region:
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
#include <iostream>
struct CorpData
{
double firstQuarterSales;
double secondQuarterSales;
double thirdQuarterSales;
double fourthQuarterSales;
double totalAnnualSales;
double averageQuarterlySales;
};
void PrintInformation(CorpData xCorpData)
{
std::cout << "First Quarterly Sales: " << xCorpData.firstQuarterSales << "\n" ;
std::cout << "Second Quarterly Sales: " << xCorpData.secondQuarterSales << "\n" ;
std::cout << "Third Quarterly Sales: " << xCorpData.thirdQuarterSales << "\n" ;
std::cout << "Fourth Quarterly Sales: " << xCorpData.fourthQuarterSales << "\n" ;
std::cout << "Total Annual Sales: " << xCorpData.totalAnnualSales << "\n" ;
std::cout << "Average Quarterly Sales: " << xCorpData.averageQuarterlySales << "\n" ;
std::cout << "\n\n\n" ;
}
int main()
{
CorpData North;
North.firstQuarterSales = 5;
North.secondQuarterSales = 8;
North.thirdQuarterSales = 2;
North.fourthQuarterSales = 7;
North.totalAnnualSales = North.firstQuarterSales +
North.secondQuarterSales +
North.thirdQuarterSales +
North.fourthQuarterSales;
North.averageQuarterlySales = North.totalAnnualSales / 4;
std::cout << "North\n" ;
PrintInformation(North);
}
North
First Quarterly Sales: 5
Second Quarterly Sales: 8
Third Quarterly Sales: 2
Fourth Quarterly Sales: 7
Total Annual Sales: 22
Average Quarterly Sales: 5.5
May 13, 2016 at 11:44pm UTC
Yeah, I caught onto that after a minute or two. Thanks for your help though!
Topic archived. No new replies allowed.