| | |
infile into array and sort
Thread Solved
![]() |
The following is my "inFile":
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.
C++ Syntax (Toggle Plain Text)
UEAALEUE RIXETOHI BACOZOEI DIUEAOAU ZIKOFIMO RESUZEAE IIJEHUMO EEQINOXO TAOEKIBI RUQICIGU ZIWAREWE HIDIFIFO MOIACALA MEJIEACO UAPEUUQU YISOZAZO KENIDONA EEVIYOHU SEJEXEYA OESEQUFU NUJECAUE RAKAWUME NISIMAKE MEPIBADO TAUELAZI IIEACINU IITOGIIE GIEIFEVU NIOUQELU IIVOFEHI TOKUCORU GUQOEOJE HOBUUIXA HOVEDAGO JEROMAFA TOLIOELU QIGOIIAA TEDEIUOA COZOLELA YOHUZUZA QUNACAKO POSAEIGA JECAMOPE VAKEKAPA LEJEVINE SONEBAEE PEBINUGO KONUNINE BERIWUQO WEXOIOVU HIXUQIAE IODOAUIE HAKESUFA ZIAIWAXA DAGOGEBE CUHIUAOO DAVUAETU COAUBAKU IIMOJOLE UIKIBUWU SOEAOILA VEGUKAXA AUMATOUO JOSUZIXI COUOFUWI ZILUAISU SEZEIAVO DEFISIZA GOSUEUFO ZEEAMAIA POROEIDI REGEJUPO YUZUAOXU AUZUXANI SIZUZIEO KIBEROWO WEGIEAFA PIPAAURI UEJIEAZE VIPIVICE CASISUIO FENOBOFU GULEUEKO NEUIGIJU LICOXUDO EUZAIAUA LOUIOARU DOKOWUUE NEIIUEQO FORUMOQU KIWOOUXO ZEMAFAIO NAJEOAQI YOIIBUFI QOUOFIPE EAOAPIMO FAQALOLE XEGIJELE WOQEJAPO XEOETUGE BAEIUITA DIHODETI PUFIYARA OEVINAYU PIUOCOXI YIBIZEOI EADIGAFO EUFENABU HILAKOYE DITAJARI WEEIOOZA TIAUSEAO ZOVUROPA PAQAWIRU KUBOGESE ZIZUAEOI RAXUHAFU
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.
C++ Syntax (Toggle Plain Text)
#include <cstring> #include <iostream> #include <iomanip> #include <cmath> #include <fstream> #include <string> #include <ctime> using namespace std; int main() { ifstream inFile; ofstream outFile; inFile.open ("listnames.txt"); outFile.open ("outnames"); string names[120]; inFile>>names; int num; for (num=0; num<120; num++) outFile<<names<<"Hello"<<endl; int i; for (i=0; i<120; i++) { if (i%5 ==0) cout<<endl; cout<<setw(10)<<names[120]; } inFile.close(); outFile.close(); return 0; }
>>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.
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.
C++ Syntax (Toggle Plain Text)
string names[25]; for(int i = 0; i < 24; i++) { for(int j = i+1; j < 25; j++) { if( names[i] > names[j]) { string temp = names[i]; names[i] = names[j]; names[j] = temp; } } }
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.
![]() |
Similar Threads
- Sort A Multidimension Array (Python)
- Can't get array to sort. (Pascal and Delphi)
- Multidimensional array sort problem (C++)
- Could someone please help me with a bucket sort. (C)
- Input Output File Streams - Sorting Help With Array (C++)
- Working with array of files (C++)
- Cannot utilize an array properly (Java)
Other Threads in the C++ Forum
- Previous Thread: hi sir/ma'am canu help me??
- Next Thread: ":" other than inheritance?
| Thread Tools | Search this Thread |
action api array auto based beginner binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count createcopyofanyfileinc delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input insert int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node noob output parameter pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets






