storing data on a .dat file

solved it using below the code if someone needs help
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
Corporate Sales Data

Write a program that uses a structure named CorpData to store the following
information on a company division:
	Division name (such as East, West, North, or South)
	First quarter sales
	Second quarter sales
	Third quarter sales
	Fourth quarter sales
	Total annual sales
	Average quarterly sales.
Include a constructor that allows the division name and four quarterly sales
amounts to be specified at the time a CorpData variable is created.
The program should create four variables of this structure, each representing one
of the following corporate divisions: East, West, North, and South. Each variable
should be passed in turn to a function that calculates and stores the total sales
and average quarterly sales for that division. Once this has been done for each
division, each variable should be passed in turn to a function that displays the
division name, total sales, and quarterly average.
*/
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

struct CorpData
{
	string DivName;     // Division name
	double FstQtr,		// First quarter sales
	       SecQtr,		// second quarter sales
	       TrdQtr,		// Third quarter sales
	       FthQtr,		// Fourth quarter sales
	       TotAnn,		// Total annual sales
	       AvgQtrly;	// Average quarterly sales

	// constructor that allows the division name and four quarterly
	// sales amounts to be specified at the time a CorpData variable
	// is created.
	CorpData(string D, double f, double s, double t, double fr)
	{
		DivName = D;
		FstQtr = f;
		SecQtr = s;
		TrdQtr = t;
		FthQtr = fr;
	}
};

// Function Prototypes
void calcSales(CorpData &);
void displayDivInfo(CorpData);

int main()
{   // Creates four variables of the CorpData struture
	// each representing a corporate division.
	CorpData East("East", 1000, 1500, 1250, 1750);
	CorpData West("West", 2500, 2000, 2750 , 2250);
	CorpData North("North", 3750, 3500, 3250, 3000);
	CorpData South("South", 4250, 4750, 4000, 4500);

	calcSales(East);
	calcSales(West);
	calcSales(North);
	calcSales(South);

	displayDivInfo(East);
	displayDivInfo(West);
	displayDivInfo(North);
	displayDivInfo(South); 
	return 0;
}

/*************************************************************
 *                       calcSales                           *
 * This function accepts a CorpData variable as an argument. *
 * calculates and stores total sales and average quarterly   *
 * sales for that division.                                  *
 *************************************************************/
void calcSales(CorpData &D)
{
	D.TotAnn = D.FstQtr + D.SecQtr + D.TrdQtr + D.FthQtr;
	D.AvgQtrly = D.TotAnn/4;
}

/*************************************************************
 *                     displayDivInfo                        *
 * This function accepts a CorpData variable as an argument. *
 * Displays the division name, totals sales and quarterly    *
 * average.                                                  *
 *************************************************************/
void displayDivInfo(CorpData D)
{
	cout << "\n           Sales Data\n"
		 << "---------------------------------------\n";
    cout << fixed << showpoint << setprecision(2);
	cout << "Division :          " << D.DivName << endl;
	cout << "Total Sales :       $" << D.TotAnn << endl;
	cout << "Quarterly average : $" << D.AvgQtrly << endl;
}

Last edited on
Hello Leonardo797,

Neither piece of code will compile. Both are full of errors.

I do not know why you think that unicode characters («) or (») are what you need when just (<<) and (>>) are what is needed. This may look fancy in your code, but just causes errors.

Line 6 in each code is a comment when it should be defining your constant variables.

First you need to fix the errors to see if will compile successfully.

Andy
Hello Leonardo797,

After I fixed the errors I did make a change to the struct.

1
2
3
4
5
6
7
8
struct Info
{
    char name[NAME_SIZE]{};
    int age{};
    char addressl[ADDR_SIZE]{};
    char address2[ADDR_SIZE]{};
    char phone[PHONE_SIZE]{};
};

I initialized all the variables.

I also had to add "again". Used, but never defined. Needs to be defined before the do/while loop.

Andy
thanks, andy,

can you post the full code thx
Last edited on
Hello Leonardo797,

No. The code I have has nothing to do with the changes to the code that you made in your OP. Even the instructions appear to have changed.

Also you should not have changed the original post with this new code. Either close this topic and start another or post the new code in a reply.

