sorting string array with bubble sort

Ok, well, the 1st line of code basically explains what it does ( 500 worst passwords). I'm doing a lab assignment for my programming class. We have only 1 class day to finish it, so this is due today (October 8, 2009). What I have here only took me about 2 hours, but I'm really stuck... it does everything exactly as it should, except in the output file it prints out:

1. 123456
2. 0
3. 0
. . .
. . .
. . .
498. zxcvbn
499. zxcvbnm
500. zzzzzz
1.
2.
3.
. . .
. . .
. . .
498.
499.
500.



What I can't figure out is why it prints the numbers 1-500 again, but with no passwords after each number, when it should have only printed each number from 1-500 only once. I used DEV C++ to compile. Here's a link to the txt file I used in case anyone wants to test it. http://www2.fairmontstate.edu/users/dtobin/comp108/passwords.txt

I would really appreciate any help I can get.


Here's my code:

//This program takes 500 passwords from a text file, stores each password as an element in an array, and sorts the array into ascending order according to ASCII values.
#include <iostream>
#include <fstream>
#include<string>
#include<iomanip>
using namespace std;

int main()
{
ifstream infile;//input file name
ofstream outfile;//output file name
string passwordarr[500];//array to hold each password
string password, temp;
int i, j, a;

cout << "Program is now opening 'passwordsin.txt'" << endl << endl;
infile.open( "passwordsin.txt" );//opens passwordsin.txt

if(!infile)
{
cerr<<"ERROR 001 OCCURRED WHEN ATTEMPTING TO OPEN PASSWORDSIN.TXT\n\n"<<endl;//shows error if passwordsin.txt does not open
exit(001);
}//end if

cout << "Program is now opening 'passwordsout.txt'" << endl << endl;
outfile.open("passwordsout.txt");//opens passwordsout.txt
if(!outfile)
{
cerr<<"ERROR 003 OCCURRED WHEN ATTEMPTING TO OPEN PASSWORDSOUT.TXT\n\n"<<endl;//displays error message if passwordsout.txt does not open
exit(002);
}//end if

cout << "Program is now storing each password in an element of the array, \n sorting them into ascending order, \n and printing them to passwordsout.txt" << endl << endl;
while ( !infile.eof() ) //while not at end of file
{
for ( i = 0; (i < 500); i++ )
{
getline ( infile,password );
passwordarr[i] = password;
}//end for
for ( j = 0; j < 498; j++ )
{
for ( a = 1; a < (500 - ( j + 1 ) ); a++)
{
if ( passwordarr[ 500 - a ] < passwordarr[ 500 - ( a + 1 ) ] )
{
temp = passwordarr[ 500 - a ];
passwordarr[ 500 - a ] = passwordarr[ 500 - ( a + 1 ) ];
passwordarr[ 500 - ( a + 1 ) ] = temp;
}//end if
}//end for
}//end for
for ( i = 0; i < 500; i++)
{
outfile << i+1 << ". " <<passwordarr[i] << endl;
}//end for
}//end while

cout << "Program is now closing 'passwordsin.txt'" << endl << endl;
infile.close();//closes passwordsin.txt

cout << "Program is now closing 'passwordsout.txt'" << endl << endl;
outfile.close();//closes passwordsout.txt

cout << "Program has completed all tasks!!" << endl << " Please open the file 'passwordsout.txt' for results." << endl;

system("PAUSE");
return 0;

}// end main
This is because of the 'while' loop. In the first iteration of this loop, your program reads 500 lines from the input file. Since the file contains those 500 lines, EOF will not occur yet, therefore the second iteration of the loop will be executed. Remember that EOF is set when you attempt to read data which does not exist in a file.

If you can assume that the input file contains exactly 500 lines, it is best to just remove the 'while' loop.
Last edited on
Thanks... works fine.
Topic archived. No new replies allowed.