473,395 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Why does cin.get() function like this?

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. char c = 0;
  6. while ( c != '\n' ) {
  7. cin.get();
  8. cout << "-"
  9. }
  10.  
  11. return 0;
  12. }
Why is the output of this code:
Hello World.
-------------

Rather than:
H-e-l-l-o- -W-o-r-l-d-.--


(When the user inputs the string "Hello World" and presses enter).
Apr 10 '11 #1

✓ answered by pcbabu

Expand|Select|Wrap|Line Numbers
  1. Hello World.
loop goes like bellow
Expand|Select|Wrap|Line Numbers
  1. takes input H and print -
  2. takes input e and print -
  3. takes input l and print -
  4. takes input l and print -
  5. ...
  6. ...
  7. takes input \n and print -
  8.  
but the cursor is already ahead of your input. Here it doesn't go backward. cout prints in cursor position so input is like that.

2 1704
whodgson
542 512MB
Because you typed in Hello World as input on line 7.
Your output came from line 8 and was a series of dashes.
Apr 10 '11 #2
pcbabu
4
Expand|Select|Wrap|Line Numbers
  1. Hello World.
loop goes like bellow
Expand|Select|Wrap|Line Numbers
  1. takes input H and print -
  2. takes input e and print -
  3. takes input l and print -
  4. takes input l and print -
  5. ...
  6. ...
  7. takes input \n and print -
  8.  
but the cursor is already ahead of your input. Here it doesn't go backward. cout prints in cursor position so input is like that.
Apr 11 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Philipp | last post by:
Hello I profiled my program and got as result that two functions taking each 10% of the calculation time are simple get() for a member of a class (returning a pointer). Do you think that...
2
by: upadhyay | last post by:
Hi, I was working on a dir like function that gives more information than the usual dir, but I am not satisfied with the way I get function arguments for callable members of an object. Take a...
1
by: DMamoun | last post by:
I am trying to call a fuction a a C# dll using the GetProcAddress, but the returned handle is null, wheras the LoadLibrary of the container dll succeeds, I write the function name as it is written...
3
by: ad | last post by:
I download the source of Portal Starter Kit form http://www.asp.net/Default.aspx?tabindex=8&tabid=47 I find there are Register tag in many .aspx, like this HtmlModule.ascx , the first line is ...
2
by: xhenxhe | last post by:
I'm using Indy Sockets for some automation. I FTP files to a UNIX and Linux server, run the files using SSH and then FTP them back locally. Everything seems to work find until FTPing back to the...
7
by: stellstarin | last post by:
hi all, I have a HTML page with two forms. While trying to add a hidden element in a first form by the default javascript function, the error message 'object does not support this property or...
1
by: kamleshsharmadts | last post by:
I am using Ajax with struts in web application. from jsp i am calling a function of ajax.js onclick of a button. code of that call function which calling from jsp given as below:- ...
7
by: dmitrey | last post by:
hi all, can anyone explain howto get function from module, known by string names? I.e. something like def myfunc(module_string1, func_string2, *args): eval('from ' + module_string1 + 'import...
2
by: Samant.Trupti | last post by:
Hi, Does main function support unicode? int main( int argc, char** argv ) can I say int mainw( int argc, wchar_t** argv )? Thanks Trupti
8
by: thatcollegeguy | last post by:
http://smarterfootball.com/exits/theHTML.html I am not sure what is wrong w/ this code. The main issue is that the table that is in the initial html will empty its td but the table that I load...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.