How do you sort names alphabetically using functions in C++ from file?
After the last set of words has been processed the program is to print out
a) the number of sets processed
b) the number of sets that were in alphabetical order to start with,
and needed no rearranging.
this is the program but the sort names function does not show the correct INORDER.
The INORDER should be 3, my program showing 7.
please help.
thanks
#include <iostream>
#include <string>
#include <conio.h>
#include <fstream>
#include <iomanip>
#define in_file "data.txt"
#define out_file "result.txt"
using namespace std;
void in_string(char[], char[], char[], char[]);
void sort_string (char [], char [], char [], char []);
void exchange(char [], char []);
void output_string(char [], char [], char [], char []);
ifstream ins; // global declaration
const int limit = 10;
void main()
{
int set = 0, INORDER;
char string1[limit], string2[limit], string3[limit], string4[limit];
char next_char;
ins.open("data.txt");
// input original data.
while(!ins.eof())
{
INORDER = set;
in_string(string1, string2, string3, string4);
sort_string(string1, string2, string3, string4);
// output sorted data.
output_string(string1, string2, string3, string4);
cout << endl;
ins.get(next_char);
set++;
}
cout << endl;
cout << "NUMBER OF CARDS = " << set << endl;
cout << "NUMBER IN ORDER = " << INORDER << endl;
getch();
ins.close();
}
void in_string(char st1[], char st2[], char st3[], char st4[])
{
ins >> st1 >> st2 >> st3 >> st4;
}
void sort_string(char str1[], char str2[], char str3[], char str4[])
{
// first pass - putting largest in its place
if (strcmp(str1, str2) > 0)
exchange(str1, str2);
if (strcmp(str2, str3) > 0)
exchange(str2, str3);
if (strcmp(str3, str4) > 0)
exchange(str3, str4);
// second pass - putting second largest in its
// place
if (strcmp(str1, str2) > 0)
exchange(str1, str2);
if (strcmp(str2, str3) > 0)
exchange(str2, str3);
// third pass - putting third largest in its place
if (strcmp(str1, str2) > 0)
exchange(str1, str2);
void exchange(char str1[],char str2[])
{
char temp[limit];
strcpy(temp,str1);
strcpy(str1,str2);
strcpy(str2,temp);
}
void output_string(char s1[], char s2[], char s3[], char s4[])
{
cout << left << setw(9) << s1 << left << setw(9);
cout << s2 << left << setw(9);
cout << s3 << left << setw(9);
cout << s4 << left << setw(9);
}
the file input is
THE TOP TUMBLED TO
WHENEVER WATER WAS WHITE
A BLUE CLEAR SKY
ALICE BOB CALVIN JADE
JACK JILL JOHN JOHNNY