The following is my "inFile":

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

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.

#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;
}

Recommended Answers

All 17 Replies

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.

Thanks much Salem....waht about the sorting of the names in normal alphabetical order?

Dunno - are you allowed to use the library sort, or are you expected to implement it yourself?

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.

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.

Member Avatar for iamthwee

>.waht about the sorting of the names in normal alphabetical order?

[search]c++ bubble sort[/search]

Member Avatar for iamthwee

>but I have no clue about how to do it for letters

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

if (a > b )
{
//do stuff
}

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?

>>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.

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;
        }
   }
}

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...

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:

#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];
	string name;
	int i, j, k;
	int count=120;
	while(getline(inFile,name))
	{
	for (i=0;i<count-1;i++)
	for (j=i+1;j<count; j++)
	if (name[i]>name[j])
	swap (name[i],name[j]);
	}
	for (k=0; k<120; k++)
	{
		if (i%5==0)
			cout<<endl;
		cout<<setw(2)<<name[i];
	}//end for i
	
	
		
	inFile.close();
	outFile.close(); 

	return 0;
}

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

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.

int count = 0;
while(getline(inFile,name))
{
    names[count] = name;
    ++count;
}

Thanks for you reply...we haven't reached checking for integer length, but i'll read up on it and see how it works.

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

int count = 0;
while(count < 120 && getline(inFile,name))
{
    names[count] = name;
    ++count;
}

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.

#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];
	string name;
	int i, j, k;
	int count=0;
	while(count < 120 && getline(inFile,name))
	{
    names[count] = name;
    ++count;
	}
	{
	for (i=0;i<count-1;i++)
	for (j=i+1;j<count; j++)
	if (names[i]>names[j])
	swap (names[i],names[j]);
	}
	for (k=0; k<120; k++)
	{
		if (i%5==0)
		cout<<endl;
		cout<<setw(10)<<names[i];
	}
				
	inFile.close();
	outFile.close(); 

	return 0;
}

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.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.