By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,006 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

Logical Error: Help Pls.

P: 9
can someone pls tell me why this thing would not calculate my division intergers and show it within my struct. PLS HELP I'M NEW TO C++, AND HAVE SPENT 4 DAYS TRYING TO FIND OUT WHY I CANT DO THIS THING RIGHT. I'M ACCTUALLY TRYING TO FIGURE OUT HOW STRUCTS WORK BUT IT SEEMS LIKE I CANT GET IT TO TOTAL MY INTERGER AND DISPLAY IT WHEN I CALL THEM IN MAIN.

THANKS


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7.  
  8. struct CorpData
  9. {
  10.     string DivisionName;
  11.     int FirstQtr;
  12.     int SecondQtr;
  13.     int ThirdQtr;
  14.     int FourthQtr;
  15.     int Total_An_Sales;
  16.     int Average_Qtr_Sales;
  17.  
  18.     CorpData(string n = "", int F1Qtr = 0, int S2Qtr = 0, int T3Qtr = 0, int F4Qtr = 0)
  19.     {
  20.         DivisionName = n;
  21.         FirstQtr = F1Qtr;
  22.         SecondQtr = S2Qtr;
  23.         ThirdQtr = T3Qtr;
  24.         FourthQtr = F4Qtr;
  25.     }
  26.  
  27. };
  28.  
  29.  
  30. CorpData DPY();
  31. CorpData Calc();
  32.  
  33. CorpData Calc()
  34. {
  35.     CorpData Math;
  36.     Math.Total_An_Sales = Math.FirstQtr + Math.SecondQtr + Math.ThirdQtr + Math.FourthQtr;
  37.     Math.Average_Qtr_Sales = Math.Total_An_Sales/4;
  38.     return Math;
  39. }
  40.  
  41.  
  42.  
  43.  
  44. CorpData DPY()
  45. {
  46.     CorpData Display;
  47.     cout <<"\n\nHere are the Quartely proceeds from Mike & Ike, by Divisions.\n\n" << endl;
  48.  
  49.     cout <<" DIVISION NAME : "<< Display.DivisionName << endl;
  50.     cout <<" FIRST QTR     : "<< Display.FirstQtr << endl;
  51.     cout <<" SECOND QTR    : "<< Display.SecondQtr << endl;
  52.     cout <<" THIRD QTR    : "<< Display.ThirdQtr << endl;
  53.     cout <<" FOURTH QTR  : "<< Display.FourthQtr << endl;
  54.     cout <<" TOTAL SALES : "<< Display.Total_An_Sales << endl;
  55.     cout <<" AVERAGE QTR SALES: "<< Display.Average_Qtr_Sales << endl;
  56.  
  57. return Display;
  58. }
  59.  
  60.  
  61. int main()
  62. {
  63. CorpData east("East", 16500, 25000, 10000, 18000);
  64. CorpData west("West", 25654, 15321, 13654, 25316);
  65. CorpData north("North", 32123, 41583, 10003, 21005);
  66. CorpData south("South", 20123, 30125, 10002, 14159);
  67.  
  68.     east = Calc();
  69.     west = Calc();
  70.     north = Calc();
  71.     south = Calc();
  72.  
  73.     east = DPY();
  74.     west = DPY();
  75.     north = DPY();
  76.     south = DPY();
  77.     return 0;
  78. }
Mar 11 '07 #1
Share this Question
Share on Google+
1 Reply


Banfa
Expert Mod 5K+
P: 8,916
You do not pass any data to the functions for them to work on. For example in Calc no data to is passed to the function, the Calc function instantiates a CorpData structure internally, this calls the constructor and sets all the values to 0, it does the calculation on those 0 values and then passes the structure back as the return value where it over-writes the actual data in the top level structures.

I similar thing happens in DPY.

You need to pass the data into the function and work on the actually data not instantiate a new object internally to the function. You can either

pass the top level structures to the function using a pointer or reference.

make the functions members of the structure so that they can act directly on the structures data.

Which every you do there will be no need to pass the structure back to main as a return value.
Mar 12 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.