infile into array and sort

Thread Solved
Reply

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

infile into array and sort

 
0
  #1
Sep 22nd, 2007
The following is my "inFile":
  1. UEAALEUE
  2. RIXETOHI
  3. BACOZOEI
  4. DIUEAOAU
  5. ZIKOFIMO
  6. RESUZEAE
  7. IIJEHUMO
  8. EEQINOXO
  9. TAOEKIBI
  10. RUQICIGU
  11. ZIWAREWE
  12. HIDIFIFO
  13. MOIACALA
  14. MEJIEACO
  15. UAPEUUQU
  16. YISOZAZO
  17. KENIDONA
  18. EEVIYOHU
  19. SEJEXEYA
  20. OESEQUFU
  21. NUJECAUE
  22. RAKAWUME
  23. NISIMAKE
  24. MEPIBADO
  25. TAUELAZI
  26. IIEACINU
  27. IITOGIIE
  28. GIEIFEVU
  29. NIOUQELU
  30. IIVOFEHI
  31. TOKUCORU
  32. GUQOEOJE
  33. HOBUUIXA
  34. HOVEDAGO
  35. JEROMAFA
  36. TOLIOELU
  37. QIGOIIAA
  38. TEDEIUOA
  39. COZOLELA
  40. YOHUZUZA
  41. QUNACAKO
  42. POSAEIGA
  43. JECAMOPE
  44. VAKEKAPA
  45. LEJEVINE
  46. SONEBAEE
  47. PEBINUGO
  48. KONUNINE
  49. BERIWUQO
  50. WEXOIOVU
  51. HIXUQIAE
  52. IODOAUIE
  53. HAKESUFA
  54. ZIAIWAXA
  55. DAGOGEBE
  56. CUHIUAOO
  57. DAVUAETU
  58. COAUBAKU
  59. IIMOJOLE
  60. UIKIBUWU
  61. SOEAOILA
  62. VEGUKAXA
  63. AUMATOUO
  64. JOSUZIXI
  65. COUOFUWI
  66. ZILUAISU
  67. SEZEIAVO
  68. DEFISIZA
  69. GOSUEUFO
  70. ZEEAMAIA
  71. POROEIDI
  72. REGEJUPO
  73. YUZUAOXU
  74. AUZUXANI
  75. SIZUZIEO
  76. KIBEROWO
  77. WEGIEAFA
  78. PIPAAURI
  79. UEJIEAZE
  80. VIPIVICE
  81. CASISUIO
  82. FENOBOFU
  83. GULEUEKO
  84. NEUIGIJU
  85. LICOXUDO
  86. EUZAIAUA
  87. LOUIOARU
  88. DOKOWUUE
  89. NEIIUEQO
  90. FORUMOQU
  91. KIWOOUXO
  92. ZEMAFAIO
  93. NAJEOAQI
  94. YOIIBUFI
  95. QOUOFIPE
  96. EAOAPIMO
  97. FAQALOLE
  98. XEGIJELE
  99. WOQEJAPO
  100. XEOETUGE
  101. BAEIUITA
  102. DIHODETI
  103. PUFIYARA
  104. OEVINAYU
  105. PIUOCOXI
  106. YIBIZEOI
  107. EADIGAFO
  108. EUFENABU
  109. HILAKOYE
  110. DITAJARI
  111. WEEIOOZA
  112. TIAUSEAO
  113. ZOVUROPA
  114. PAQAWIRU
  115. KUBOGESE
  116. ZIZUAEOI
  117. RAXUHAFU
The question is: Write a program that will read an unknown number of single names (of jumbled letters) into an array from a file named “listnames.txt”. There are no more than 120 names in the list. Sort the names in normal alphabetical order and then display them to the screen and print printer in 5 columns.
Any assistance is appreciated...The following (below) is what I have so far....but I want to make sure.....a couple things:
1st. The program keeps repeating the the groups of letters on the first line..why does it not read in the data from each line?
2nd. For right now..the program won't run....is it because of how I have my string 'names' declared? I pretty much want the program to read from the 'inFile' into an array..is my declaration ok?
3. I'm kinna familiar with BubbleSort...but only for numeric values... how can I sort the names in normal alphabetical order.
  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. inFile>>names;
  25.  
  26.  
  27. int num;
  28.  
  29. for (num=0; num<120; num++)
  30.  
  31. outFile<<names<<"Hello"<<endl;
  32.  
  33. int i;
  34. for (i=0; i<120; i++)
  35.  
  36. {
  37. if (i%5 ==0)
  38. cout<<endl;
  39. cout<<setw(10)<<names[120];
  40. }
  41.  
  42.  
  43.  
  44. inFile.close();
  45. outFile.close();
  46.  
  47. return 0;
  48. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
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: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: infile into array and sort

 
0
  #2
Sep 22nd, 2007
Perhaps output names[ i ], not names[ 120 ]

You also need a loop to input them as well, you can't just read the whole lot like that.
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
  #3
Sep 22nd, 2007
Thanks much Salem....waht about the sorting of the names in normal alphabetical order?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
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: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: infile into array and sort

 
0
  #4
Sep 22nd, 2007
Dunno - are you allowed to use the library sort, or are you expected to implement it yourself?
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
  #5
Sep 22nd, 2007
Our professor simply wnet over the Bubble-sort, but he didn't specify what kinna sorts we were suppose to use..To be honest...I always thought Bubble sorts were for numbers...because you could always compare the values...but I have no clue about how to do it for letters. Thanks for you input.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
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: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: infile into array and sort

 
0
  #6
Sep 22nd, 2007
You can use any sort on any data which has the < operator defined.

Can you do for example
names[ i ] < names[ i + 1 ]

If you can, you can sort them.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
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: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: infile into array and sort

 
0
  #7
Sep 22nd, 2007
>.waht about the sorting of the names in normal alphabetical order?

[search]c++ bubble sort[/search]
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
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: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: infile into array and sort

 
0
  #8
Sep 22nd, 2007
>but I have no clue about how to do it for letters

string a ="apple";
string b ="banana";

if (a > b )
{
//do stuff
}
*Voted best profile in the world*
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
  #9
Sep 22nd, 2007
string a ="apple";
string b ="banana";

if (a > b )
{
//do stuff
}
so considering the fact, that each line in the inFile contains all leters of the alphabeth, at some point or the other....i'd have to use a lot of if-else statements? For example... if (x>y)...all the way down?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,152
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: 1436
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: infile into array and sort

 
0
  #10
Sep 23rd, 2007
>>so considering the fact, that each line in the inFile contains all leters of the alphabeth, at some point or the other....i'd have to use a lot of if-else statements? For example... if (x>y)...all the way down?

Naw -- it is not that hard. What jamthwee posted was just an example to show you how to compare two strings. Since you have them in an array you can use a loop to run through them exactly like you would sort an array of integers. Like this bubble sort algorithm -- there are other more efficient sort algorithms, but this is ok for small arrays.
  1. string names[25];
  2. for(int i = 0; i < 24; i++)
  3. {
  4. for(int j = i+1; j < 25; j++)
  5. {
  6. if( names[i] > names[j])
  7. {
  8. string temp = names[i];
  9. names[i] = names[j];
  10. names[j] = temp;
  11. }
  12. }
  13. }
Last edited by Ancient Dragon; Sep 23rd, 2007 at 2:36 am.
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  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC