Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cin

Status
Not open for further replies.

MrGeek

MIS
Nov 3, 1999
113
US
Hehe all this time dishing out answers (some even sounding as If I was being very condescending) now it is my turn to be beaten up by all you wise and noble men (people for the PC)

Anyhow, I am turning my skills to programming and I am very literal so the smallest of things need an answer. So forgive my extreme noob questions. Does "cin" generate a newline as "\n" and "endl" would? In my lessons, newlines are being generated that I have not specified, only thing present is "cin" in the areas the newline is appearing.

Thx

Geek
The Geek


Dont be afraid to share what you know. There are no losers in our arena, only self rightous monkeys atop their own tree.
 
No? Ok, then could someone explain why the below snipet is printing a new line?

snip

int MetsScore, YankeesScore;
cout <<&quot;Enter the scrore for the Mets: &quot;;
cin >> MetsScore;

cout <<&quot;Enter the score for the Yankees: &quot;;
cin >> YankeesScore;
snip

this should print the following (all in one line)

Enter the score for the Mets: 10Enter the score for the Yankees: 8

However it is doing this (which clearly resembles a newline character)

Enter the score for the Mets: 10
Enter the score for the Yankees: 8
The Geek


Dont be afraid to share what you know. There are no losers in our arena, only self rightous monkeys atop their own tree.
 
I am gonna stick with the theory that &quot;cin&quot; does act as a newline trigger, becuase I have just done another program where it does the same thing. If someone comes up with a more plausable idea, I'll be bock :)
The Geek


Dont be afraid to share what you know. There are no losers in our arena, only self rightous monkeys atop their own tree.
 
There's a difference between what cin gives you and what appears onscreen. cin doesn't affect output at all.

Whatever you type appears onscreen. When you type a newline (the Enter key), it appears onscreen.

When you hit Enter, everything you've typed is transmitted to an input buffer that cin looks at. It parses the input for, in your case, integers. It ignores leading whitespace (and therefore also trailing whitespace), which includes the newline.

Again, cin does not affect output at all. What you type is directly shown on the screen. That's a function of the terminal or shell or whatever you're using, not of your program.

Just try hitting Enter multiple times without entering a number. All those newlines show up. That's because you typed them and the shell interpreted them, not because of anything cin is doing. The newlines will be passed to cin, which will ignore them unless told otherwise.
 
Hmm perhaps I am asking this all wrong. Let me try again.

If I type the following

sample

std::cout << &quot;To print this text on two neat readable lines, &quot;;
std:cout << &quot;you will need to place a \n at the end of lines.&quot;;

end sample

But notice I didn't place the \n after lines, so the output of that statement will look like

text here text here

and not like

text here
text here

This is hopefully explaining it better because thats what I am getting with &quot;cin&quot; when i ask for user input it seems to place a \n as well on its own. Bottom line its cool because I don't have to place \n on my cout / cin statements.
The Geek


Dont be afraid to share what you know. There are no losers in our arena, only self rightous monkeys atop their own tree.
 
No, I understood you. As I said, that newline appears onscreen because the user typed it, not because of anything cin is doing.

If you put newlines at the ends of your output statements, you'd get this:

Enter the score for the Mets:\n#1
10\n#2
Enter the score for the Yankees:\n#3
8\n#4

You'd get newlines 1 and 3 at the end of each output statement because you told it to output.

You'd get newlines 2 and 4 because the user pressed the Enter key.


Think about this: when the user types letters on the screen, you don't have anything in your application that's echoing them to the screen. cin doesn't perform any output. You don't use cout to get them there. There's nothing in your program doing that. That's just a property of the shell you're typing at. It echos typed characters. And that goes for newlines, too. Newlines are produced by the Enter key. Pressing the Enter key causes a newline to appear on the screen just as pressing the 'A' key causes an 'A' to appear on the screen.

Try writing a program that spins forever in a for loop. Then run it and type some characters. They appear. Type a newline. It appears. You have no cin or cout anywhere in your program. You aren't expecting to do any input or output. Yet stuff appears onscreen. Just because you typed it. That's a property of the shell. It echos typed characters.
 
Shorter explanation if it helps:

You never told it to output &quot;10,&quot; but that still appears on the screen when they type it. Same thing with the newline.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top