cin.get() question

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using namespace std;

int main() {

char c = 0;
while ( c != '\n' ) {
cin.get(c);
cout << "-";
}
cout << endl;
return 0;
}


Why is it that the output of the above code's output (if the user types "Hello World") is:
Hello World.
-------------

I am confused because cin.get() extracts a character from the stream without the user pressing the return key. So why is the output not something like
H-e-l-l-o- -W-o-r-l-d-.-
?

Thanks
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main
{
clrscr();
char c = 0;
while ( c != '\n' )
{
cin.get(c);
cout << "-";
cout<<c;     //----------display the entered character..
}
cout << endl;
getch();
return 0;
}


actually your string is entered successfully but you not display it back again....
nd in ur output ur "hello world" is that wat u write ...and "-" is what u dislpay in output.. cout<<"-"; :p
I'm saying why is the '-' character not displayed as the user types the string "Hello World" rather than after s/he has hit enter?
1
2
3
4
5
6
7
8
9
10
11
#include <iostream.h>
#include<conio.h>
void main()
{
char c;
while ( c != '\t') //----press tab key to exit
{
c=getche();
cout<<"-";
}
}

I tried hard to it....bt seriously i found this problem...very complicated to me....at the end i did this and get the result...but i em not satisfied....i ll' try it later dude oka....
"I am confused because cin.get() extracts a character from the stream without the user pressing the return key. "
I'm not sure if this is true. Seems like the compiler is executing the rest of the loop as soon as you press enter.
I THINK U SHOULD TRY ASCII VALUE FOR ENTER KEY IN WHILE CONDITION......
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream.h>
#include<conio.h>
int main()
{
char c='\0';
while ( c != '\r')
{
c=getche();
cout<<"-";
}
return 0;
}


finally got something for u... use carrige return to terminate the while...
nd getche as it does not hold the characters and does not require ENTER to execute,,,as in case of cin.get or getchar...
Last edited on
That's because cin.get() doesn't get characters instantly, it has to wait for the buffer to fill up. That only occurs when the user hits ENTER.

@arun thakur:
Don't use conio.h or getch() etc, those are non-standard functions and probably won't work on other systems.
You seem to be confused as to how cin.get() works. It doesn't grab characters as the user types them. What cin.get() does is get the next character to be entered into the cin stream and store it in the specified variable. So what your code does is loop through every character the user enters, storing it in the "c" variable, and then printing a "-". What you need to do is change this line:

 
cout << "-";


To:
 
cout << c << "-";
But he specifically wanted it to add a dash immediately after each character he types, AS he types...not to repeat what he typed with a dash after each letter.
Well, he could something like...

1
2
3
4
5
6
7
8
9
cout << "Start Typing: ";

char x;

while(true)
{
    cin >> x;
    cout << "-";
}


The only problem with that is that a newline is inserted when you input data using CIN because of pressing enter.
@firedraco-
i usually work on windows....nd i use the traditional c compiler....may u r familiar with gcc linux compiler....... i don't think dat i should change my os for such things....it only matter the logic.....if u r programmer than u must knw dat syntax may differ bt logic...is always same in all languages....
Topic archived. No new replies allowed.