Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does `cin` produce a newline automatically?

Tags:

c++

Let's consider the following code:

#include<iostream>

int main()
{
    std::cout<<"First-";
    std::cout <<"-Second:";
    int i;
    std::cin>>i;
    std::cout<<"Third in a new line.";

    while(1){}


}

The output when value 4 is given to i is:

First--Second:4
Third in a newline

cout doesn't print any newline. But after I input any value(4) for i a newline is printed. There could be two possible reasons for this:

  • The Enter key I press after typing a numerical value for i is printed as a newline.
  • cin automatically generates a newline.

Although the first reason seems more reasonable but the reason, I am thinking 2nd reason could also be true, is because after Third is printed when I press Enter no new line is printed even the program continue to run because of while(1)--which means the console window doesn't print a newline when Enter key is pressed. So it seems like cin automatically prints a newline.

So, why is the newline being generated after I give input to cin? Does cin automatically prints a newline?

like image 635
user31782 Avatar asked Feb 27 '16 11:02

user31782


People also ask

Does CIN leave a newline?

cin is not a command. It's an object of type std::ostream . In contrast, >> is std::ostream::operator>> , i. e. an operator (still not a "command"). It leaves the newline in the buffer because the C++ standard says that this operator has to leave the newline in the buffer.

Does CIN read line by line?

The code cin >> y; only reads in one word, not the whole line.

Does C++ Getline include newline?

The getline() function extracts characters from the input stream and appends it to the string object until the delimiting character is encountered. If no delimiter is specified, the default is the newline character at the end of the line. The input value does NOT contain the delimiter.

Can you use an Endl after a cin?

You can use endl rather than the \n if you want, but what you have is fine. Show activity on this post.


3 Answers

The number and newline you entered is printed by the console software. cin won't print anything in my understanding.

Try giving some input via redirect or pipe, and I guess you will see no new line printed.
For example:

$ echo 4 | ./a.out
First--Second:Third in a new line.

where $ is the prompt and echo 4 | ./a.out(Enter) is your input.

like image 58
MikeCAT Avatar answered Oct 14 '22 07:10

MikeCAT


Check this out: http://ideone.com/tBj1uS

You can see there that input and output are separated.

stdin:

1
2

stdout:

First--Second:Third in a new line.

Which means, that the newline is produced by the Enter key and is a part of the input, not the output.

like image 3
sjaustirni Avatar answered Oct 14 '22 05:10

sjaustirni


If someone will have the same problem, I was able to solve it like this:

string ans1;
getline(cin, ans1);
    ans1.erase(remove(ans1.begin(), ans1.end(), '\n'),
        ans1.end());
int ans1int = atoi(ans1.c_str());

Basically, it works by deleting all the newline characters in the string, and then ,aking it integer or whatever else you need. Also you will need algorithm library for it It's not that elegant, but hey, it works!

like image 1
Geryno Avatar answered Oct 14 '22 06:10

Geryno