can "endl" be changed to end line twice?

Pages: 12
closed account (1CfG1hU5)
 
cout << "this is a sample text " << endl << endl;


int t;

t = endl+endl; ?

looking for a single endl define to end line twice.
std::string doubleDown = "\n\n";
Last edited on
You can define your own version of endl if you like.
1
2
3
std::ostream& my_endl(std::ostream& out) {
    return out << "\n\n" << std::flush;
}

This should work for ostreams, though I haven't tested it. You could modify the function to be more general by having it work on a basic_ostream, I think.
closed account (1CfG1hU5)
useful the "\n" in the language still. could not define "endl" to do same.

is there a way?
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

struct double_endl_impl {} double_endl;

std::ostream& operator<<(std::ostream& out, double_endl_impl)
{
    return out << "\n\n" << std::flush;
}

int main()
<%
	std::cout << '1' << double_endl << '2';
%>
1

2
\n is the proper way to end a line. If you look up the definition of endl it's actually \n followed by std::flush.

from GCC:
1
2
3
4
  template<typename _CharT, typename _Traits>
    inline basic_ostream<_CharT, _Traits>&
    endl(basic_ostream<_CharT, _Traits>& __os)
    { return flush(__os.put(__os.widen('\n'))); }


Which is pretty similar to:
1
2
3
4
std::ostream& endl(std::ostream& os)
{
  return std::flush( os.put( os.widen('\n') ) );
}


Which is a non-operator way to do:
1
2
3
4
std::ostream& endl(std::ostream& os)
{
  return os << '\n' << std::flush;
}

@jt1

This works for me..

1
2
3
4
5
6
7
8
9
10
// Define after #include's
//#define endline "\n\n"; // Put as many \n as you need.
//Corrected by removing semicolon, as mentioned by Disch
#define endline "\n\n" // Put as many \n as you need.
// ... 

//Now in int main()
cout << "this is a sample text " << endline;
cout << "Next printed line.." << endline;
...



Last edited on
Don't end your #defines with a semicolon.

 
cout << "This will fail" << endline << "because you added a semicolon";




Also... don't use #defines.
closed account (1CfG1hU5)
a #define or std::string define or string with using namespace std; before main looks like the best way to save lines of text.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

struct endl_n_ { explicit constexpr endl_n_( int n = 2 ) noexcept : cnt(n) {} ; const int cnt ; };

template < typename C, typename T >
std::basic_ostream<C,T>& operator<< ( std::basic_ostream<C,T>& stm, endl_n_ en )
{
    for( int i = 0 ; i < en.cnt ; ++i ) stm.put( stm.widen('\n') ) ;
    return stm.flush() ;
}

constexpr endl_n_ endln( int c ) noexcept { return endl_n_(c) ; }

template < typename C, typename T >
std::basic_ostream<C,T>& endl2( std::basic_ostream<C,T>& stm ) { return stm << endln(2) ; }

int main()
{
    std::cout << 'a' << endl2 << 'b' << endln(3) << 'c' << '\n' ;
    std::wcout << L'd' << endl2 << L'e' << endln(3) << L'f' << L'\n' ;
}

http://coliru.stacked-crooked.com/a/0c8efc287f28d398
Method 1: Create a simple stream manipulator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

template <typename CharT, class Traits>
inline
std::basic_ostream <CharT, Traits> & 
end2( std::basic_ostream <CharT, Traits> & outs )
{
  return outs << '\n' << '\n' << flush;
}

int main()
{
  cout << end2 << "Hello" << end2 << "world" << endl;
}

Method 2: Create a better stream manipulator that takes an argument:

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
28
29
30
31
#include <iostream>
using namespace std;

struct endn
{
  unsigned n;
  endn( unsigned n = 1 ): n( n ) { }
};

template <class CharT, class Traits>
inline
std::basic_ostream <CharT, Traits> &
operator << ( std::basic_ostream <CharT, Traits> & outs, const endn& e )
{
  unsigned n = e.n;
  while (n--) outs << '\n';
  return outs.flush();
}

// (feel free to overload stuff)
inline
endn
endl( unsigned n )
{
  return endn( n );
}

int main()
{
  cout << endn(2) << "Hello" << endl(3) << "world" << endl;
}

All the template stuff is so that it works with any STL stream class (like wcout).

Enjoy!
Last edited on
closed account (1CfG1hU5)
#define no good with ; at end. same with some older languages i have.

#define DECIMAL_VALUE 114
#define HEX_VALUE 0x72
#define CHAR_VALUE 'r'

all 3 are for char 'r'

Consider
1
2
3
constexpr char CHAR_VALUE = 'r' ; 
constexpr int DECIMAL_VALUE = CHAR_VALUE ; // 114 
constexpr unsigned int HEX_VALUE = DECIMAL_VALUE ; // 0x72 
closed account (1CfG1hU5)
the constexpr is inside main?
The constexpr is like using const, except that the value is usable by the compiler itself instead of just by your compiled program. In the interest of being as specific and useful as possible, JLBorges made them constexpr instead of just const.
closed account (1CfG1hU5)
since constexpr statements end in semicolon, can believe are inside main or a function with
or without prototype.
Those statements are declarations. They can be at global scope, or any other scope.

(EDIT: Of course, global variables are rarely a good way to solve a problem...)
Last edited on
closed account (1CfG1hU5)
.....
Last edited on
Where did you see that? If you're talking about Disch's correction, that has nothing to with globals, as #defines are preprocessor directives.
http://www.cplusplus.com/doc/tutorial/preprocessor/
This may shed some light on the difference.
closed account (1CfG1hU5)
.....
Last edited on
Pages: 12