Why do we use endl or /n only after the cout statement ?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why do we use endl or /n only after the cout statement ??

if they are line breakers why don't we use them after typing the variables ??and how when we write👇 # include <iostream> using namespace std; int main () how the program make a newline or breaks the line alone ???? please help

14th Dec 2016, 3:10 PM
NadaKarim
3 Answers
+ 1
I got you wrong the first time so i deleted my previous post. The only way to do a line break in the console is to do it after the cout statement because cout stands for Console Output. In simple words you say the computer "Give a blank output" - The endl or '\n' operators translate these human words/thoughts simply to the computer. To break a line without anything you need at least to type: cout << '\n'; I hope this helps a bit :) P.S.: I've found out that " cout << '\n'; " doesn't work here in the code playground.. If you use this in a compiler like Code::Blocks it works. To do it on sololean code playground you have to type cout << '\n'<<endl; or cout <<""<<endl;
14th Dec 2016, 3:40 PM
C S
C S - avatar
0
sorry I can't understand what do you mean by this paragraph " In simple words you say the computer "Give a blank output" - The endl or '\n' operators translate these human words/thoughts simply to the computer". I do know that endl and \n are line breakers but the that I can't reach or understand is that if they are line breakers why don't we use them after typing the variables ??and how when we write👇 # include <iostream> using namespace std; int main () how the program make a newline or breaks the line alone ???? thanks a lot for helping me 😊😊
14th Dec 2016, 7:17 PM
NadaKarim
0
With the "blank output" I mean: If you would type: cout<<"Text1"; cout<<"Text2"; It will show up as: Text1Text2 If you type cout<<"Text1"<<endl; cout<<"Text2"<<endl; You give the computer the streamoperator "endl" which stands for "end of line" so now the computer knows that it's output is Text1 Text2 Not like above both strings in one line. ------------------------------------------------ For your variable question: I hope I get you right in this point, cause I'm not quite sure what you mean with "why don't we use them after typing the variables" You don't have to type "endl" when you give input or set the variables, you can break the lines within the code simply like in any texteditor # include <iostream> using namespace std; int main () int intVar1 = 0; double dblVar1 = 0; ... etc. In this case you don't need to tell the computer "this is the end of the line" because you say with the ";" it's the end of the line of code. The "endl" streamoperator is only necessary, if you want to show something to the user in the console and break a line in the output. ------------------------------------------------ To the question "how the program make a newline or breaks the line alone ????" - I hope I get you right in this point: I am new to C++, too. But as far as I know you can't break a line automaticly. To break a line alone you can use the statement " cout<<'\n'; ".
15th Dec 2016, 8:43 AM
C S
C S - avatar