Hello,
i dont know where is the problem.
i should input some data in the linked-list.
when i put 2 person,the result just show the last person.
can someone help me to solve this prob ?
this is my code :
Code:REMOVED
Printable View
Hello,
i dont know where is the problem.
i should input some data in the linked-list.
when i put 2 person,the result just show the last person.
can someone help me to solve this prob ?
this is my code :
Code:REMOVED
Hm?
Code:system("CLS");
oo yeah..
my fault.
hahahahaha.
thanks.
can someone show me how to list out person who have salary less than 100 ?
Can't you just go through the list (like your code currently does) and then use an if statement to check the salary and only output if the salary is less than 100?
Give it a go, and if you can't solve it yourself, post your attempt.
--
Mats
ok,
here is my code
the compiler dont show any error.Code:REMOVED
but when i test the program,it crash.
maybe stuck in the loop or something ?
If you want to list ONLY those who are under 100 in salary, perhaps you should put the if-statement inside your loop.
If you want to first print ALL entries, then only the ones under 100, then you need two loops.
Your current code crashes, because temp3 is NULL when the loop is done.
--
Mats
ok,Code:REMOVED
i put inside the loop and it still crash.
Your if-statement is after the "next" step, which means on the last iteration, it's going to access NULL->salary, which is an invalid memory location.
--
Mats
ok mats,
i found the solution,
i put it like this
however,it seems not so good if we have more than 1 person that have less than 100 salary and not a great solution since the person who have salary less than 100 always appear before NAME,SALARY...etc.Code:REMOVED
maybe you can give me some other ideas ?
What part of that code is outputting the person information?
When you find that code, put it inside the if block since you only want the output to happen if the if statement is true.
OK,i did a lot of changes in my code.
thanks to mats who give the idea to me.
here is my code
now i faced with 2 more problems.Code:REMOVED
1.for Person with salary less than 100 and not in SALES DEPMNT,the system should display only person who do not work under SALES DEPMNT.but i dont know why the system show the person with SALES department also.where is the problem ?
2.how to make the system can calculate how much average salary to person who join after 1 Jan 2008 ? i declared date as string.i dont think system know it is a date.need some idea from u.
The problem isand to fix this you'll need to beCode:REMOVED
:pCode:REMOVED
Your code still doesn't look quite right. It is moving to the next value only in the if block.
LOL. REMOVED ?
hahahaha
i removed old codes.
and i put my new codes.
daved,
whats wrong with my code ?
i get the output that i need.
i just facing with 2 more problems.
for prob no 1.....
temp5->department != "SALES" is comparing pointers.
what i need to do is like this
strcmp(s,"exit")!=0
how to change the pointer back to char ?
i did like this :
but it gives me compilation error.Code:char *dp;
temp5->department = *dp;
if(temp5->salary < 100 && strcmp(dp,"SALES")==0)
1. Mostly because of the way you attempt to compare. You understand how to apply your filter, but character arrays compare their memory locations with !=, not their contents. The fix is to use std::strings, where the != operator does what you need.
2. Try using a simpler format like "yyyy/mm/dd" and using a string comparison:
It just so happens that later dates, when stored as text, can be ordered chronologically this way.Code:#include <string>
struct node {
std::string date;
// other members ...
};
// ... in the filter:
if( node.date > "2008/01/01" )
// then ...
Well I suppose I could come up with an example you could use. Not much of this will look familiar but you will get the idea. You should probably use a more C++ like approach to this anyway.
You need to build a class for your linked list. First, concentrate on creating and then deleting nodes. After you get that working, then you have a solid foundation to build upon, because you can properly make and destroy a list, and it will be time to move on to writing the algorithms you need.
You will copy your data into a list.
Then you will apply your filter like this, in pseudocode:
Hopefully that's not too bad.Code:procedure apply_filter ( this_filter_func f ) :
i = 0;
while i < list.size() :
if f( list( i ) ) :
display list( i )
endif
endwhile
done
For a more concrete example, look at the C++ STL:
And now, if you look, the poor_list only contains the poor salesman named Joe.Code:#include <list>
#include <algorithm>
#include <string>
struct node
{
std::string name;
std::string dept;
float salary;
// other members ...
};
bool filter ( const node & n )
{
return n.salary > 100.0f || n.dept != "SALES";
}
int main()
{
// copy your data ubto a linked list:
node data[] = {
{ "Joe", "SALES", 99.0f }, { "Sam", "MGMT", 200.0f },
{ "Bob", "ACCNT", 80.0f }, { "Oliver", "MGMT", 399.0f }
};
std::list<node> full_list( data, data+4 );
std::list<node> poor_list( full_list.size() );
// apply your filter:
std::list<node>::iterator last_copied = std::remove_copy_if(
full_list.begin(), full_list.end(), poor_list.begin(), filter );
// fixing the list size:
poor_list.erase( last_copied, poor_list.end() );
}
I dunno if I expressed myself very well, but this is the sort of approach a more experienced person would take. If you want to build the linked list data structure yourself (always a fun, learning exercise), go ahead. Just make sure that you build appropriate functions to make a list, apply your filter to items in that list, and display the result.
Do you not understand that you should not do that?!
You make it very hard for others to help you, and for anyone else to learn from your mistakes, and is very selfish.
I genuinely did have helpful advice for the problems you were facing, and really did simply choose not to post it. Obviously my humourous hint did not express the "back-at ya" annoyance it was meant to convey, or you would have broken the habbit very quickly.
Next time you post, I'm going to quote your entire post in my own; You can't REMOVE that!
True
please do not remove your codes the next time.
This post has been useful for you but it's useless for others which would have learn from your mistake instead.