can "endl" be changed to end line twice?

Pages: 12
Oct 13, 2014 at 12:54am
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.
Oct 13, 2014 at 1:17am
std::string doubleDown = "\n\n";
Last edited on Oct 13, 2014 at 1:19am
Oct 13, 2014 at 2:46am
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.
Oct 13, 2014 at 5:15pm
closed account (1CfG1hU5)
useful the "\n" in the language still. could not define "endl" to do same.

is there a way?
Oct 13, 2014 at 5:25pm
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
Oct 13, 2014 at 5:25pm
\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;
}

Oct 13, 2014 at 5:31pm
@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 Oct 13, 2014 at 5:44pm
Oct 13, 2014 at 5:41pm
Don't end your #defines with a semicolon.

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




Also... don't use #defines.
Oct 13, 2014 at 9:50pm
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 Oct 13, 2014 at 10:12pm
Oct 14, 2014 at 1:05am
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
Oct 14, 2014 at 1:25am
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 Oct 14, 2014 at 1:26am
Oct 14, 2014 at 6:53am
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'

Oct 14, 2014 at 1:57pm
Consider
1
2
3
constexpr char CHAR_VALUE = 'r' ; 
constexpr int DECIMAL_VALUE = CHAR_VALUE ; // 114 
constexpr unsigned int HEX_VALUE = DECIMAL_VALUE ; // 0x72 
Oct 14, 2014 at 5:11pm
closed account (1CfG1hU5)
the constexpr is inside main?
Oct 14, 2014 at 6:59pm
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.
Oct 14, 2014 at 10:25pm
closed account (1CfG1hU5)
since constexpr statements end in semicolon, can believe are inside main or a function with
or without prototype.
Oct 14, 2014 at 11:35pm
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 Oct 14, 2014 at 11:36pm
Oct 14, 2014 at 11:53pm
closed account (1CfG1hU5)
.....
Last edited on Oct 15, 2014 at 3:53am
Oct 15, 2014 at 12:05am
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.
Oct 15, 2014 at 12:59am
closed account (1CfG1hU5)
.....
Last edited on Oct 15, 2014 at 3:52am
Pages: 12