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 !
#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;
}
#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;
}
VERY bad advice, especially with usingnamespace 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.
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.
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:)