cin.get() glitch[solved...sort of]

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace 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?
Last edited on
You forgot to zero the waldo.
So when you try to show all it's contents it show you everything the memory has (junk).
Last edited on
z...zeroing...?
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' );

Here is the documentation for fill_n()
http://www.cplusplus.com/reference/algorithm/fill_n.html
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main(){
    char waldo[2000];
    fill_n( waldo, 2000, '\0' );
    cin.get(waldo, 2000);
    for(int i = 0; i < 2000; i++)
    cout << waldo[i];
    cout << endl;
    cin.get();
    cin.get();
    return 0;}

Now, it will only show the output for a millisecond, then go to a blank screen. Any ideas (again)?
When you use two cin.get() it stops for me.
You can use cin.ignore(); after the cin.get(waldo, 2000); to clear the cin stream.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main(){
    char waldo[2000];
    fill_n( waldo, 2000, '\0' );
    cin.get(waldo, 2000);
    cin.ignore();
    for(int i = 0; i < 2000; i++)
    cout << waldo[i];
    cout << endl;
    cin.get();
    return 0;
}


(Sorry, I edited because I had a mistake)
Last edited on
Nope.

Stramge...
It doesn't work even with the cin.ignore() ?
Then sorry, I don't know, it works fine with me...
(add the #include <algorithm> for the fill_n)
hmm... what exactly is your program all about?
I really dont understand cos ive already tried it and it only prints random characters you see,
@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...
@QWERTYman
If you want to read a line why don't you use the getline() command?
You won't have such problems.

[EDIT]
Here is how the program with the getline should be:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main(){
    char waldo[2000];
    fill_n( waldo, 2000, '\0' );
    cin.getline(waldo, 2000);
    for(int i = 0; i < 2000; i++)
    cout << waldo[i];
    cout << endl;
    cin.get();
    return 0;
}
Last edited on
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.
Last edited on
QWERTYman

Mitsakos, don't be too harsh toward unattached. I might have done the same thing.
(Goodness knows I usually do.)


hMmMmMm.... well, it was nothing for me... 'that' kind of people, they cant
harm you.... thank for saying that, though...

the truth is, i really had NO idea bout his program but i would like to learn
something from here...

Mitsakos
chill... relax...


anyway, are you using the same compilers? that may be a problem....
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');

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <limits>
using namespace std;

int main(){
    char waldo[2000];
    fill_n( waldo, 2000, '\0' );
    cin.get(waldo, 2000);
    cin.ignore(numeric_limits<streamsize>::max(),'\n');
    for(int i = 0; i < 2000; i++)
    cout << waldo[i];
    cout << endl;
    cin.get();
    return 0;
}
Last edited on
Even now, it refuses to work, but now I have learned much about the subject from this.

Thanks.
Topic archived. No new replies allowed.