944,098 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 675
  • C++ RSS
Nov 7th, 2009
0

first time using a class: help?

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string.h>
  3. #include <iomanip>
  4. #include <ctype.h>
  5. #include "String2.cpp"
  6. using namespace std;
  7.  
  8. const int MAX_WORD_LENGTH = 254;
  9.  
  10. // The type definition below permits much easier prototyping of
  11. // functions that pass arrays of strings.
  12.  
  13. typedef char String[MAX_WORD_LENGTH + 1]; // + 1 for null terminator. ****THIS IS WHERE ONE ERROR IS******
  14.  
  15. void ReadWord (String Word, int MaxLength);
  16.  
  17. void WriteWords (String Word[],
  18. int Count[],
  19. int TotalWordCount,
  20. int DistinctWordCount);
  21.  
  22. void StoreWord (String NewWord,
  23. String Word[],
  24. int Count[],
  25. int &TotalWordCount,
  26. int &DistinctWordCount,
  27. int MaxWordCount);
  28.  
  29. /*************************** main ***********************************/
  30.  
  31. void main()
  32. {
  33. const int MAX_WORD_COUNT = 130;
  34.  
  35. String NewWord,
  36. Word[MAX_WORD_COUNT + 1] = {"", ""}; // ALWAYS allow 1 extra slot!
  37.  
  38. int Count[MAX_WORD_COUNT + 1] ={0}, // Initialize array to zeros
  39. DistinctWordCount = 0,
  40. TotalWordCount = 0;
  41.  
  42. ReadWord(NewWord, MAX_WORD_LENGTH); // Get the first word
  43.  
  44. while ( NewWord[0] != 0 ) // ***THIS IS THE SECOND ERROR*** While NewWord is not null string
  45. {
  46. StoreWord (NewWord, Word, Count, TotalWordCount,
  47. DistinctWordCount, MAX_WORD_COUNT);
  48.  
  49. ReadWord(NewWord, MAX_WORD_LENGTH); // Get the next word
  50. }
  51.  
  52. WriteWords(Word, Count, TotalWordCount, DistinctWordCount);
  53.  
  54. }
  55.  
  56. /************************ ReadWord *********************************
  57.  
  58. DESCRIPTION Reads a word from standard input and stores in the array
  59.   Word.
  60.  
  61.   For the purposes of this routine, a "word" is any
  62.   contiguous sequence of non-blank characters.
  63.  
  64. PARAMETERS Word An array of char.
  65.   MaxWordLength: The maximum number of chars to store in
  66.   NewWord (not including the null terminator).
  67.   Chars beyond this number are discarded.
  68.  
  69. CALLS cin.get and cin.good, both in the iostream library.
  70.   isspace from ctype file.
  71.  
  72. NOTE Words longer than MaxWordLength are truncated.
  73.  
  74. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  75. void ReadWord (String NewWord, int MaxWordLength)
  76. {
  77. char Ch;
  78. int K;
  79.  
  80. cin >> Ch; // Skip blanks, and get the first character of word
  81.  
  82. K = 0;
  83. while ( cin.good() && K < MaxWordLength && !isspace(Ch) ) // Store chars
  84. {
  85. NewWord[K++] = Ch;
  86. cin.get(Ch);
  87. }
  88. NewWord[K] = 0; // Add null terminator
  89.  
  90. while ( cin.good() && !isspace(Ch) ) // Discard tail end of
  91. cin.get(Ch); // long words
  92. }
  93.  
  94. /*************************** StoreWord ********************************
  95.  
  96. DESCRIPTION Adds a string NewWord (holding up to MAX_WORD_LENGTH
  97.   characters) to the end of the array Word.
  98.  
  99. PARAMETERS NewWord The string (array of char) to be added
  100.   Word An array of String (char[MAX_WORD_LENGTH])
  101.   DistinctWordCount The number of different words in the array
  102.   TotalWordCount The total number of word in the file
  103.   MaxWordCount The max number of words that can be stored
  104.   in the array Word. If DistinctWordCount ==
  105.   MaxWordCount, the function is exited.
  106.  
  107. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  108. void StoreWord (String NewWord,
  109. String Word[],
  110. int Count[],
  111. int &TotalWordCount,
  112. int &DistinctWordCount,
  113. int MaxWordCount)
  114. {
  115. int i, k = 0;
  116.  
  117. while ( strcmp(NewWord, Word[k]) > 0 && k < DistinctWordCount )
  118. ++k;
  119.  
  120. // Assert: k is NewWord's correct position in the ordered array Word
  121.  
  122. if ( strcmp(NewWord, Word[k]) == 0 ) // NewWord is already there
  123. {
  124. ++Count[k];
  125. ++TotalWordCount;
  126. }
  127.  
  128. else if ( DistinctWordCount < MaxWordCount ) // Room for a new word
  129. {
  130. ++DistinctWordCount; // If this line reached, found new word
  131. ++TotalWordCount;
  132.  
  133. for ( i = DistinctWordCount-1; i > k; --i ) // Make room for
  134. { // NewWord
  135. strcpy(Word[i], Word[i-1]);
  136. Count[i] = Count[i-1];
  137. }
  138. strcpy(Word[k], NewWord); // Store NewWord in array
  139. Count[k] = 1;
  140. }
  141. }
  142.  
  143. /*************************** WriteWords ********************************
  144.  
  145. DESCRIPTION Writes the strings in the array Word to standard output.
  146.   The words are left justified. After WORDS_PER_ROW words have
  147.   been written a new line is started.
  148.  
  149. PARAMETERS Word An array of String (char[MAX_WORD_LENGTH])
  150.   WordCount The number of words in the array
  151.  
  152. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  153. void WriteWords (String Word[],
  154. int Count[],
  155. int TotalWordCount,
  156. int DistinctWordCount)
  157. {
  158. const char *Header1 = " Word Count | ",
  159. *Header2 = " | ",
  160. *Separator = " | ";
  161.  
  162. const int WORD_FW = 17,
  163. COUNT_FW = 4,
  164. WORDS_PER_ROW = 3;
  165.  
  166. int C;
  167.  
  168. cout << "\n\n";
  169.  
  170. if ( TotalWordCount == 0 )
  171. return;
  172.  
  173. for ( C = 1; C <= WORDS_PER_ROW; ++C )
  174. cout << Header1;
  175. cout << endl;
  176.  
  177. for ( C = 1; C <= WORDS_PER_ROW; ++C )
  178. cout << Header2;
  179. cout << endl;
  180.  
  181. int N = 0;
  182.  
  183. while ( N < DistinctWordCount )
  184. {
  185. for ( C = 1; C <= WORDS_PER_ROW && N < DistinctWordCount; ++C )
  186. {
  187. cout << setiosflags(ios::left) << setw(WORD_FW) << Word[N];
  188. cout << setiosflags(ios::right) << setw(COUNT_FW)<< Count[N];
  189. cout << Separator;
  190. cout << resetiosflags(ios::right);
  191. ++N;
  192. }
  193. cout << endl;
  194. }
  195.  
  196. cout << "\nTotal Word Count : " << TotalWordCount << endl;
  197. cout << "\nDistinct Word Count : " << DistinctWordCount << endl;
  198. }

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.
Last edited by ninreznorgirl2; Nov 7th, 2009 at 9:36 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ninreznorgirl2 is offline Offline
11 posts
since Oct 2009
Nov 8th, 2009
0
Re: first time using a class: help?
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.
Reputation Points: 78
Solved Threads: 15
Junior Poster
u8sand is offline Offline
131 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Loan Program
Next Thread in C++ Forum Timeline: C++ Strings For Input





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC