HELP HELP HELP ME PLEASE!! 
Author Message
 HELP HELP HELP ME PLEASE!!

Quote:

> I am in the process of taking a Visual C++ class.  As an assignment, we have to
> sort strings.  I'm having difficulty doing this.  If anyone can help, that
> would be greatly appreciated.
> Justin


be better newsgroups to ask in, since comp.lang.c is about the C
language, not C++ or Windows.

<code cut>

--

I believe we can change anything.
I believe in my dream.
    - Joe Satriani



Tue, 03 Oct 2000 03:00:00 GMT  
 HELP HELP HELP ME PLEASE!!

I am in the process of taking a visual c++ class.  As an assignment, we have to
sort strings.  I'm having difficulty doing this.  If anyone can help, that
would be greatly appreciated.
Justin

this assignment was given with this code.  It will read in a text file and list
the frequency of each occurance of each word.  It should sort as the file is
being read or built.

# include <iostream.h>
# include <fstream.h>
# include <iomanip.h>
# include <ctype.h>
# include <stdlib.h>
# include <string.h>

void LowerCase(char * );
int FillWords(char * , char *[], int[]);
bool IsDuplicate(char * [], char *, int ,  int &);
void PrintWords(char *[], int, char * );
void PrintFrequencies(char *[], int *, int, char *);
int FindPlace(char *[], char *, int, bool &);

// One Global maximum number of words.
const int MaxWords = 500;
void main()
{
        // Local Variable Declarations
        char * infile = "words.txt";
        char * outfile = "wordout.txt";
        char * TheWords[MaxWords] = {0};
        int FrequencyTable[MaxWords] =  {0};
        int wordcount = 0;

        wordcount = FillWords(infile, TheWords, FrequencyTable);
        PrintWords(TheWords, wordcount, outfile);
        PrintFrequencies(TheWords, FrequencyTable, wordcount, outfile);

Quote:
}

bool IsDuplicate(char * WordList[], char * Word, int wordcount, int & where)
{
        for (int i=0; i< wordcount; i++)
        {       if (strcmp(WordList[i], Word) == 0)
                {       where = i;      return true;}
        }
        return false;

Quote:
}

void LowerCase(char * Word)
{
        // Pre-condition:       alphanumeric characters in "Word"
        // Post condition: lowercase alphabetical characters in word.
        // For the length of the word, change case.
        int SizeOfWord = strlen(Word);

        for (int i=0; i<SizeOfWord ; i++)
                Word[i] = tolower(Word[i]);
        return;

Quote:
}

int FindPlace(char * WordList[], char * Word, int OldCount, bool & exists)
{
        int where = 0;
        exists = IsDuplicate(WordList, Word, OldCount, where);

        // If the word exists, return where
        if (exists) return where;
        // Else return the number of words = poistion of new word.
        return OldCount;

Quote:
}

int FillWords(char * datafile, char * WordList[], int Freq[])
{
        int Position  = 0;
        char nextword[100];
        int WordLength;
        bool duplicate = false;
        int wordcount = 0;

        ifstream fin(datafile);
        if (fin)
        {
                while ((!fin.eof()) && (wordcount < MaxWords ))
                {
                        // Clear nextword and get the next word from the file.
                        *nextword = 0;
                        fin >> nextword;

                        // if the word has a trailing punctuation mark, remove it.
                        WordLength = strlen(nextword);
                        if (ispunct(nextword[WordLength-1]) )
                                nextword[WordLength-1] = '\0';

                        if(isalnum(nextword[0]))
                        {      
                                // Call Lowercase only if nextword is alphanumeric
                                LowerCase(nextword);

                                // find the position of existing word, or where it should go.
                                Position = FindPlace(WordList, nextword, wordcount, duplicate);
                                if (duplicate)
                                        // If the word exists, update frequency.
                                        Freq[Position]++;
                                else
                                {
                                        // Create new space and write the word in the strign array
Wordlist[position].
                                        // Update frequency and wordcount.
                                        WordList[Position] = new char[strlen(nextword)];
                                        strcpy(WordList[Position], nextword);
                                        Freq[Position]++;
                                        wordcount++;
                                }
                        }
                }
        }
        else
                // Come here only if there is a file I/O error.
                cout << "Data File could not be opened" << endl;

        cout << "\n \n" << wordcount << "  Words have been added \n";
        return wordcount;

Quote:
}

void PrintWords( char * WordList[], int wordcount, char * outputfile)
{
        ofstream fout(outputfile, ios::out);

        for (int i=0; i< wordcount; i++)
        {      
                if ((i % 10) == 0) fout << endl;
                fout << WordList[i] <<",  ";
        }

        fout << "\n \n \n********************\n \n";

Quote:
}

void PrintFrequencies(char *WordList[] , int * Frequencies, int wordcount, char
* outputfile)
{
        ofstream fout(outputfile, ios::app);
        for (int i=0; i< wordcount; i++)
        {
                fout.setf(ios::left|ios::fixed);fout << setw(20) << WordList[i] << "\t";
                fout.setf(ios::right|ios::fixed);fout<<setw(5)<< Frequencies[i] << endl;
        }
Quote:
}



Wed, 04 Oct 2000 03:00:00 GMT  
 HELP HELP HELP ME PLEASE!!


Quote:

>I am in the process of taking a visual c++ class.  As an assignment, we have to
>sort strings.  I'm having difficulty doing this.  If anyone can help, that
>would be greatly appreciated.
>Justin

I'm afraid you will have to ask your question in comp.lang.c++.

It's too bad really,  because your program doesn't seem to use C++ features
other than iostreams and // comments. It could almost be a C program.

I'd ask for my money back because what you are learning is not the kind
of object oriented C++ that is expected of C++ developers. :)



Wed, 04 Oct 2000 03:00:00 GMT  
 HELP HELP HELP ME PLEASE!!


:
:I am in the process of taking a visual c++ class.  As an assignment, we have
to
:sort strings.  I'm having difficulty doing this.  If anyone can help, that
:would be greatly appreciated.
:Justin

========
Justin, C++ is a different language from C.
There are newsgroups (comp.lang.c++ among them) for
discussions of C++.  I suggest you visit one of those
with your questions.  BTW, the headers you use in your
code suggest that you are using an old compiler.  You
might look into an upgrade.



Wed, 04 Oct 2000 03:00:00 GMT  
 HELP HELP HELP ME PLEASE!!



Quote:

>// One Global maximum number of words.

  This is not C.

Quote:
>void main()

  This is not legal C.

Quote:
>bool IsDuplicate(char * WordList[], char * Word, int wordcount, int & where)

  This is, in fact, C++.  Try next door in comp.lang.c++.

David G



Fri, 06 Oct 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!

2. Please help!!!!Please help!!!!Please help!!!!

3. SOMEONE HELP ME, HELP ME, HELP ME PLEASE...

4. HELP HELP HELP HELP HELP HELP!!!

5. NEED HELP WITH PRITING AN ARRAY, PLEASE PLEASE HELP

6. PLEASE PLEASE HELP HELP...question on interleaving C functions

7. HELP HELP HELP HELP HELP

8. HELP ++++++++++++++ HELP +++++++++++ HELP ++++++++++++++ HELP

9. OSP (Operating System Project) HELP HELP HELP HELP (files module)

10. HELP help HELP help

11. help VIRUS help help VIRUS help !

12. Help C newbie need help please

 

 
Powered by phpBB® Forum Software