Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing output from word array to screen

Status
Not open for further replies.

tmcneil

Technical User
Nov 17, 2000
294
US
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.

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
 
> So, what am I doing wrong?
Looking good so far - all you need to do now is implement a sort function when you do
[tt] case 's':[/tt]

--
 
I knew that posting that output would confuse posters. Well, I'm firther along and fixed the 5 to a line problem. I had to remove the ending "\t" character which was pushing the word output to the next line. Anyways, I still have to figure out the frequency of each word in the file and I'm trying to use the one I wrote for numbers and replacing that array with the one I created for words. I keep getting this compile error though:

Code:
Words.cpp(183) : error C2677: binary '[' : no global operator found which takes type 'std::string' (or there is no acceptable conversion)

I get a few others, but I think if I fix this one, I can get the rest. here's what I have so far:
Code:
void WordArray::read_words (void)
{    
    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\n\t\t      Original List of Words in Text File\n\n";
                  cout << "\t\t\t     " << SEPARATOR << endl;
                  break;
       case 's':  pg++;
                  cout << "\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 in Text File\n\n";
                  cout << "\t\t\t       By: " << NAME;
                  cout << endl << endl << "\t\t\t     " << SEPARATOR << endl;
                  break;
       case 'l':  pg++;
                  cout << "\n\t\t\t\t\t\t\t\t       Page " << pg << ".";
                  cout << "\n\t\t\t       Report Number 3A";
                  cout << "\n\n\t\t     Alphabetized Words All in Lower Case\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 << left << setw(15) << all_words [i];
    }

    cout << endl << DASHES << endl;
    cout << "\t\tTotal Number of Words = " << count <<  "." << endl << endl;
}  // end print_words()

//***************************************************************************
// sort_words() method sorts all_words in alphabetical order.
//***************************************************************************
void WordArray::sort_words (void)
{
    for (int i = 0; i < count; i++)
        for (int j = i + 1; j < count; j++)
            if (all_words[i] > all_words[j])         // if names are out of order
            {    
                temp_word = all_words[i];    // switch them
                all_words[i] = all_words[j];
                all_words[j] = temp_word;
            } 
}  // end sort_words()

//***************************************************************************
// convert_to_lower_case() method converts all upper case to lower case in
// alphabetized word array
//***************************************************************************
void WordArray::convert_to_lower_case (void)
{
    for (int i = 0; i < count; i++)
    {
        temp_word = all_words[i];
        size = temp_word.length();   //get size/length
        for (int j = 0; j < size; j++)
            if (isupper (temp_word[j]))      //check for uppercase
            {   
                temp_word = tolower(temp_word[j]);
                all_words[i] = temp_word;
            }
    }

}  // end convert_to_lower_case()

//****************************************************************************
// compute_occurences () method computes the frequency of each word in
// all_words array and prints the Unique Word #, Unique Word and Occurences
// of each word found.  Gives an appropriate table heading. Also print the
// total # of words in the array. Number the columns and lines.
//****************************************************************************

void WordArray::compute_occurences (void)
{
    string occur_words[500];
    int i, k;
    int l_no = 1;
    pg++;
 
    //  Initialize frequency array
    for (i = 0; i < 500; i++)
        occur_words[i] = "";
 
    //  Compute frequency distribution
    for (i = 0; i < count; i++)
        occur_words[all_words[i]]++; //error occurs here
 
    //  Output heading   
    cout << "\n\n\t\t\t\t\t\t\t\t Page No." << pg << endl;
    cout << "\n\t\t\t       Report Number 3B";
    cout << "\n\n\t\t    Unique Words and their Occurrences\n\n";
    cout << "\t\t\t      By: " << NAME;
    cout << endl << endl << "\t\t\t      " << SEPARATOR << endl;
    cout << DASHES << endl;
    cout << setw(25) << "Unique Word #" << setw(15) << "Unique Word" << setw(15)
         << "Occurences" << endl;
    cout << DASHES << endl;
 
    //  Output frequency distribution
    for (i = 0, k = 1; i < 500; i++)
    {
        //  Only process values found in distribution...
        if (occur_words[i] > 0)
        {
            cout << setw(23) << l_no++ << "." << setw(13) << i << setw(15)
                 << occur_words[i];
            k++;
        }
    }

    cout << endl << DASHES << endl;
    cout << "\t\tTotal Number of Words = " << count <<  "." << endl << endl;
    cout << endl << DASHES << endl;
} // end compute_occurences

