Data structures help

Pages: 12
@Albatross,

Thank you. I changed one thing and I got it to print out the names from the text file. Now I am just trying to figure out how to sort it alphabetically by last name, and again sort it by total sales from largest to smallest.

They did not specify which algorithm I am supposed to use.
If you're permitted, look at std::sort() in <algorithm>:
http://www.cplusplus.com/reference/algorithm/sort/

For your purpose, I think you want variation 2. Specifically, you can call:
std::sort(cust, cust+10, compName)

Where you've previously declared compFunc as:
1
2
3
4
5
bool compName(const customer &c1, const customer &c2)
{
   // WRITE CODE THAT RETURNS TRUE IF c1.name < c2.name
   // OTHERWISE, RETURN FALSE
}


Notice that all you have to do is write the function that compares two customers. std::sort() does the rest for you.

Then to sort by total sales you do:
1
2
3
4
5
6
bool compSales(const customer &c1, const customer &c2) {
{
    // RETURN TRUE IF TOTAL SALES FOR C1 > TOTAL SALES FOR C2
}
...
std::sort(cust, cust+10, compSales);

I don't work with boolean's to want to even attempt giving myself a bigger headache. However, I modified my code entirely and I think I got up to the sorting by total sales part. Can someone run it and see why it is saying 'i' was not declared in line 80?

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
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <algorithm> // for std::sort

using namespace std;

class customer{
      public:
      string first; // represents sequence - string
      string last;
      string state;
      double sHistory[3]; // Sales history for three years.
      double totalSales; // Total sales (adding all three years together)
      int purchaseUnits;
      };
      
void printcust(customer[], int); // voided to show that no actual parameter is there when called.
void sortname(customer[], int); // voided to show that no actual parameter is there when called.
void sortsales(customer[], int); // voided to show that no actual parameter is there when called.


int main () 
{
	
	int i = 0;
		customer custarray[10];
		
		ifstream infile;
		infile.open("infodata.txt");
		
		cout.setf(ios::floatfield); // floating point values are set to fixed
		cout.precision(3);  // specifies the maximum number to be displayed
		
		infile >> custarray[i].first;
		infile >> custarray[i].last;
		infile >> custarray[i].state;
		infile >> custarray[i].sHistory[0];
		infile >> custarray[i].sHistory[1];
		infile >> custarray[i].sHistory[2];
		infile >> custarray[i].purchaseUnits;
	
	
	
	while(infile)
	{
		i++;
		infile >> custarray[i].first;
		infile >> custarray[i].last;
		infile >> custarray[i].state;
		infile >> custarray[i].sHistory[0];
		infile >> custarray[i].sHistory[1];
		infile >> custarray[i].sHistory[2];
		infile >> custarray[i].purchaseUnits;
	
	}
	
	printcust(custarray, i);
	sortname(custarray, i);
	printcust(custarray, i);
	sortsales(custarray, i);
	printcust(custarray, i);
	
	system("pause");
	return 0;
	
}

//

	void sortsales (customer custarray[], int numb)
	{
		customer temp;
		
		for(int pass = 0; pass>numb; pass++)
			for (int i=0; i>numb-1; i++)
				if (custarray[i].purchaseUnits > custarray[i - 1].purchaseUnits)
						temp = custarray[i];
						custarray[i] = custarray[i - 1];
						custarray [i - 1] = temp;
						
		
		return;
		
	}
	
	
		
	]
Last edited on
CodingIsHard17 wrote:
see why it is saying 'i' was not declared in line 80?

Because i died on line 79 when it went out of scope.

Use some curly braces { and } to limit the scope of your for and if statements; this isn't python where you rely on indentation.
Last edited on
This is where I added the } :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void sortsales (customer custarray[], int numb)
	{
		customer temp;
		
		for(int pass = 0; pass>numb; pass++)
			for (int i=0; i>numb-1; i++)
				if (custarray[i].purchaseUnits > custarray[i - 1].purchaseUnits)
						temp = custarray[i];
						custarray[i] = custarray[i - 1];
						custarray [i - 1] = temp;
	}
						
		
		return;


But it is still dying on line 71 (i).

Here is how your compiler "sees" your function (braces added for clarity):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void sortsales(customer custarray[], int numb)
{
   customer temp;

   for (int pass = 0; pass > numb; pass++)
   {
      for (int i = 0; i > numb - 1; i++)
      {
         if (custarray[i].purchaseUnits > custarray[i - 1].purchaseUnits)
         {
            temp = custarray[i];
         }
      }
   }
   custarray[i] = custarray[i - 1];
   custarray[i - 1] = temp;

   return;
}


