12 Replies - 963 Views - Last Post: 01 December 2009 - 08:46 PM Rate Topic: -----

#1 react05   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 29-September 09

Help with sorting names in c++

Post icon  Posted 01 December 2009 - 10:21 AM

hi, how would i sort this by names?

void sort (name [], int n)
{
int last;
int j;
bool haveSwap;
int pass;
double temp;
last = 1;
for (pass = 1; pass <= n-1; pass++)
{
haveSwap = false;
for (j = n-1; j >= last; j--)
{
if (s[j].lastName < s[j-1].lastName)
{
haveSwap = true;
temp = s[j].lastName;
s[j].lastName = s[j-1].lastName;
s[j-1] = temp;
}
}
if (haveSwap == false)
pass = n;
last++;
}
}


This post has been edited by react05: 01 December 2009 - 11:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Help with sorting names in c++

#2 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Help with sorting names in c++

Posted 01 December 2009 - 10:30 AM

to compare two c-style strings (char arrays) you should use strcmp() -- you can't use > and <

A char array is just an array of chars... so if you compare s[j].lastName < s[j-1].lastName you are just comparing two pointers to the arrays and not any actual data in the arrays.
Was This Post Helpful? 0
  • +
  • -

#3 react05   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 29-September 09

Re: Help with sorting names in c++

Posted 01 December 2009 - 10:40 AM

Oh i see. so should i do this ?

if(strcmp s[j].lastName, s[j-1].lastName < 0)
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Help with sorting names in c++

Posted 01 December 2009 - 11:00 AM

...well your getting closer... just need to figure out the syntax. I provided a link in my post above to an example using strcmp().
Was This Post Helpful? 0
  • +
  • -

#5 react05   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 29-September 09

Re: Help with sorting names in c++

Posted 01 December 2009 - 01:06 PM

alright, ill look at that.
Was This Post Helpful? 0
  • +
  • -

#6 react05   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 29-September 09

Re: Help with sorting names in c++

Posted 01 December 2009 - 01:18 PM

i think i got it.

This post has been edited by react05: 01 December 2009 - 11:31 PM

Was This Post Helpful? 0
  • +
  • -

#7 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3207
  • View blog
  • Posts: 19,239
  • Joined: 14-September 07

Re: Help with sorting names in c++

Posted 01 December 2009 - 01:22 PM

What do you mean, how does it look?

Have you run it? Does it do what you want/intend? If the answers to the previous two questions is yes, then yes it "looks" fine.


Specific questions get you specific assistance.
Was This Post Helpful? 0
  • +
  • -

#8 react05   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 29-September 09

Re: Help with sorting names in c++

Posted 01 December 2009 - 01:30 PM

I mean, im skipping around parts of my programs so i can't even run it yet.
Was This Post Helpful? 0
  • +
  • -

#9 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Help with sorting names in c++

Posted 01 December 2009 - 01:34 PM

my advice when programming -- do a little, then test, do a little then test, do a little then test....

You should be compiling your code every half hour or so... if your not, then you are not programming right...

I don't see how anyone could hope to craft a full program and expect it to compile when you finally get all the logic in there... terrible way to program.
Was This Post Helpful? 0
  • +
  • -

#10 react05   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 29-September 09

Re: Help with sorting names in c++

Posted 01 December 2009 - 01:48 PM

ok. thanks for the advice. im new to this stuff and im not very good at it either.

This post has been edited by react05: 01 December 2009 - 11:30 PM

Was This Post Helpful? 0
  • +
  • -

#11 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Help with sorting names in c++

Posted 01 December 2009 - 02:36 PM

strcmp() is for comparing strings -- for doubles values (like gpa) you can use the > and < operators.
Was This Post Helpful? 0
  • +
  • -

#12 react05   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 29-September 09

Re: Help with sorting names in c++

Posted 01 December 2009 - 02:38 PM

Oh! i see. i gotcha. thanks!
Was This Post Helpful? 0
  • +
  • -

#13 ccubed   User is offline

  • It's That Guy
  • member icon

Reputation: 165
  • View blog
  • Posts: 1,416
  • Joined: 13-June 08

Re: Help with sorting names in c++

Posted 01 December 2009 - 08:46 PM

http://www.dreaminco...snippet2958.htm

That snippet's been around awhile. plus there's one from gabe.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1