I've worked with making classes, but I've never had to use a class in code that I write, so I'm having a hard time.

#include <iostream>
#include <string.h>
#include <iomanip>
#include <ctype.h>
#include "String2.cpp"
using namespace std;

const int MAX_WORD_LENGTH = 254;

// The type definition below permits much easier prototyping of
// functions that pass arrays of strings.

typedef char String[MAX_WORD_LENGTH + 1]; //  + 1 for null terminator.  ****THIS IS WHERE ONE ERROR IS******

void ReadWord (String Word, int MaxLength);

void WriteWords (String  Word[],
                 int     Count[],
                 int     TotalWordCount,
                 int     DistinctWordCount);

void StoreWord (String  NewWord,
                String  Word[],
                int     Count[],
                int    &TotalWordCount,
                int    &DistinctWordCount,
                int     MaxWordCount);

/***************************  main  ***********************************/

void main()
{
  const  int MAX_WORD_COUNT  = 130;

  String  NewWord,
          Word[MAX_WORD_COUNT + 1] = {"", ""};       // ALWAYS allow 1 extra slot!

  int     Count[MAX_WORD_COUNT + 1] ={0}, // Initialize array to zeros
          DistinctWordCount = 0,
          TotalWordCount = 0;

  ReadWord(NewWord, MAX_WORD_LENGTH);  //  Get the first word
 
  while ( NewWord[0] != 0 ) // ***THIS IS THE SECOND ERROR***         While NewWord is not null string
    {
    StoreWord (NewWord, Word, Count, TotalWordCount,
               DistinctWordCount, MAX_WORD_COUNT);

    ReadWord(NewWord, MAX_WORD_LENGTH);             //  Get the next word
    }

  WriteWords(Word, Count, TotalWordCount, DistinctWordCount);

}

