How cin.get() works in loop ?
-
#include
using namespace std;int main(){
char c;
cin.get(c); //get a charecter from keyboard and assigns it to cwhile(c != '\\n'){ cout << c ; //displays the charecter on screen cin.get(c); //get another charecter }
}
int the code above shouldn't cout << c print every charecter i put in cin.get() in loop ?
Quote:
while(c != '\n'){ cout << c ; //displays the charecter on screen cin.get(c); //get another charecter }
instead it print the string i enter collectively, for ex.
Quote:
input : "Jhon simon" output : "Jhon simon"
please explain Thank you
-
#include
using namespace std;int main(){
char c;
cin.get(c); //get a charecter from keyboard and assigns it to cwhile(c != '\\n'){ cout << c ; //displays the charecter on screen cin.get(c); //get another charecter }
}
int the code above shouldn't cout << c print every charecter i put in cin.get() in loop ?
Quote:
while(c != '\n'){ cout << c ; //displays the charecter on screen cin.get(c); //get another charecter }
instead it print the string i enter collectively, for ex.
Quote:
input : "Jhon simon" output : "Jhon simon"
please explain Thank you
Your explanation is a bit unclear. If the "input:" and "output:" are the same, what exactly is the issue? With limited information, the only thing I can suggest is:
cout << c << std::flush;
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
#include
using namespace std;int main(){
char c;
cin.get(c); //get a charecter from keyboard and assigns it to cwhile(c != '\\n'){ cout << c ; //displays the charecter on screen cin.get(c); //get another charecter }
}
int the code above shouldn't cout << c print every charecter i put in cin.get() in loop ?
Quote:
while(c != '\n'){ cout << c ; //displays the charecter on screen cin.get(c); //get another charecter }
instead it print the string i enter collectively, for ex.
Quote:
input : "Jhon simon" output : "Jhon simon"
please explain Thank you
-
#include
using namespace std;int main(){
char c;
cin.get(c); //get a charecter from keyboard and assigns it to cwhile(c != '\\n'){ cout << c ; //displays the charecter on screen cin.get(c); //get another charecter }
}
int the code above shouldn't cout << c print every charecter i put in cin.get() in loop ?
Quote:
while(c != '\n'){ cout << c ; //displays the charecter on screen cin.get(c); //get another charecter }
instead it print the string i enter collectively, for ex.
Quote:
input : "Jhon simon" output : "Jhon simon"
please explain Thank you
Google "cout buffering" for an explanation.
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
-
#include
using namespace std;int main(){
char c;
cin.get(c); //get a charecter from keyboard and assigns it to cwhile(c != '\\n'){ cout << c ; //displays the charecter on screen cin.get(c); //get another charecter }
}
int the code above shouldn't cout << c print every charecter i put in cin.get() in loop ?
Quote:
while(c != '\n'){ cout << c ; //displays the charecter on screen cin.get(c); //get another charecter }
instead it print the string i enter collectively, for ex.
Quote:
input : "Jhon simon" output : "Jhon simon"
please explain Thank you
cin.get()
does not read individual keystrokes -- the input is buffered by the terminal until you hit Enter, then all of the input is sent to the program at once. To read individual keystrokes, you'll have to rely on a third-party tool like GNU's ncurses library.