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"
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);
	//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(4); 
	cout << setw(4) << w2 << " ";
	cout << setw(4) << w3 << " ";
	cout << setw(4) << w4 << " ";
}

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.

Recommended Answers

All 5 Replies

setw() tells cout the minimum width of a column. When the actual text is longer than that cout will just ignore the setw() value. So make the columns wide enough to hold the longest line plus a few spaces for best looking product. Also use left to left-justify the text in the space.
Try this simple example and see how it looks:

#include<iostream>
#include <iomanip>
using namespace std;

int main()
{
cout << left << setw(20) << "Hello" << "World\n";
}

setw() tells cout the minimum width of a column. When the actual text is longer than that cout will just ignore the setw() value. So make the columns wide enough to hold the longest line plus a few spaces for best looking product. Also use left to left-justify the text in the space.
Try this simple example and see how it looks:

#include<iostream>
#include <iomanip>
using namespace std;

int main()
{
cout << left << setw(20) << "Hello" << "World\n";
}

Thank you for your suggestion! The program works well now. Silly me, I've neglected the setw() rule. Thank you again! Cheers!

Hello,

I am really knew in Borland C++ Builder 6. ( 2 days now )

What I want to do is take an old DOS program (made in Turbo C), that exported the results in a .txt file with a certain alignment and spacing, and port it in BCB6.

I tried using a TMemo and a TRichEditBox in order to keep the alignment and spacing as in the plain text .. but no luck ..
It always looks as the file opened in Wordpad.

Example:

in notepad


ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄ¿
Buffer 1 ³ TCAT1 ³ ACC2N ³ RAM2N ³ UM/US ³ RANG2 ³ RANG1 ³ MPM ³ MPC ³
ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄ´

It shows in columns, very nice .. it is ok.

But in Wordpad, the columns are ruined ..

Hope I was able to make myself understood.
And I hope you and help me.

Thanks in advance!

As the text editor on this site is in plain text, but the post in not :)
I am writing again the text as I want it to look, in CODE

The one in the previous post looks like in Wordpad, as I don't want it to look like.

So, in Notepad is OK and looks like

           ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄ¿
 Buffer 1  ³ TCAT1 ³ ACC2N ³ RAM2N ³ UM/US ³ RANG2 ³ RANG1 ³  MPM  ³  MPC  ³
           ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄ´

While in BCB6 looks like

ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄ¿
Buffer 1 ³ TCAT1 ³ ACC2N ³ RAM2N ³ UM/US ³ RANG2 ³ RANG1 ³ MPM ³ MPC ³
ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄ´

Since I've been on this all day long, i've figured it out

For who ever needs my solution .. here it is:

Set the text Font to anything that loks like a console .. Lucinda Console in my case :)

Good luck coding!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.