It will take a little time to understand all the new changes.

Andy
Note changes to the function definitions:

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

struct CorpData
{
	string DivName;     // Division name
	double FstQtr {},	// First quarter sales
		SecQtr {},		// second quarter sales
		TrdQtr {},		// Third quarter sales
		FthQtr {},		// Fourth quarter sales
		TotAnn {},		// Total annual sales
		AvgQtrly {};	// Average quarterly sales

 // constructor that allows the division name and four quarterly
 // sales amounts to be specified at the time a CorpData variable
 // is created.
	CorpData(const string& D, double f, double s, double t, double fr) : DivName(D), FstQtr(f), SecQtr(s), TrdQtr(t), FthQtr(fr) {}
};

// Function Prototypes
void calcSales(CorpData&);
void displayDivInfo(const CorpData&);

int main()
{   // Creates four variables of the CorpData structure
	// each representing a corporate division.
	CorpData East("East", 1000, 1500, 1250, 1750);
	CorpData West("West", 2500, 2000, 2750, 2250);
	CorpData North("North", 3750, 3500, 3250, 3000);
	CorpData South("South", 4250, 4750, 4000, 4500);

	calcSales(East);
	calcSales(West);
	calcSales(North);
	calcSales(South);

	displayDivInfo(East);
	displayDivInfo(West);
	displayDivInfo(North);
	displayDivInfo(South);
}

/*************************************************************
 *                       calcSales                           *
 * This function accepts a CorpData variable as an argument. *
 * calculates and stores total sales and average quarterly   *
 * sales for that division.                                  *
 *************************************************************/
void calcSales(CorpData& D)
{
	D.TotAnn = D.FstQtr + D.SecQtr + D.TrdQtr + D.FthQtr;
	D.AvgQtrly = D.TotAnn / 4;
}

/*************************************************************
 *                     displayDivInfo                        *
 * This function accepts a CorpData variable as an argument. *
 * Displays the division name, totals sales and quarterly    *
 * average.                                                  *
 *************************************************************/
void displayDivInfo(const CorpData& D)
{
	cout << "\n           Sales Data\n"
		<< "---------------------------------------\n";
	cout << fixed << showpoint << setprecision(2);
	cout << "Division :          " << D.DivName << endl;
	cout << "Total Sales :       $" << D.TotAnn << endl;
	cout << "Quarterly average : $" << D.AvgQtrly << endl;
}


Note that displayDivInfo() and calcSales() would usually be member functions of CorpData and not stand-alone functions:

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

struct CorpData
{
	string DivName;     // Division name
	double FstQtr {},	// First quarter sales
		SecQtr {},		// second quarter sales
		TrdQtr {},		// Third quarter sales
		FthQtr {},		// Fourth quarter sales
		TotAnn {},		// Total annual sales
		AvgQtrly {};	// Average quarterly sales

	CorpData(const string& D, double f, double s, double t, double fr) : DivName(D), FstQtr(f), SecQtr(s), TrdQtr(t), FthQtr(fr) {}

	void calcSales()
	{
		TotAnn = FstQtr + SecQtr + TrdQtr + FthQtr;
		AvgQtrly = TotAnn / 4;
	}

	void displayDivInfo()
	{
		cout << "\n           Sales Data\n"
			<< "---------------------------------------\n";
		cout << fixed << showpoint << setprecision(2);
		cout << "Division :          " << DivName << endl;
		cout << "Total Sales :       $" << TotAnn << endl;
		cout << "Quarterly average : $" << AvgQtrly << endl;
	}
};

int main()
{
	// Creates four variables of the CorpData structure
	// each representing a corporate division.
	CorpData East("East", 1000, 1500, 1250, 1750);
	CorpData West("West", 2500, 2000, 2750, 2250);
	CorpData North("North", 3750, 3500, 3250, 3000);
	CorpData South("South", 4250, 4750, 4000, 4500);

	East.calcSales();
	West.calcSales();
	North.calcSales();
	South.calcSales();

	East.displayDivInfo();
	West.displayDivInfo();
	North.displayDivInfo();
	South.displayDivInfo();
}

Last edited on
Topic archived. No new replies allowed.