fstream isnt getting the data right

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: fstream isnt getting the data right

 
0
  #11
May 18th, 2005
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:
  1. struct record {
  2. string name;
  3. // ...
  4. } rec[N];
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 16
Reputation: Tetsu is an unknown quantity at this point 
Solved Threads: 0
Tetsu Tetsu is offline Offline
Newbie Poster

Re: fstream isnt getting the data right

 
0
  #12
May 19th, 2005
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

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;
	}
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: fstream isnt getting the data right

 
0
  #13
May 19th, 2005
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:
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <iterator>
  4.  
  5. template <typename T>
  6. void bubble(T list[], int sz)
  7. {
  8. for (int i = 0; i < sz; i++) {
  9. for (int j = i; j < sz; j++) {
  10. if (list[j] < list[i])
  11. std::swap(list[i], list[j]);
  12. }
  13. }
  14. }
  15.  
  16. int main()
  17. {
  18. int a[] = {9,8,7,6,5,4,3,2,1,0};
  19. int b[] = {6,7,5,8,9,0,4,3,2,1};
  20.  
  21. copy(a, a + 10, std::ostream_iterator<int>(std::cout, " "));
  22. std::cout << '\n';
  23. copy(b, b + 10, std::ostream_iterator<int>(std::cout, " "));
  24. std::cout << '\n';
  25.  
  26. bubble(a, 10);
  27.  
  28. copy(a, a + 10, std::ostream_iterator<int>(std::cout, " "));
  29. std::cout << '\n';
  30. copy(b, b + 10, std::ostream_iterator<int>(std::cout, " "));
  31. std::cout << '\n';
  32. }
To get it to work, you need to pass the other arrays and swap them too, but only use name in the test:
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <iterator>
  4.  
  5. template <typename T>
  6. void bubble(T list1[], T list2[], int sz)
  7. {
  8. for (int i = 0; i < sz; i++) {
  9. for (int j = i; j < sz; j++) {
  10. if (list1[j] < list1[i]) {
  11. std::swap(list1[i], list1[j]);
  12. std::swap(list2[i], list2[j]);
  13. }
  14. }
  15. }
  16. }
  17.  
  18. int main()
  19. {
  20. int a[] = {9,8,7,6,5,4,3,2,1,0};
  21. int b[] = {6,7,5,8,9,0,4,3,2,1};
  22.  
  23. copy(a, a + 10, std::ostream_iterator<int>(std::cout, " "));
  24. std::cout << '\n';
  25. copy(b, b + 10, std::ostream_iterator<int>(std::cout, " "));
  26. std::cout << '\n';
  27.  
  28. bubble(a, b, 10);
  29.  
  30. copy(a, a + 10, std::ostream_iterator<int>(std::cout, " "));
  31. std::cout << '\n';
  32. copy(b, b + 10, std::ostream_iterator<int>(std::cout, " "));
  33. std::cout << '\n';
  34. }
Or you can lump everything into a record structure and avoid that hassle and keep everything together automatically:
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <iterator>
  4.  
  5. struct record {
  6. int a;
  7. int b;
  8. };
  9.  
  10. void bubble(record list[], int sz)
  11. {
  12. for (int i = 0; i < sz; i++) {
  13. for (int j = i; j < sz; j++) {
  14. if (list[j].a < list[i].a)
  15. std::swap(list[i], list[j]);
  16. }
  17. }
  18. }
  19.  
  20. int main()
  21. {
  22. record list[10] = {
  23. 9,6,8,7,7,5,6,8,5,9,4,0,3,4,2,3,1,2,0,1
  24. };
  25.  
  26. for (int i = 0; i < 10; i++)
  27. std::cout << '(' << list[i].a << ',' << list[i].b << ')' << '\n';
  28.  
  29. std::cout << '\n';
  30. bubble(list, 10);
  31.  
  32. for (int i = 0; i < 10; i++)
  33. std::cout << '(' << list[i].a << ',' << list[i].b << ')' << '\n';
  34. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 16
Reputation: Tetsu is an unknown quantity at this point 
Solved Threads: 0
Tetsu Tetsu is offline Offline
Newbie Poster

Re: fstream isnt getting the data right

 
0
  #14
May 20th, 2005
Ah Thanks you help me a lot!
Thank you
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC