| | |
fstream isnt getting the data right
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
When you swap the name array, you'll have to swap all of the other arrays along with it, since each array is a unique object. A good alternative is to stuff everything in an structure and then sort an array of structs:
That would save you a lot of work because you can treat a record as a single unit rather than an index in a bunch of arrays.
C++ Syntax (Toggle Plain Text)
struct record { string name; // ... } rec[N];
•
•
Join Date: Mar 2005
Posts: 16
Reputation:
Solved Threads: 0
But I think I need to use the arrays because there are 30 records. I need to sort the records by names. I got the Last name down but what should I do to get that person other records along with his last name?
I try this but it would'nt display the first name
I try this but it would'nt display the first name
void sortLastname(int a)
{
string temp = string();
string temp2 = string();
char pick = char();
for(int i=0; i<30; i++)
{
for (int j=0; j<30-i; j++)
{
if (lname[j] > lname[j+1]) // compare the two neighbors
{
temp = lname[j];
lname[j] = lname[j+1]; // swap lname[j] and lname[j+1]
lname[j+1] = temp;
for(int q=0; q<30; q++)
{
for (int w=0; w<30-i; w++)
{
if (fname[w] > fname[w+1]) // compare the two neighbors
{
temp = fname[j];
fname[w] = fname[w+1]; // swap fname[w] and fname[w+1]
fname[w+1] = temp2;
}
}
}
}
}
cout << temp << " " <<temp2 <<endl;
//cout << setw(5) << left << ID[i] << setw(10) << left << fname[i] << setw(12) << lname[i] << setw(2) << middlei[i] << setw(7) << state[i] << setw(5) << right << zip[i] << setw(6) << right << hrWork[i] << setw(8) << right << ratePhour[i] << endl;
} You're sorting by name, and name only. Just because a name goes in one spot doesn't mean the corresponding data goes in the same spot if you sort it too. This is basically what you're doing:
To get it to work, you need to pass the other arrays and swap them too, but only use name in the test:
Or you can lump everything into a record structure and avoid that hassle and keep everything together automatically:
C++ Syntax (Toggle Plain Text)
#include <algorithm> #include <iostream> #include <iterator> template <typename T> void bubble(T list[], int sz) { for (int i = 0; i < sz; i++) { for (int j = i; j < sz; j++) { if (list[j] < list[i]) std::swap(list[i], list[j]); } } } int main() { int a[] = {9,8,7,6,5,4,3,2,1,0}; int b[] = {6,7,5,8,9,0,4,3,2,1}; copy(a, a + 10, std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; copy(b, b + 10, std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; bubble(a, 10); copy(a, a + 10, std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; copy(b, b + 10, std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; }
C++ Syntax (Toggle Plain Text)
#include <algorithm> #include <iostream> #include <iterator> template <typename T> void bubble(T list1[], T list2[], int sz) { for (int i = 0; i < sz; i++) { for (int j = i; j < sz; j++) { if (list1[j] < list1[i]) { std::swap(list1[i], list1[j]); std::swap(list2[i], list2[j]); } } } } int main() { int a[] = {9,8,7,6,5,4,3,2,1,0}; int b[] = {6,7,5,8,9,0,4,3,2,1}; copy(a, a + 10, std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; copy(b, b + 10, std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; bubble(a, b, 10); copy(a, a + 10, std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; copy(b, b + 10, std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; }
C++ Syntax (Toggle Plain Text)
#include <algorithm> #include <iostream> #include <iterator> struct record { int a; int b; }; void bubble(record list[], int sz) { for (int i = 0; i < sz; i++) { for (int j = i; j < sz; j++) { if (list[j].a < list[i].a) std::swap(list[i], list[j]); } } } int main() { record list[10] = { 9,6,8,7,7,5,6,8,5,9,4,0,3,4,2,3,1,2,0,1 }; for (int i = 0; i < 10; i++) std::cout << '(' << list[i].a << ',' << list[i].b << ')' << '\n'; std::cout << '\n'; bubble(list, 10); for (int i = 0; i < 10; i++) std::cout << '(' << list[i].a << ',' << list[i].b << ')' << '\n'; }
![]() |
Other Threads in the C++ Forum
- Previous Thread: New To Daniweb
- Next Thread: Using data i read from *.* files
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy desktop directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