Here's my output:
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.


                                                                       Page 2.
                               Report Number 2

                    Alphabetized List of Words in Text File

                               By: Todd McNeil

                             -------------------

Although       C++            Computer       How            Once
Problem        Problem        Several        The            The
This           This           To             a              a
a              a              a              a              a
a              a              a              a              a
a              abstraction    also           an             an
an             analysis       and            and            and
and            and            appreciation   approached     are
as             before         begins         begins         can
careful        chapter        chapter        computer       computer
computer       computer       computer       computer       computer
computers      computers      computing      continue       design
design         design         design         development    discipline
discipline     discover       discussion     discussion     effectively
end            engineering    environment    examines       execution
exploration    feature        for            fully          function
gain           good           illustrate     implementation implemented
in             in             includes       indication     involves
involves       is             is             is             is
is             language       languages      logical        major
many           mastery        mathematical   means          merely
method         methodologies  model          more           much
nature         not            objective      of             of
of             of             of             of             of
of             of             of             of             on
on             or             our            practical      problem
problem        problem        problem        problem        problems
process        program        programming    programming    programs
required       required       requires       rich           science
science        scientific     simple         simply         software
software       solution       solution       solution       solve
solving        solving        solving        solving        solving
spend          steps          steps          system         test
text           text           text           than           that
that           that           that           the            the
the            the            the            the            the
the            the            the            the            the
their          theory         these          this           this
this           three          throughtout    tied           time
to             to             to             tool           tools
tools          top-down       ultimate       using          we
we             will           will           with           with
with           with           work           working        working
writing        you            your
------------------------------------------------------------------------------
                Total Number of Words = 218.


                                                                       Page 3.
                               Report Number 3A

                     Alphabetized Words All in Lower Case

                               By: Todd McNeil

                             -------------------

a              c              c              h              o
p              p              s              t              t
t              t              t              a              a
a              a              a              a              a
a              a              a              a              a
a              abstraction    also           an             an
an             analysis       and            and            and
and            and            appreciation   approached     are
as             before         begins         begins         can
careful        chapter        chapter        computer       computer
computer       computer       computer       computer       computer
computers      computers      computing      continue       design
design         design         design         development    discipline
discipline     discover       discussion     discussion     effectively
end            engineering    environment    examines       execution
exploration    feature        for            fully          function
gain           good           illustrate     implementation implemented
in             in             includes       indication     involves
involves       is             is             is             is
is             language       languages      logical        major
many           mastery        mathematical   means          merely
method         methodologies  model          more           much
nature         not            objective      of             of
of             of             of             of             of
of             of             of             of             on
on             or             our            practical      problem
problem        problem        problem        problem        problems
process        program        programming    programming    programs
required       required       requires       rich           science
science        scientific     simple         simply         software
software       solution       solution       solution       solve
solving        solving        solving        solving        solving
spend          steps          steps          system         test
text           text           text           than           that
that           that           that           the            the
the            the            the            the            the
the            the            the            the            the
their          theory         these          this           this
this           three          throughtout    tied           time
to             to             to             tool           tools
tools          top-down       ultimate       using          we
we             will           will           with           with
with           with           work           working        working
writing        you            your
------------------------------------------------------------------------------
                Total Number of Words = 218.



****************************Goodbye for Now********************************


Press any letter or digit key to exit:

it's the compute_occurences that is giving me the headache. Any ideas?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top