I have some code here that I've worked on that part of a class structure. I have to print these words out to the screen in a columnar fashion, 5 to a line and left justified.
The output to the console screen looks like this:
So, what am I doing wrong?
Todd
Code:
//*****************************************************************************
// read_words(): method Reads data in the file, Lab5TextData.txt, into the
// array and keeps track of (count) the number of words read.
//-----------------------------------------------------------------------------
void WordArray::read_words (void)
{
string temp_word;
size = 0;
count = 0;
infile >> temp_word; //read into temporary word first
while (!infile.eof() && count < 500) //read no more than 500 words
{
size = temp_word.length(); //how many characters in temp_word
//if last char is punctuation; one_word’s array index is 0 through
//size-1 etc.)
if (temp_word[size-1] == '!' || temp_word[size-1] == ';' ||
temp_word[size-1] == ':' || temp_word[size-1] == ' ' ||
temp_word[size-1] == '.' || temp_word[size-1] == ',' ||
temp_word[size-1] == '?' || temp_word[size-1] == '\n')
temp_word[size-1] = '\0'; //replace the punctuation by NULL character
all_words[count] = temp_word; //store word read in array
count++;
// read next word in temporary location
infile >> temp_word; //next read
}
}//end read_words
//***************************************************************************
// print_words() method prints the words stored in the array 5 to a line.
// Print each word left justified columnar fashion and give an appropriate
// table heading. Also print the total # of words read from the file.
//***************************************************************************
void WordArray::print_words (char ch)
{
switch (ch)
{
case 'o': pg++;
cout << "\t\t\t\t\t\t\t\t Page " << pg << ".";
cout << "\n\t\t\t Report Number 1";
cout << "\n\t\t Original List of Words in Text File\n\n";
cout << "\t\t\t " << SEPARATOR << endl;
break;
case 's': pg++;
cout << "\n\n\t\t\t\t\t\t\t\t Page " << pg << ".";
cout << "\n\t\t\t Report Number 2";
cout << "\n\n\t\t Alphabetized List of Words Read\n\n";
cout << "\t\t\t By: " << NAME;
cout << endl << endl << "\t\t\t " << SEPARATOR << endl;
break;
} //end switch
for (int i = 0; i < count; i++)
{
if (i % 5 == 0)
cout << endl;
//cout << setw(5) << left << all_words [i];
cout << setw(7) << left << all_words [i] << "\t";
}
cout << endl << DASHES << endl;
cout << "\t\tTotal Number of Words = " << count << "." << endl << endl;
} // end print_words()
The output to the console screen looks like this:
Code:
Word Array Processing Program
------------------------------------------------------------------------------
by
Programmer Name: Todd McNeil
Section: CSI 155/Section 875
Lab/Project No.: Lab #5
File: Lab5.cpp
Due Date: 04/05/2006
Turned in On: 04/06/2006
Instructor: Dr. Raj Gill
Penalty: 10%
Compiler Used: Visual Studio.Net 2003
------------------------------------------------------------------------------
Page 1.
Report Number 1
Original List of Words in Text File
-------------------
The steps required in problem
solving are an indication that
computer science involves more than
simply programming Computer science is
a mathematical scientific and engineering
discipline with three major working
methodologies theory abstraction and design
This text begins your exploration
of this rich discipline with
a discussion of practical tools
computers and software Although you
will spend much time working
with these tools their mastery
is merely a means to
an end not the ultimate
objective of this text This
chapter begins with a discussion
that will continue throughtout the
text How can we effectively
solve problems using the computers
as a tool Problem solving
involves careful analysis to discover
the nature of the problem
Problem solving also includes the
development of a logical process
or design of a solution
before the computer is approached
Once a solution is implemented
on a computer good problem
solving requires that we fully
test our work To gain
an appreciation of the computing
environment this chapter examines a
model of a computer system
and the steps required for
execution software on that computer
Several simple programs illustrate the
implementation of a problem solution
design in the computer language
C++ The top-down design method
of problem solving and computer
program writing is tied to
a feature of many programming
languages the function
------------------------------------------------------------------------------
Total Number of Words = 218.
****************************Goodbye for Now********************************
Press any letter or digit key to exit:
So, what am I doing wrong?
Todd