/************************    ReadWord    *********************************

DESCRIPTION  Reads a word from standard input and stores in the array
             Word.

             For the purposes of this routine, a "word" is any
             contiguous sequence of non-blank characters.

PARAMETERS   Word            An array of char.
             MaxWordLength:  The maximum number of chars to store in
                             NewWord (not including the null terminator).
                             Chars beyond this number are discarded.

CALLS        cin.get and cin.good, both in the iostream library.
             isspace from ctype file.

NOTE         Words longer than MaxWordLength are truncated.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
void ReadWord (String  NewWord,  int MaxWordLength)
{
  char Ch;
  int   K;

  cin >> Ch;     // Skip blanks, and get the first character of word

  K = 0;
  while ( cin.good() && K < MaxWordLength && !isspace(Ch) )  //  Store chars
    {
    NewWord[K++] = Ch;
    cin.get(Ch);
    }
  NewWord[K] = 0;                             //  Add null terminator

  while ( cin.good() && !isspace(Ch) )        //  Discard tail end of
    cin.get(Ch);                              //    long words
}

/***************************  StoreWord   ********************************

DESCRIPTION  Adds a string NewWord (holding up to MAX_WORD_LENGTH
             characters) to the end of the array Word.

PARAMETERS   NewWord            The string (array of char) to be added
             Word               An array of String (char[MAX_WORD_LENGTH])
             DistinctWordCount  The number of different words in the array
             TotalWordCount     The total number of word in the file
             MaxWordCount       The max number of words that can be stored
                                in the array Word. If DistinctWordCount ==
                                MaxWordCount, the function is exited.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
void StoreWord (String  NewWord,
                String  Word[],
                int     Count[],
                int    &TotalWordCount,
                int    &DistinctWordCount,
                int     MaxWordCount)
{
  int i, k = 0;

  while ( strcmp(NewWord, Word[k]) > 0 && k < DistinctWordCount )
    ++k;

  // Assert:  k is NewWord's correct position in the ordered array Word

  if ( strcmp(NewWord, Word[k]) == 0 )   // NewWord is already there
    {
    ++Count[k];
    ++TotalWordCount;
    }

  else if ( DistinctWordCount < MaxWordCount ) // Room for a new word
    {
    ++DistinctWordCount;  // If this line reached, found new word
    ++TotalWordCount;

    for ( i = DistinctWordCount-1;  i > k;  --i ) // Make room for
      {                                             //  NewWord
      strcpy(Word[i], Word[i-1]);
      Count[i] = Count[i-1];
      }
    strcpy(Word[k], NewWord); // Store NewWord in array
    Count[k] = 1;
    }
}

/***************************  WriteWords   ********************************

DESCRIPTION  Writes the strings in the array Word to standard output.
             The words are left justified. After WORDS_PER_ROW words have
             been written a new line is started.

PARAMETERS   Word       An array of String (char[MAX_WORD_LENGTH])
             WordCount  The number of words in the array

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
void WriteWords (String  Word[],
                 int     Count[],
                 int     TotalWordCount,
                 int     DistinctWordCount)
{
  const char *Header1   = "    Word          Count | ",
             *Header2   = "                        | ",
             *Separator = "   | ";

  const int WORD_FW       = 17,
            COUNT_FW      = 4,
            WORDS_PER_ROW = 3;

  int C;

  cout << "\n\n";

  if ( TotalWordCount == 0 )
    return;

  for ( C = 1; C <= WORDS_PER_ROW;  ++C )
    cout << Header1;
  cout << endl;

  for ( C = 1; C <= WORDS_PER_ROW; ++C )
    cout << Header2;
  cout << endl;

  int N = 0;

  while ( N < DistinctWordCount )
  {
    for ( C = 1; C <= WORDS_PER_ROW && N < DistinctWordCount; ++C )
    {
      cout << setiosflags(ios::left)  << setw(WORD_FW) << Word[N];
      cout << setiosflags(ios::right) << setw(COUNT_FW)<< Count[N];
      cout << Separator;
      cout << resetiosflags(ios::right);
      ++N;
     }
    cout << endl;
   }

  cout << "\nTotal    Word Count : " << TotalWordCount    << endl;
  cout << "\nDistinct Word Count : " << DistinctWordCount << endl;
}

my professor wrote most of the code up. i added the class heading at the top and thats about it. I cant get it to compile and I'm getting an errors that say:

hw7.cpp(34) : error C2040: 'String' : 'char [255]' differs in levels of indirection from 'String'
hw7.cpp(65) : error C2676: binary '[' : 'String' does not define this operator or a conversion to a type acceptable to the predefined operator

the class is called String2.cpp.
I dont know why its not compiling.

Dani AI

Generated

Short diagnosis and what to try first

The compiler errors you posted are a classic name collision: you have a C-style typedef named String (a char[...]) in your file and you also pulled in a user-defined type from String2.cpp that is named String. The compiler ends up seeing two different types with the same identifier and gets confused — that explains the “differs in levels of indirection” message and why NewWord[0] fails (the compiler now thinks NewWord is an object type, not a char array, so operator[] is not available).

Practical fixes (pick one)

  • Easiest: rename your C-style alias so it won’t collide. For example:

    typedef char CStr[MAX_WORD_LENGTH + 1];

    Then update the function prototypes and variables to use CStr. This is minimally invasive.

  • Better: stop using raw char arrays and use std::string (and std::vector<std::string> for the word list). That avoids this whole class-name/typedef problem and is less error-prone.

  • If you intend to use the class in String2.cpp instead of C-style strings, convert the code to use that class’s API (or rename the class in String2.*).

Other important notes

  • Don’t #include a .cpp file. Create/use a header (e.g. String2.h) and compile String2.cpp separately (or add both .cpp files to the project). Including .cpp files can mask problems and cause multiple-definition/linker issues later.
  • Change void main() to standard int main() and return 0;.

Next step

As suggested, post the contents of String2.cpp (or better, the header) so people can confirm the class name and show the exact mismatch if renaming the typedef doesn’t fix it.

I'm sorry, I can't help you if I don't even see the class. Please post the contents of String2.cpp
I'm assuming the class is named String? You probably get an error where it says "**** this is where error is ****" because of constructor issues.
Anyway please post String2.cpp's contents, I would help you but I'm about to leave. Maybe someone else can help you.

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.