Solved

please debug this

Posted on 1998-04-15
12
201 Views
Last Modified: 2010-04-10
i got this far on the problem, can someone please help me along the rest of it? thanx -Steve...here is the code:

#include<iostream.h>
#include<iomanip.h>
#include<string.h>
array_size=65;
string_size=80;

class wordcount
{
      public:
         void wordcount();
      int entertext();
      int uppercase();
      int sortdata();
   private:
         char strarray1[arraysize][stringsize];
      char strarray2[arraysize][stringsize];
      int k;
};
wordcount::wordcount()
{
      //????????
}
int wordcount::entertext()
{
      cout<<"enter text: ";
   for (k=0;k<arraysize;k++)
         cin>>strarray1[k];
}
int wordcount::uppercase()
{
      for(k=0;k<arraysize;k++)
         strcpy(strarray2[k],strarray1[k]);
   for(k=0;k<arraysize;k++)
         strupr(strarray2[k]);
}
int wordcount::sortdata()
{
      //??????
}
void main()
{
      wordcount.s1;
                  s1.uppercase;
                  s1.sortdata;
}
0
Comment
Question by:alter7148
  • 8
  • 4
12 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1184315
What is it doing wrong?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1184316
some inprovements.

Make "k" a local variable to uppercase() not a data member.

The three member procedures are declared to return int's.  But none of them do.  There is no reason for them to return int's so declare them to return void.
0
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 1184317
I see a bug.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1184318
you start of testing by calling uppercase() on s1.  However, you haven't filled in the strarray1 in s1 yet. This means that straray1 contains garbage. This means that some of the strings might appear to be unterminated, that is, too long for the storage space you set aside.  So when you try to copy the information to strarray2 you could be causing memory access violations.

Soluton.  Have your constructor initialize strarray1.  Or keep an integer count of the number of entries in strarray1 that are valid.  Initialize this to 0.  (The advantage to this is that you don't have to always completely fill the array when you enter the data).
0
 

Author Comment

by:alter7148
ID: 1184319
ummm yea thats all great an dandy about what ya said, but i still don't knwo what the heck to do!??! i mean ya said that stuff but it goes in one ear through the other. heh
and i don't know what to put in the wordcount::sortdata() fuction and the wordcount::wordcoount()function and also the rest of the main function.
-Steve
p.s. would you need the wording of the problem given to me by a co-worker?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1184320
When you create wordcount object like

wordcount.s1;

The strarray1 (and 2 as well) contain garbage.  You do not attempt to initialize them.  So when you do

s1.uppercase();

the program will try to copy garbage from strarray1 to strarray2.  This will probably cause it to crash.

You must prevent this somehow.  How is up to you and your needs.  You could
A.  Initialize strarray1 so it does not contain garbage by making each string stored a NUL string, that is, a string that contains no characters.  You could do this in the constructor.
B.  Keep track of how many strings are actually stored in strarray1 and make uppercase() copy only those strings that are actually stored.  (You have room for array_size strings, but you haven't necessarily filled in array_size entries.)
C.  You can make sure that every string in the array has been set before you call uppercase() or other functions.

As to the sort function, I have no idea what it is supposed to do.  What is supposed to be sorted?

As for counting words.  Create a function that counts the number of words in a string and returns this number.  Then use a for loop to count the words in each string stored in the array and sum them up.  Again you will need to know how many stings in the array have been set or you will need to set all the strings in the array.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:alter7148
ID: 1184321
ok, i still am having a lil trouble with it, maybe this will help ya, it's what my co-worker gave me:
C++ Language Question
    Title: "C++ program to perform to read non-punctuated text and give word frequency"

    How to implement a C++ program to perform a case-insensitve, frequency of occurance, analysis of words contained within a
    non-punctuated text. Only blank spaces between words, newline characters, and a single $ to mark end of text.
    Example text: Yellow Blue Green BLUE Red GREEN yellow $
    Example output: Word:   Frequency:
                    BLUE       2
                    GREEN      2      CONVERT ALL WORDS TO
                    RED        1        UPPERCASE
                    YELLOW     2       in alphabetical order
0
 
LVL 22

Expert Comment

by:nietod
ID: 1184322
Co-worker?  I think you mean teacher.

Given that assignment.  I think you are going about it the wrong way.  You have created a data structure to store the data that is entered (each line, as it is entered).  You don't need that.  You just need to read input a word at a time and process the word.  You don't need to store each line.

What you need to do is for each word, convert it to uppercase and then look for it in a list of words.  If it is in the list.  Increment the count associated with the word.  If it is not in the list.  Add it to the list and set its count to 1.  

Now the best way to do that would be to use the STL's map<> template.  That will do 99% of the work for you.  If you are allowed to use it.
0
 

Author Comment

by:alter7148
ID: 1184323
huh, i dunno what that is, and yes a co-worker, but i tink he had it fer a class i dunno?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1184324
Well does your co-worker understand?  If not can he ask specific questions.  It is unethical for me to provide him with a complete answer, but I can give advice, answer specific questions, and look over and critique code he has written.  
0
 

Author Comment

by:alter7148
ID: 1184325
i just wrote that code fer ya!
0
 
LVL 22

Expert Comment

by:nietod
ID: 1184326
That code is not appropriate for the task.  You need to create a structore that stores a list of words and the associated counts.  Each time a new word is encountered add it to the list with a count of one.  Each time a word in the list is found increment the count.

That's the general approach.  Now do you have specifc questions?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now