I will arrange my output in well-alligned columns, but I don't think I should use 3D char arrays since my teacher just wants us to stick with what we've learned so far.

Check out my code. I'm happy about it, but I just can't get the alignment right.

//File: lab9.cpp
/*Purpose: to write a program that should read in four words
at a time, each word being no longer than 9 characters. It will
print out each set of words on a separate line, in alphabetical 
order. After the last set of words has been processed, the program
is to print out: 
	a) number of sets processed
	b) number of sets that were in alphabetical order to start with 
		and needed no rearranging*/

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <conio.h>
#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; //ins as input stream
const int limit = 10;
int set = 0, INORDER;

void main()
{
	char word1[limit],word2[limit],word3[limit],word4[limit];
	char next_char;
	
	ins.open(in_file);
	outs.open(out_file);
	//input original data
	while(!ins.eof())
	{
		INORDER = set;
		in_string(word1,word2,word3,word4);
		sort_string(word1,word2,word3,word4);
		output_string(word1,word2,word3,word4);
		cout << endl;
		ins.get(next_char);
		set++;
	}
	cout << "\nNUMBER OF CARDS = " << set;
	cout << "\nNUMBER IN ORDER = " << INORDER;

	getch();
	ins.close();
}

void in_string(char st1[],char st2[],char st3[],char st4[])
{
	ins >> st1;
	ins >> st2;
	ins >> st3;
	ins >> st4;
}

void sort_string(char a[],char b[],char c[],char d[])
{
	//first pass - putting largest in its place
	if(strcmp(a,b) > 0) exchange(a,b);
	else INORDER--;
	if(strcmp(b,c) > 0) exchange(b,c);
	else INORDER--;
	if(strcmp(c,d) > 0) exchange(c,d);
	
	//second pass - putting second largest in its place
	if(strcmp(a,b) > 0) exchange(a,b);
	else INORDER--;
	if(strcmp(b,c) > 0) exchange(b,c);

	//third pass - putting third largest in its place
	if(strcmp(a,b) > 0) exchange(a,b);
	else INORDER--;
}

void exchange(char A[],char B[])
{
	char temp[limit];
	strcpy(temp,A);
	strcpy(A,B);
	strcpy(B,temp);
}


void output_string(char w1[],char w2[],char w3[],char w4[])
{
	cout << w1 << " " << setw(8); 
	cout << w2 << setw(8)<< " ";
	cout << w3 << setw(8)<< " ";
	cout << w4 << setw(8)<< " ";
}

This is what's in my file "data.txt"

SALESMAN MONTH PROGRAM WRITE
SEE THE BLUE SKY
CAT THROUGH A RAN
THE TOP TUMBLED TO
WHENEVER WATER WAS WHITE
A BLUE CLEAR SKY


This is the output it provides:
MONTH PROGRAM SALESMAN WRITE
BLUE SEE SKY THE
A CAT RAN THROUGH
WAS WATER WHENEVER WHITE
A BLUE CLEAR SKY

NUMBER OF CARDS = 6
NUMBER IN ORDER = 1

As you guessed, the perfect output should be aligned. But my program can't do that yet. Could anyone help me? The code is ok. It just needs some tweaking so that it can display the aligned words. As I said, maybe it's not good for a newbie to have 3D char arrays. Thank you so much.

You need to take a look into I/O manipulators. You'll need 3 in all - setw(int) is one of them. Have a bash at it first and let me know.

Try changing your output from cout << w1 << " " << setw(8); to cout << setw(8) << w1 << " ";

Following WaltP's example:
another possible format is:
cout << left << setw(10) << w1;

Ok. mistake in my other with 3!

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