Swapping chars within an array

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2007
Posts: 21
Reputation: iaaan is an unknown quantity at this point 
Solved Threads: 0
iaaan iaaan is offline Offline
Newbie Poster

Swapping chars within an array

 
0
  #1
Feb 20th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Swapping chars within an array

 
0
  #2
Feb 20th, 2007
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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 21
Reputation: iaaan is an unknown quantity at this point 
Solved Threads: 0
iaaan iaaan is offline Offline
Newbie Poster

Re: Swapping chars within an array

 
0
  #3
Feb 20th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Swapping chars within an array

 
0
  #4
Feb 20th, 2007
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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Swapping chars within an array

 
0
  #5
Feb 20th, 2007
And you need strcmp. Use it like follows:

http://www.cppreference.com/stdstring/strcmp.html
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Swapping chars within an array

 
0
  #6
Feb 20th, 2007
Since structure assignment is supported, just do
  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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 21
Reputation: iaaan is an unknown quantity at this point 
Solved Threads: 0
iaaan iaaan is offline Offline
Newbie Poster

Re: Swapping chars within an array

 
0
  #7
Feb 20th, 2007
Worked perfect, thanks guys.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 2782 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC