How to get new line?

Hello, complete noob here.
Basically i'm following the tutorial, and when i run the code i get an answer 57, but in the same line. How to make that it prints in second line?
Appreciate the help !

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "stdafx.h"
#include <iostream>

// void means the function does not return a value to the caller
void returnNothing()
{
	// This function does not return a value so no return statement is needed
}

// int means the function returns an integer value to the caller
int return5()
{
	return 5; // this function returns an integer, so a return statement is needed
}

int main()
{
	std::cout << return5(); std::endl; // prints 5 // Why std::endl; doesn't work?
	std::cout << return5() + 2; // prints 7

	returnNothing(); // okay: function returnNothing() is called, no value is returned
	return5(); // okay: function return5() is called, return value is discarded

	//std::cout << returnNothing(); // This line will not compile.  You'll need to comment it out to continue.

	return 0;
}
closed account (E0p9LyTq)
std::endl needs to be inserted into the output stream.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

// void means the function does not return a value to the caller
void returnNothing()
{
   // This function does not return a value so no return statement is needed
}

// int means the function returns an integer value to the caller
int return5()
{
   return 5; // this function returns an integer, so a return statement is needed
}

int main()
{
   std::cout << return5() << std::endl;
   std::cout << return5() + 2 << "\n";

   returnNothing(); // okay: function returnNothing() is called, no value is returned
   return5(); // okay: function return5() is called, return value is discarded

   return 0;
}
also small tip
using namespace std;
or safer option:
1
2
3
using std::cout;
using std::cin;
using std::endl;

and then you can just use
1
2
 cout << "small tip" << endl; 
cin >> something;


saves the time
Last edited on
closed account (E0p9LyTq)
@EloMelo,

VERY bad advice, especially with using namespace std;. If typing a few extra characters is too much an effort, then expect LOTS of wasted time trying to track down linker errors when dealing with libraries other than the C++ standard library, and/or multiple included header files.

http://www.lonecpluspluscoder.com/2012/09/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/
@FurryGuy
yeah i know, thats why i added safer option
closed account (E0p9LyTq)
Even specifying a single use is still a potential cause of problems later. I had linker/compile areas I couldn't track down and fix....

until I stopped using the "time-saver" using statements. Both forms.
Thanks for your time FurryGuy!
Interesting, i thought << std::endl; was all you needed.

std::cout << return5() << std::endl; // std::endl; end of the line i assume.
std::cout << return5() + 2 << "\n";
std::cout << return5() << "\n";

Thank you
Last edited on
closed account (E0p9LyTq)
std::endl is the C++ newline object, '\n' (or "\n") is a C-style escape code.

I use std::endl if I need a single newline. I use the C escape code if I want multiple newlines or if I want a newline at the start and/or end of a string output.

1
2
std::cout << 5 << std::endl;
std::cout << "\nHello World!\n\n";


A list of escape codes:

http://www.cplusplus.com/doc/tutorial/constants/

There are some i/o considerations whether to use std::endl or "\n":

http://www.cplusplus.com/forum/beginner/7350/
Thank you both for input.
I did noticed that some people like to use using namespace std; as its seems takes less characters to type. Initially i also liked the idea but upon reading more into it, decided against it.

P.S like Furry pointed out it really doesn't take that long to write couple more characters:)
Topic archived. No new replies allowed.