943,987 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 5376
  • C++ RSS
Dec 4th, 2005
0

Character Arrays

Expand Post »
Im trying to create a simple RLC program. Im n ot sure the best way about it, but im trying to to use to 20 character arrays. One to read in the characters to be compressed (buffer) and the other is used to as a check. Either way, the buffer[20] reads in from a file and is fine, but other array (outputs[20]) does not read anything in, so when i set the first element of outputs[0] = buffer[0], i get the first letter of buffer, followed by a gibberish, which throws off all calculations. Code is below:

C++ Syntax (Toggle Plain Text)
  1. int Readchar(ifstream& fileread)
  2. {
  3. char buffer[20];
  4. char temp;
  5. int space, count, length;
  6. count = 0;
  7. cout << fileread.peek() << endl;
  8. while (fileread.peek() != EOF)
  9. {
  10. fileread >> buffer;
  11. Checkbuffer(buffer);
  12. count ++;
  13. }
  14. return 0;
  15. }
  16.  
  17.  
  18. int Checkbuffer(char buffer[])
  19. {
  20. char outputs[20];
  21. int k, length, count, adj;
  22. adj = 0;
  23. count = 0;
  24. length = strlen(buffer);
  25. outputs[0] = buffer[0];
  26. cout << outputs;
  27. for (k = 1; k <= length; k++)
  28. {
  29. if (buffer[k] == outputs[k-1])
  30. {
  31. outputs[k] = buffer[k];
  32. }
  33. else
  34. {
  35. Outputstring(outputs,k);
  36. }
  37. }
  38. return 0;
  39. }

If anyone can suggest how to empty outputs[20] so it contains nothing, it would be appreciated.
Regards
Kevin
Last edited by Dave Sinkula; Dec 4th, 2005 at 2:08 pm. Reason: Added [code][/code] tags.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kdw3 is offline Offline
15 posts
since Oct 2005
Dec 4th, 2005
0

Re: Character Arrays

cout << buffer -- buffer needs to be a null-terminated character array. when you defined buffer you did not initialize it to anything, that means it contains random characters and the buffer may or may not contain a null-terminating 0. So cout just keeps displaying characters searching all memory for the first 0, which may be considerably beyond the end of the buffer.

To avoid that problem, declare buffer like this
 char buffer[20]  = 0;
That will fill the buffer with all 0s.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,953 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: returning a value from a header file
Next Thread in C++ Forum Timeline: Cryptology





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


Follow us on Twitter


© 2011 DaniWeb® LLC