Character Arrays

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2005
Posts: 11
Reputation: kdw3 is an unknown quantity at this point 
Solved Threads: 0
kdw3 kdw3 is offline Offline
Newbie Poster

Character Arrays

 
0
  #1
Dec 4th, 2005
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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,569
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1484
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Character Arrays

 
0
  #2
Dec 4th, 2005
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC