| | |
Character Arrays
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2005
Posts: 11
Reputation:
Solved Threads: 0
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:
If anyone can suggest how to empty outputs[20] so it contains nothing, it would be appreciated.
Regards
Kevin
C++ Syntax (Toggle Plain Text)
int Readchar(ifstream& fileread) { char buffer[20]; char temp; int space, count, length; count = 0; cout << fileread.peek() << endl; while (fileread.peek() != EOF) { fileread >> buffer; Checkbuffer(buffer); count ++; } return 0; } int Checkbuffer(char buffer[]) { char outputs[20]; int k, length, count, adj; adj = 0; count = 0; length = strlen(buffer); outputs[0] = buffer[0]; cout << outputs; for (k = 1; k <= length; k++) { if (buffer[k] == outputs[k-1]) { outputs[k] = buffer[k]; } else { Outputstring(outputs,k); } } return 0; }
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.
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
That will fill the buffer with all 0s.
To avoid that problem, declare buffer like this
char buffer[20] = 0;![]() |
Similar Threads
- Can arrays be sent to functions in C? (C)
- Problems with cin (char arrays|strings) (C++)
- Please help me with arrays (C++)
- Problem with Character Arrays (C++)
- Sorting character arrays! (C++)
Other Threads in the C++ Forum
- Previous Thread: returning a value from a header file
- Next Thread: Cryptology
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






