infile into array and sort

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2007
Posts: 2,462
Reputation: zandiago is on a distinguished road 
Solved Threads: 25
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: infile into array and sort

 
0
  #11
Sep 23rd, 2007
I also tried something like
while(!inFile.eof)
...however, it says that the expression isn't valid. Pretty much what I wanted was for the program to stop/warn user in the event that there was an error in the infile...
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 2,462
Reputation: zandiago is on a distinguished road 
Solved Threads: 25
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: infile into array and sort

 
0
  #12
Sep 26th, 2007
Thanks to everyone for their assistance thus far. I have a bit of problem. When I try to compile the program...I get: Debug assertion failed (string subscript out of range)
So pretty much, the program was able to read the files from the infile..however since i inputted the loops I had those error messages. Below is my code:
  1. #include <cstring>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <cmath>
  5. #include <fstream>
  6. #include <string>
  7. #include <ctime>
  8.  
  9. using namespace std;
  10.  
  11.  
  12.  
  13. int main()
  14. {
  15.  
  16.  
  17. ifstream inFile;
  18. ofstream outFile;
  19.  
  20. inFile.open ("listnames.txt");
  21. outFile.open ("outnames");
  22.  
  23. string names[120];
  24. string name;
  25. int i, j, k;
  26. int count=120;
  27. while(getline(inFile,name))
  28. {
  29. for (i=0;i<count-1;i++)
  30. for (j=i+1;j<count; j++)
  31. if (name[i]>name[j])
  32. swap (name[i],name[j]);
  33. }
  34. for (k=0; k<120; k++)
  35. {
  36. if (i%5==0)
  37. cout<<endl;
  38. cout<<setw(2)<<name[i];
  39. }//end for i
  40.  
  41.  
  42.  
  43. inFile.close();
  44. outFile.close();
  45.  
  46. return 0;
  47. }
What I'm simply trying to do is sort the names in normal alphabetical order and then display them to the screen and print printer in 5 columns.(refer to my initial post for a copy of the inFile)...Thx much
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,632
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1497
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: infile into array and sort

 
0
  #13
Sep 26th, 2007
Suggestion: don't attempt to sort the array on every iteration of the input loop. Read all the strings and sort only one time after the loop terminates.

>>int count=120;
initialize that variable to 0 instead of the max array size. Then insert the new string into the array at count element and increment it in anticipation for another string.
  1. int count = 0;
  2. while(getline(inFile,name))
  3. {
  4. names[count] = name;
  5. ++count;
  6. }
Last edited by Ancient Dragon; Sep 26th, 2007 at 6:26 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 2,462
Reputation: zandiago is on a distinguished road 
Solved Threads: 25
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: infile into array and sort

 
0
  #14
Sep 26th, 2007
Thanks for you reply...we haven't reached checking for integer length, but i'll read up on it and see how it works.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,632
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1497
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: infile into array and sort

 
0
  #15
Sep 26th, 2007
don't check for integer length but check to make sure the program doesn't read in more strings than the array can hold, something like this
  1. int count = 0;
  2. while(count < 120 && getline(inFile,name))
  3. {
  4. names[count] = name;
  5. ++count;
  6. }

The above loop when either of the two conditions exist: either the value of count becomes 120 or when end-of-file is reached. After that loop terminates then you can code a bubble sort or some other sorting algorithm to sort the strings.
Last edited by Ancient Dragon; Sep 26th, 2007 at 6:36 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 2,462
Reputation: zandiago is on a distinguished road 
Solved Threads: 25
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: infile into array and sort

 
0
  #16
Sep 27th, 2007
  1. #include <cstring>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <cmath>
  5. #include <fstream>
  6. #include <string>
  7. #include <ctime>
  8.  
  9. using namespace std;
  10.  
  11.  
  12.  
  13. int main()
  14. {
  15.  
  16.  
  17. ifstream inFile;
  18. ofstream outFile;
  19.  
  20. inFile.open ("listnames.txt");
  21. outFile.open ("outnames");
  22.  
  23. string names[120];
  24. string name;
  25. int i, j, k;
  26. int count=0;
  27. while(count < 120 && getline(inFile,name))
  28. {
  29. names[count] = name;
  30. ++count;
  31. }
  32. {
  33. for (i=0;i<count-1;i++)
  34. for (j=i+1;j<count; j++)
  35. if (names[i]>names[j])
  36. swap (names[i],names[j]);
  37. }
  38. for (k=0; k<120; k++)
  39. {
  40. if (i%5==0)
  41. cout<<endl;
  42. cout<<setw(10)<<names[i];
  43. }
  44.  
  45. inFile.close();
  46. outFile.close();
  47.  
  48. return 0;
  49. }

Thats what I have so far....however, i only the the colums for one of the lines of my inFile...in particular 'ZOVUROPA'....i copied the inFile into Microsoft Word and it located that particular line of letters @ line 120....i don't see what i'm doing wrong.....also, it doesn't print out in 5 columns as i wanted...it prints out in 7 instead....thx for the assistance.
Last edited by Ancient Dragon; Sep 27th, 2007 at 9:06 am. Reason: add line numbers to code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,632
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1497
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: infile into array and sort

 
0
  #17
Sep 27th, 2007
line 38 uses k as the loop counter, but lines 40 and 42 use a different variable. Either correct lines 40 and 42, or change line 38 to re-use i integer (preferred). Programs should attempt to reuse variables, especially one-letter loop counters, whenever possible so that the program has fewer variables and makes it easier to understand.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 2,462
Reputation: zandiago is on a distinguished road 
Solved Threads: 25
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: infile into array and sort

 
0
  #18
Sep 27th, 2007
Thanks Acient Dragon.....thx for the info...i never knew that we could re-use integers..our professor never made any mention of that...This solves the problem.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC