Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

AravindG

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 ! :)

  • one year ago
  • one year ago

  • This Question is Closed
  1. AravindG
    Best Response
    You've already chosen the best response.
    Medals 0

    @Opcode

    • one year ago
  2. AravindG
    Best Response
    You've already chosen the best response.
    Medals 0

    how is endl useful when working with files ?

    • one year ago
  3. Opcode
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

    • one year ago
  4. AravindG
    Best Response
    You've already chosen the best response.
    Medals 0

    well then whats the difference between "\n" and endl ?

    • one year ago
  5. Opcode
    Best Response
    You've already chosen the best response.
    Medals 1

    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.)

    • one year ago
  6. AravindG
    Best Response
    You've already chosen the best response.
    Medals 0

    but what do they say abt the buffer part ?

    • one year ago
  7. AravindG
    Best Response
    You've already chosen the best response.
    Medals 0

    why is cleaning buffer necessary ?

    • one year ago
  8. Opcode
    Best Response
    You've already chosen the best response.
    Medals 1

    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)

    • one year ago
  9. AravindG
    Best Response
    You've already chosen the best response.
    Medals 0

    so when all should i use endl when working with files ?

    • one year ago
  10. AravindG
    Best Response
    You've already chosen the best response.
    Medals 0

    i usually us \n for my programs

    • one year ago
  11. Opcode
    Best Response
    You've already chosen the best response.
    Medals 1

    \n doesn't flush output buffer... endl does.

    • one year ago
  12. Opcode
    Best Response
    You've already chosen the best response.
    Medals 1

    So if your using files I'd use endl. But otherwise I'd go with \n

    • one year ago
  13. AravindG
    Best Response
    You've already chosen the best response.
    Medals 0

    flushing of buffer increases speed?

    • one year ago
  14. Opcode
    Best Response
    You've already chosen the best response.
    Medals 1

    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."

    • one year ago
  15. AravindG
    Best Response
    You've already chosen the best response.
    Medals 0

    Question 2: My following code always gives o/p "as file does not exist" any solution ?

    • one year ago
  16. AravindG
    Best Response
    You've already chosen the best response.
    Medals 0

    #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(); }

    • one year ago
  17. rsmith6559
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

    • one year ago
  18. AravindG
    Best Response
    You've already chosen the best response.
    Medals 0

    @Opcode ....

    • one year ago
  19. AravindG
    Best Response
    You've already chosen the best response.
    Medals 0

    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(); }

    • one year ago
  20. AravindG
    Best Response
    You've already chosen the best response.
    Medals 0

    @Opcode

    • one year ago
  21. AravindG
    Best Response
    You've already chosen the best response.
    Medals 0

    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 ?

    • one year ago
  22. Opcode
    Best Response
    You've already chosen the best response.
    Medals 1

    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; } ```

    • one year ago
  23. AravindG
    Best Response
    You've already chosen the best response.
    Medals 0

    i dunno is that the method ?

    • one year ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.