944,044 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4934
  • C++ RSS
Feb 20th, 2007
0

Swapping chars within an array

Expand Post »
Hey, i am trying to do a sort in C++. I have a strucutre of different records such as Quote number, Surname, Total cost and Deliverycost. I am trying to sort in order of Quote Number. Im using a bubble sort to do this:

for(i = 1; (i <= LastRefNum) && flag; i++)
{
flag = 0;
for (j=0; j < LastRefNum; j++)
{
if (Quote[j+1].RefNumber < Quote[j].RefNumber)
{
//Swap the records here
}



Its the swapping thats the problem. It works for the quote number, and costs but the names won't swap. Im using this code to swap the int's places:


RefnumberTemp = Quote[j].RefNumber;
Quote[j].RefNumber = Quote[j+1].RefNumber;
Quote[j+1].RefNumber = RefnumberTemp;


And i am using this for the surname:

strcpy (SurnameTemp, Quote[j].Surname);
strcpy (Quote[j+1].Surname, Quote[j].Surname);
strcpy (SurnameTemp, Quote[j+1].Surname);


I dont think tis algorithym is working though. Any help greatful. Thanks.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
iaaan is offline Offline
23 posts
since Jan 2007
Feb 20th, 2007
0

Re: Swapping chars within an array

Are the names defined as std::strings or char[].

If it is char[] don't forget you need to use strcmp to actually compare them.

If you are using std::string you can overload the > operator.
Last edited by iamthwee; Feb 20th, 2007 at 4:52 pm.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Feb 20th, 2007
0

Re: Swapping chars within an array

Quote ...
Are the names defined as std::strings or char[]
They are defined as char[],

So i compare them first, and then swap them? The swap doesn't seem to work though.

And why do i need to compare them before i swap them?

Thanks, Ian
Last edited by iaaan; Feb 20th, 2007 at 5:00 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
iaaan is offline Offline
23 posts
since Jan 2007
Feb 20th, 2007
0

Re: Swapping chars within an array

RefnumberTemp = Quote[j].RefNumber;
Quote[j].RefNumber = Quote[j+1].RefNumber;
Quote[j+1].RefNumber = RefnumberTemp;

Assuming the above logic works shouldn't it the the below?

strcpy (SurnameTemp, Quote[j].Surname);
strcpy (Quote[j].Surname, Quote[j+1].Surname);
strcpy (Quote[j+1].Surname,SurnameTemp);
Last edited by iamthwee; Feb 20th, 2007 at 5:10 pm.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Feb 20th, 2007
0

Re: Swapping chars within an array

And you need strcmp. Use it like follows:

http://www.cppreference.com/stdstring/strcmp.html
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Feb 20th, 2007
0

Re: Swapping chars within an array

Since structure assignment is supported, just do
C++ Syntax (Toggle Plain Text)
  1. temp = Quote[j+1];
  2. Quote[j+1] = Quote[j];
  3. Quote[j] = temp;
where temp is the same type as the struct used for your array.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Feb 20th, 2007
0

Re: Swapping chars within an array

Worked perfect, thanks guys.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
iaaan is offline Offline
23 posts
since Jan 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: 3d to 1d Array Conversion
Next Thread in C++ Forum Timeline: can anybody help me with this





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC