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

I don's see you doing anything to test if a set of inputs is in order to begin with. Before you sort the strings, check their order, using code similar to the first block in your sort, setting a flag if an out of order pair is seen.

1.
Where in your code are you actually checking if "a set was in alphabetical order to start with, and needed no rearranging."??
Currently your code is

ins.open("data.txt");

// input original data.
while(!ins.eof())
{
	INORDER = set; // <<--
	...
	set++; // <<--

}
...
cout << "NUMBER OF CARDS = " << set << endl;
cout << "NUMBER IN ORDER = " << INORDER << endl;

With this you can not expect set and INORDER to have different values. Also I think value of set would be one more than the actual number of sets.

2.
This code is NOT C++ code. It's C in C++.

This article has been dead for over six months. Start a new discussion instead.