Example
#{example}"); ipb.editor_values.get('templates')['togglesource'] = new Template(""); ipb.editor_values.get('templates')['toolbar'] = new Template(""); ipb.editor_values.get('templates')['button'] = new Template("
Emoticons
"); // Add smilies into the mix ipb.editor_values.set( 'show_emoticon_link', false ); ipb.editor_values.set( 'bbcodes', $H({"snapback":{"id":"1","title":"Post Snap Back","desc":"This tag displays a little linked image which links back to a post - used when quoting posts from the board. Opens in same window by default.","tag":"snapback","useoption":"0","example":"[snapback]100[/snapback]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"topic":{"id":"5","title":"Topic Link","desc":"This tag provides an easy way to link to a topic","tag":"topic","useoption":"1","example":"[topic=1]Click me![/topic]","switch_option":"0","menu_option_text":"Enter the topic ID","menu_content_text":"Enter the title for this link","single_tag":"0","optional_option":"0","image":""},"post":{"id":"6","title":"Post Link","desc":"This tag provides an easy way to link to a post.","tag":"post","useoption":"1","example":"[post=1]Click me![/post]","switch_option":"0","menu_option_text":"Enter the Post ID","menu_content_text":"Enter the title for this link","single_tag":"0","optional_option":"0","image":""},"spoiler":{"id":"7","title":"Spoiler","desc":"Spoiler tag","tag":"spoiler","useoption":"0","example":"[spoiler]Some hidden text[/spoiler]","switch_option":"0","menu_option_text":"","menu_content_text":"Enter the text to be masked","single_tag":"0","optional_option":"0","image":""},"acronym":{"id":"8","title":"Acronym","desc":"Allows you to make an acronym that will display a description when moused over","tag":"acronym","useoption":"1","example":"[acronym='Laugh Out Loud']lol[/acronym]","switch_option":"0","menu_option_text":"Enter the description for this acronym (EG: Laugh Out Loud)","menu_content_text":"Enter the acronym (EG: lol)","single_tag":"0","optional_option":"0","image":""},"hr":{"id":"12","title":"Horizontal Rule","desc":"Adds a horizontal rule to separate text","tag":"hr","useoption":"0","example":"[hr]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"1","optional_option":"0","image":""},"php":{"id":"14","title":"PHP Code","desc":"Allows you to enter PHP code into a formatted/highlighted syntax box","tag":"php","useoption":"0","example":"[php]$variable = true;\n\nprint_r($variable);[/php]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"html":{"id":"15","title":"HTML Code","desc":"Allows you to enter formatted/syntax-highlighted HTML code","tag":"html","useoption":"0","example":"[html]\n \n[/html]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"sql":{"id":"16","title":"SQL Code","desc":"Allows you to enter formatted/syntax-highlighted SQL code","tag":"sql","useoption":"0","example":"[sql]SELECT p.*, t.* FROM posts p LEFT JOIN topics t ON t.tid=p.topic_id WHERE t.tid=7[/sql]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"xml":{"id":"17","title":"XML Code","desc":"Allows you to enter formatted/syntax-highlighted XML code","tag":"xml","useoption":"0","example":"[xml]19 Replies - 132 Views - Last Post: Today, 08:29 PM
#1
Reputation: 1
- Posts: 36
- Joined: 19-September 12
Posted Today, 12:42 PM
Hey guys,I was hoping for some direction on this problem I have. I've gotten my program to sort a .dat file that will sort a list of students (who have enrolled in various classes) by their last names. The problem is some students have the same last name and will then need to be sorted by their first names. Something tells me I'll need to come up with another bool operator function to handle my first names as it does with last but I'm stuck (been going at it for a couple hours). Here are the files.
reportwriter.cpp
#include <algorithm> #include <fstream> #include <iostream> #include <string> #include "enrolledstudent.h" using namespace std; int main() { EnrolledStudent stu; string scratch; EnrolledStudent students[5000]; int numStudents = 0; // Read in the enrollment data while (cin >> stu.course >> stu.section >> stu.lastName >> stu.firstName) { students[numStudents] = stu; ++numStudents; getline (cin, scratch); // discard rest of line } // Sort the enrollment data sort (students, students+numStudents); // Print the report for (int i = 0; i < numStudents; ++i) { cout << students[i].course << "\t" << students[i].section << "\t" << students[i].lastName << ", " << students[i].firstName << endl; } return 0; }
enrolledstudent.h
#ifndef ENROLLEDSTUDENT_H #define ENROLLEDSTUDENT_H #include <string> struct EnrolledStudent { std::string lastName; std::string firstName; std::string course; int section; }; bool operator< (const EnrolledStudent& left, const EnrolledStudent& right); #endif
enrolledstudent.cpp
#include "enrolledstudent.h" using namespace std; bool operator< (const EnrolledStudent& left, const EnrolledStudent& right) { return left.lastName < right.lastName; { return left.firstName < right.firstName; } }
I'll admit this is for school but it is ungraded so I do not NEED to do it. What kind of comp sci student would I become if I didn't try to understand the material though right? Any help would be appreciated. Thanks all.
Is This A Good Question/Topic? 0
Replies To: Sort by last name then by first name
#2
Reputation: 4888
- Posts: 11,284
- Joined: 16-October 07
Re: Sort by last name then by first name
Posted Today, 01:02 PM
bool operator< (const EnrolledStudent& left, const EnrolledStudent& right) { return left.lastName < right.lastName; { return left.firstName < right.firstName; } }
What on earth do you think that means?
Simply, if lastName is < then true, if lastName is > then false, if last name is... then you look at the next criteria.
#3
Reputation: 1
- Posts: 36
- Joined: 19-September 12
Re: Sort by last name then by first name
Posted Today, 01:25 PM
I figured that would be the way to handle it however, the results are coming back as such:Zahir, Gustavo
Zahir, Cryshel
.......other names
.......other names
Zimmerebner, Jean
Zimmerebner, Ayako
when it should come back as:
Zahir, Cryshel
Zahir, Gustavo
.......other names
.......other names
Zimmerebner, Ayako
Zimmerebner, Jean
Just to see what would happen I also tried an if statement.
#include "enrolledstudent.h" using namespace std; bool operator< (const EnrolledStudent& left, const EnrolledStudent& right) { return left.lastName < right.lastName; if (left.lastName > right.lastName) { return left.firstName < right.firstName; } }
Thanks for your reply.
#4
Reputation: 1929
- Posts: 5,757
- Joined: 05-May 12
Re: Sort by last name then by first name
Posted Today, 01:54 PM
I think that you are forgetting that the return statement returns out of the current function. So there is no way to get to your if statement after your return statement.
#5
Reputation: 1
- Posts: 36
- Joined: 19-September 12
Re: Sort by last name then by first name
Posted Today, 02:13 PM
Ah that makes sense. Anything afterwards was being disregarded correct? I have tried two other things given that.#include "enrolledstudent.h" using namespace std; bool operator< (const EnrolledStudent& left, const EnrolledStudent& right) { left.lastName < right.lastName; if (left.lastName > right.lastName) { return left.firstName < right.firstName; } }
Result - randomness with no logical sort to the names.
and
#include "enrolledstudent.h" using namespace std; bool operator< (const EnrolledStudent& left, const EnrolledStudent& right) { left.lastName < right.lastName; if (left.lastName = right.lastName) { return left.firstName < right.firstName; } }
#6
Reputation: 1929
- Posts: 5,757
- Joined: 05-May 12
Re: Sort by last name then by first name
Posted Today, 02:26 PM
The line:left.lastName < right.lastName;
is effectively a do nothing line.
In your first chunk of code:
if (left.lastName > right.lastName) { return left.firstName < right.firstName; }
Only if left.lastName is greater than right.lastName, will there be a true or false be returned depending on whether left.firstName is less than right.firstName. If left.lastName is less than or equal to right.lastName, then you run into undefined behavior because you don't explicitly say what to return.
In your second chuck of code:
if (left.lastName = right.lastName) { return left.firstName < right.firstName; }
The single '=' is an assignment and not a comparison. In most cases, the assignment will be non-zero, and so the returned value will be true or false depending on whether left.firstName is less than right.firstName.
The code logic that you need is something like this:
if left.lastName is the same as right.lastName return true if left.firstName is less than right.firstName return true if left.lastName is less than right.lastName
#7
Reputation: 2486
- Posts: 8,533
- Joined: 08-August 08
Re: Sort by last name then by first name
Posted Today, 02:29 PM
What do you think this line does?left.lastName < right.lastName;
Edit: too slow.
This post has been edited by CTphpnwb: Today, 02:30 PM
#8
Reputation: 1
- Posts: 36
- Joined: 19-September 12
Re: Sort by last name then by first name
Posted Today, 02:41 PM
CTphpnwb, on 23 May 2013 - 02:29 PM, said:
What do you think this line does?left.lastName < right.lastName;
Edit: too slow. />
It should sort the students by their last names. With As being at the top of the report printout and Zs atvthe bottom.
#9
Reputation: 5672
- Posts: 22,526
- Joined: 23-August 08
Re: Sort by last name then by first name
Posted Today, 02:46 PM
Quote
I'll admit this is for school but it is ungraded so I do not NEED to do it. What kind of comp sci student would I become if I didn't try to understand the material though right?
While I truly admire your willingness to attempt something which you don't need, it would appear to me you really need to go back to the basics before jumping into this.
Not knowing that statement does nothing tells me you lack a fundamental understanding of C++.
#10
Reputation: 1
- Posts: 36
- Joined: 19-September 12
Re: Sort by last name then by first name
Posted Today, 02:52 PM
JackOfAllTrades, on 23 May 2013 - 02:46 PM, said:
Quote
I'll admit this is for school but it is ungraded so I do not NEED to do it. What kind of comp sci student would I become if I didn't try to understand the material though right?
While I truly admire your willingness to attempt something which you don't need, it would appear to me you really need to go back to the basics before jumping into this.
Not knowing that statement does nothing tells me you lack a fundamental understanding of C++.
Nothing without return in front I just answered in assumption that it was there. Could someone then perhaps explain what to do then so I can learn? It would be appreciated
#11
Reputation: 3057
- Posts: 9,302
- Joined: 25-December 09
Re: Sort by last name then by first name
Posted Today, 03:15 PM
If you are trying to sort the names based on last name and first name I suggest you combine the last and first names of each student before you compare these two "complete" names.bool operator< (const EnrolledStudent& left, const EnrolledStudent& right) { std::string lstud = left.lastName + " " + left.firstName; std::string rstud = right.lastName + " " + right.firstName; // Now compare lstud to rstud and return the result. }
Jim
#12
Reputation: 1
- Posts: 36
- Joined: 19-September 12
Re: Sort by last name then by first name
Posted Today, 06:09 PM
I've made a huge jump and with the revisions with all of the help received from this forum have now managed to pass 4 of the 5 tests this program was run through, each worth 16 points. Again, it is ungraded but hey its a nice confidence booster. Here are the revisions.#include "enrolledstudent.h" using namespace std; bool operator< (const EnrolledStudent& left, const EnrolledStudent& right) { std::string lhs = left.lastName + left.firstName; std::string rhs = right.lastName + right.firstName; if (lhs == rhs) { return left.firstName < right.firstName; } else { return left.lastName < right.lastName; } }
The test I didn't pass says that an arbitrary mixture of students, among different sections
and courses are involved and some sections have multiple students.
To my understanding this is just another "probable case" that can occur during the test. Much like if the students had the same last name then the program was to sort by the first name. I would assume from this "hint" given that if a section has multiple students to then sort them in some fashion. I'll continue to fool with that and probably implement the std::string course and int section from my enrolledstudent.h. Thanks again and if you wish to add your thoughts on the last section feel free!
#13
Reputation: 3057
- Posts: 9,302
- Joined: 25-December 09
Re: Sort by last name then by first name
Posted Today, 06:41 PM
In your code snippet what is the purpose of your rhs and lhs strings?Jim
#14
Reputation: 1
- Posts: 36
- Joined: 19-September 12
Re: Sort by last name then by first name
Posted Today, 06:52 PM
I put them in so that I could have something to compare. Kind of like two variables kind of thinking x1 and x2. I could then say if x1 == x2 do "this". Did I not need that? (was it redundant?)
#15
Reputation: 3057
- Posts: 9,302
- Joined: 25-December 09
Re: Sort by last name then by first name
Posted Today, 07:42 PM
Quote
I put them in so that I could have something to compare.
Then why aren't you using them in some kind of comparison instead of left and right?
Jim
Source: http://www.dreamincode.net/forums/topic/321683-sort-by-last-name-then-by-first-name/
lottery Preakness results justin bieber John Hurt taylor swift taylor swift Candice Glover
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.