This program runs fine but I am getting funny looking characters.
It's just really one, it's at the botton of the screen. You should see it once u run for yourself.
I don't know how it gets there or why but It would be GREATLY APPRECIATED IF SOMEONE COULD HELP ME OUT WITH THIS ONE.
CAN"T THANK-YOU ENOUGH.
This is infact an assignment due in a couple of days.
/* By: Huynh Pham
Date: 07/03/02
File: ass57q2.cpp
Uses the selection sort algorithm to sort an unsorted list of 10 names and telephone numbers into ascending alphabetic order
*/
#include <iostream.h>
#include <string.h>
#include <fstream.h>
#define size 10
class List_class
{
private:
char names[size][15], numbers[size][15];
public:
List_class();
void to_screen();
void selection_sort();
void to_file();
};
//-----------------------------------------------------------------------
void main(void)
{
List_class names_list;
cout << "This is the list before it was sorted" << endl;
names_list.to_screen();
names_list.selection_sort();
cout << endl << endl << "This is the list after it was sorted" << endl;
names_list.to_screen();
names_list.to_file();
} // main
//-----------------------------------------------------------------------
List_class::List_class()
// reads the list in from a file
{
fstream infile ("a57q1.txt", ios::in);
for (int a = 1; a <= size; a++)
{
infile >> names[a] >> numbers[a];
} // for
infile.close();
} // constructor
//-----------------------------------------------------------------------
void List_class::to_screen()
// displays the list to the screen
{
for (int a = 1; a <= size; a++)
{
cout << names[a] << " " << numbers[a] << endl;
} // for
} // show_list
//-----------------------------------------------------------------------
void List_class::to_file()
// writes sorted list to text file: a57q3.txt
{
fstream outfile ("a57q3a.txt", ios:ut);
for (int a = 1; a <= size; a++)
{
outfile << names[a] << " " << numbers[a] << endl;
} // for
outfile.close();
} // constructor
//-----------------------------------------------------------------------
void List_class::selection_sort()
//sorts an array into alphabetic order
{
int loc_of_min;
char min_names[size], min_num[size], temp_names[size], temp_num[size];
//The first element in the array is set to be the minimum.
for (int y = 1; y <= size; y++)
{
strcpy(min_names, names[y]);
strcpy(min_num, numbers[y]);
loc_of_min = y;
//The rest of the element is compared to the
minimum.
for (int z = y + 1; z <= size; z++)
{
//If there is an element smaller
then the minimum element then set
//this to the minimum.
if (strcmp(names[z], min_names) < 0)
{
strcpy(min_names, names[z]);
strcpy(min_num, numbers[z]);
loc_of_min = z;
} //if
} //for
//Swap the minimum element with the new
minimum. Numbers corresponding
//to the name is also swap to stop numbers
pairing up with other names
//other than their original.
if (strcmp(min_names, names[y]) < 0)
{
strcpy(temp_names, names[y]);
strcpy(temp_num, numbers[y]);
strcpy(names[y], min_names);
strcpy(numbers[y], min_num);
strcpy(names[loc_of_min],
temp_names);
strcpy(numbers[loc_of_min],
temp_num);
} //if
} //for
} // selection sort
*************TEXT FILE LOOKS LIKE THIS*****************
Ren 999666
Nidhish 111222
Nadeem 888222
Mark 789001
Sharif 456789
Jalna 987654
Miwako 000111
Misha 007007
Jayantha 123456
Diana 111555