EOSERV Forum > Lounge > newline in C++
Topic is locked.
Page: << 1 >>
newline in C++
Author Message
Post #60656 newline in C++

I'm making a program in C++ but i cant figure out how to put in a newline. i've tried;


cout << "BlahBLah /n";


cout << "BlahBlah" /n;


and acouple more.

14 years, 13 weeks ago
Post #60659 Re: newline in C++

cout << "Hello" << endl;

---
Love you too.
14 years, 13 weeks ago
Post #60660 Re: newline in C++

Well I am not sure what you are asking for but here...

#include <windows.h>
#include <string>
#include <iostream> //Allow us to use 'cout' and 'cin'

using namespace std; //Automatically add std:: before 'cout' and 'cin'

int main() {
system("CLS");
Sleep(1000);
cout << "Program closes in 3 secounds" << endl;
Sleep(1000);
cout << "3" << endl;
Sleep(1000);
cout << "2" << endl;
Sleep(1000);
cout << "1" << endl;
Sleep(1000);
cout << "Terminating Program...." << endl;
Sleep(1000);

return 0;

14 years, 13 weeks ago
Post #60661 Re: newline in C++

Newline is \n. not /n.

std::cout << "Hello world!" << endl;

-or-

std::cout << "Hello world!\n";

Both do the same thing. Using a backslash (\) is a universal escape character, so if you also want to print a ", you can do:

std::cout << "\"Hello world!\"" << endl;

---
class EOSERV {
Programmer | Oldbie
Open source EO Client: https://github.com/ethanmoffat/EndlessClient
};
14 years, 13 weeks ago
Post #60662 Re: newline in C++

Thanks for correcting me :)

14 years, 13 weeks ago
Post #60663 Re: newline in C++

Meh, I have never been accustomed to the "n" thing. endl was easier for me haha.

---
Love you too.
14 years, 13 weeks ago
Post #60664 Re: newline in C++

Lol, everyone was racing to correct him.. lol


and ethan why not just tell him to use

using namespace std;

it puts the std:: in front of all the "cout" and "cin".

14 years, 13 weeks ago
Post #60666 Re: newline in C++

i already am using namespace std;


thats why i didn't include std::

14 years, 13 weeks ago
Post #60667 Re: newline in C++

std::endl is not the same thing as \n. endl implicitly flushes the stream, so it is equivalent to << '\n' << std::flush;

This is important if you're going to be writing many lines, as flushing the stream is rather costly; but for console I/O you typically want to flush every line immediately anyway.

14 years, 13 weeks ago
Page: << 1 >>
Topic is locked.
EOSERV Forum > Lounge > newline in C++