| | |
Swapping chars within an array
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2007
Posts: 21
Reputation:
Solved Threads: 0
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:
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:
And i am using this for the surname:
I dont think tis algorithym is working though. Any help greatful. Thanks.
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.
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);
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*
Since structure assignment is supported, just do
where temp is the same type as the struct used for your array.
C++ Syntax (Toggle Plain Text)
temp = Quote[j+1]; Quote[j+1] = Quote[j]; Quote[j] = temp;
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: 3d to 1d Array Conversion
- Next Thread: HTTP Post VIA C or C++
Views: 2782 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






