#include <iostream>
usingnamespace std;
int main(){
char waldo[2000];
cin.get(waldo, 2000).get();
for(int i = 0; i < 2000; i++)
cout << waldo[i];
cout << endl;
cin.get();
return 0;}
I'm not sure if it's my syntax or what, but once I start the program, I type a letter, then get spammed with random characters and the \a alert. Any ideas?
edit: If you are wondering, I use waldo because it's metasyntactic, the name doesn't matter. I could easily use 'foo', and who would care?
When you declare char waldo[2000] you just reserve from the memory 2000 places of the size of char.
C++ doesn't automatically clear that part of memory. So when you try to view the data in that place it returns everything it had from other programs that used it before.
One way to zero the memory, that waldo uses, is the following: for(int i=0; i<2000; i++) waldo[i] = 0;
Just put it after the declaration of waldo.
You can also use the fill_n() from the algorithms. fill_n( waldo, 2000, '\0' );
@unattached
Do you even read what other people post? Or you just read the first post and try to answer it?
What people post as their code doen't all the time has to make sence. They have a problem with a part of it and they just post this part...
It IS my entire code, I was showing somebody how you can store strings in a character array.
Mitsakos, don't be too harsh toward unattached. I might have done the same thing.
(Goodness knows I usually do.)
The program SHOULD echo the input, but it will flash the output, then clear the screen, then wait for a carriage return.
unattached
Sorry about that, it usually bothers me when people give answers that doesn't have to do with the replies above it.
QWERTYman
Did it finaly work?
I was reading something about cin.ignore() and found out that if you want to ignore everything cin has in it you can include <limits> and use: cin.ignore(numeric_limits<streamsize>::max(),'\n');