### 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:
 ``12345`` ``````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:
 ``123456`` ``````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?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990`` ``````#include #include #include #include #include // 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 } :

 ``1234567891011121314`` ``````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):
 ``12345678910111213141516171819`` ``````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?
 ``1234567891011121314151617181920`` ``````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:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146`` ``````#include #include #include #include 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

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