Did you mean this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void sortsales(customer custarray[], int numb)
{
   customer temp;

   for (int pass = 0; pass > numb; pass++)
   {
      for (int i = 0; i > numb - 1; i++)
      {
         if (custarray[i].purchaseUnits > custarray[i - 1].purchaseUnits)
         {
            temp = custarray[i];
         }

         custarray[i] = custarray[i - 1];
         custarray[i - 1] = temp;
      }
   }

   return;
}

C++ does not care about whitespace.

Learn to use braces.
Thank you. If the braces don't get me then the semicolons do. Here is how I modified it:

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

class customer{
      public:
      string first; // represents sequence - string
      string last;
      string state;
      double sHistory[3]; // Sales history for three years.
      double totalSales; // Total sales (adding all three years together)
      int purchaseUnits;
      };
      
void printcust(customer[], int); // voided to show that no actual parameter is there when called.
void sortname(customer[], int); // voided to show that no actual parameter is there when called.
void sortsales(customer[], int); // voided to show that no actual parameter is there when called.


int main () 
{
	
	int i = 0;
		customer custarray[10];
		
		ifstream infile;
		infile.open("infodata.txt");
		
		cout.setf(ios::floatfield); // floating point values are set to fixed
		cout.precision(3);  // specifies the maximum number to be displayed
		
		infile >> custarray[i].first;
		infile >> custarray[i].last;
		infile >> custarray[i].state;
		infile >> custarray[i].sHistory[0];
		infile >> custarray[i].sHistory[1];
		infile >> custarray[i].sHistory[2];
		infile >> custarray[i].purchaseUnits;
	
	
	
	while(infile)
	{
		i++;
		infile >> custarray[i].first;
		infile >> custarray[i].last;
		infile >> custarray[i].state;
		infile >> custarray[i].sHistory[0];
		infile >> custarray[i].sHistory[1];
		infile >> custarray[i].sHistory[2];
		infile >> custarray[i].purchaseUnits;
	
	}
	
	printcust(custarray, i);
	sortname(custarray, i);
	printcust(custarray, i);
	sortsales(custarray, i);
	printcust(custarray, i);
	
	system("pause");
	return 0;
	
}

// sorting by alphabetically by customer last name

	void sortbane(customer custarray[], int numb)
	{
		customer temp;
		
		for(int pass = 0; pass > numb; pass++)
		{
			for(int i=0; i>numb+1; i++)
			{
				if(custarray[i].last > custarray[i].last)
				{
					temp = custarray[i];
				}
				custarray[i] = custarray[i + 1];
				custarray[i + 1] = temp;
			}
		}
			return;
	}
	




// sorting by descending total sales

	void sortsales (customer custarray[], int numb)
	{
		customer temp;
		
		for(int pass = 0; pass>numb; pass++)
		{
			for (int i=0; i>numb-1; i++)
			{
				if (custarray[i].purchaseUnits > custarray[i - 1].purchaseUnits)
				{
					temp = custarray[i];
				}
				
				custarray[i] = custarray[i - 1];
				custarray [i - 1] = temp;
			}
		}
	
	return;
	}
		
	
// printing information of customers

void printcust(customer custarray[], int numb)
{
	cout << "\tFirst" << "\t\tLast" << "\tState" << "\tSales History";
	cout << "\tTotal Sales" << endl;
	cout << setw(50) << "0    1    2" << endl << endl;
	
	for(int i = 0; i<numb; i++)
	{
		cout << setw(12) << custarray[i].first;
		cout << setw(14) << custarray[i].last;
		cout << setw(7) << custarray[i].state;
		cout << setw(5) << custarray[i].sHistory[0];
		cout << setw(5) << custarray[i].sHistory[1];
		cout << setw(5) << custarray[i].sHistory[2];
		cout << setw(6) << custarray[i].purchaseUnits;
		
		custarray[i].totalSales = custarray[i].sHistory[0] + custarray[i].sHistory[1];
		custarray[i].totalSales = custarray[i].totalSales + custarray[i].sHistory[2];
		cout << setw(12) << custarray[i].totalSales << endl;
	}
	return;
		
	}
	
	
		
	


I got a "[Error] Id returned 1 exit status" what do I do for that?
You don't just have to add braces ... you have to put them in the right place! If two items are in the wrong order (line 104) then you need ALL the following three executable statements to swap them, not just setting temp!

Your linker error tells you that you haven't defined one function, and in cpp.sh, at least, it tells you which one (try it - gear wheel icon at the top right of your code sample). You probably think you have defined that function, but if you look carefully at it then you will find a misspelling.
Last edited on
Your linker error tells you that you haven't defined one function

The OP's issue was answered in another topic he created:

http://www.cplusplus.com/forum/general/262233/

(had a function declared with one name (void sortname), and the definition had the function name differently (void sortbane).
Last edited on
Registered users can post here. Sign in or register to post.
Pages: 12