EXAM PREPARATION DOUBTS : endl-Inserts a new-line character. Additionally, for buffered streams, endl flushes the buffer (i.e. writes all unwritten characters in the buffer to the output sequence,) .. why do we need to flush the buffer can i get an example so that I understand the use of endl ?Is endl a function .If yes what is its header file ? Its my final lab exam today ! Pray for me ! :)
@Opcode
how is endl useful when working with files ?
How do I explain this... Okay. So endl means end line and it renders the output so the next output will start from a new line. ``` int main() { cout << "I sleep." << endl; cout << "On a bed."; return 0; } Output: I sleep. On a bed. Now if you removed endl and your code looked like: int main() { cout << "I sleep. "; cout << "On a bed."; return 0; } Output: I sleep. On bed. ``` So you can think of endl like the enter key to make a new line.
well then whats the difference between "\n" and endl ?
Silly :3 endl is only for C++. \n is for C you use the \n in printf to do the same thing. (Make the output look the same.)
but what do they say abt the buffer part ?
why is cleaning buffer necessary ?
Flushing the buffer is necessary, because the user will see the output written to the stream immediately. Output is generally buffered before it's written to the intended device. That way, when writing to slow to access devices(like files), it doesn't have to access the device after every single character. Flushing means emptying the buffer and actually writing it to the device. Check this: http://stackoverflow.com/questions/4751972/c-endl-and-flushing-the-buffer Read: (Benjamin Lindley's answer)
so when all should i use endl when working with files ?
i usually us \n for my programs
\n doesn't flush output buffer... endl does.
So if your using files I'd use endl. But otherwise I'd go with \n
flushing of buffer increases speed?
Somewhat. And especially when working with files. "That way, when writing to slow to access devices(like files), it doesn't have to access the device after every single character."
Question 2: My following code always gives o/p "as file does not exist" any solution ?
#include<fstream.h> #include<conio.h> #include<process.h> void main() { char f1[20],f2[20]; char ch; cout<<"\n\nEnter original file name:"; cin>>f1; cout<<"\nFile copy:"; cin>>f2; ifstream in(f1); ofstream out(f2); if(in.fail()) { cout<<"File does not exist "; exit(1); } while(!in.eof()) { in>>ch; out<<ch; } in.close(); out.close(); getch(); }
std::endl is more portable than hard coding '\n'. Even after all these years Unix uses a linefeed, Windows uses carriage return line feed together and some Mac GUIs still use carriage returns to end a line. std::endl is probably defined as whatever the OS specifies for ending a line.
@Opcode ....
urgent :can u identify error in my program below its not working either #include<iostream.h> #include<conio.h> class distance { int m,cm; public: void read(); void operator +(distance); void display(); }; void distance::read() { cout<<"\n\nEnter distances:"; cin>>m>>cm; } void distance::display() { cout<<"\n distances:"; cout<<m<<cm; } void distance::operator +(distance d1) { distance temp; temp.cm=cm+d1.cm; temp.m=m+d1.m; if(temp.cm>=100) { temp.m+=1; temp.cm=(temp.cm%100); } } void main() { distance d3,d4,d5; d3.read(); d4.read(); d5=d3+d4; d5.display(); getch(); }
@Opcode
well my teacher told me instead of using for loop to input data to file char c[200] outfile out; for(i=0;c[i]!='\0';i++) { cin>>c[i]; out<<c; } you should use getline so that the length is not limited to 200 .What did she mean by that?I mean how we use getline like that ?
Wait. So like: ``` #include <iostream> using namespace std; int main() { string input; while (true) { cout << "Type your input: "; while (getline(cin, input)) cout << input << endl; } return 0; } ```
i dunno is that the method ?
Join our real-time social learning platform and learn together with your friends!