views:

125

answers:

3

I initialize a string as follows:

std::string myString = "'The quick brown fox jumps over the lazy dog' is an English-language pangram (a phrase that contains all of the letters of the alphabet)";

and the myString ends up being cut off like this:

'The quick brown fox jumps over the lazy dog' is an English-language pangram (a phrase that contains

Where can i set the size limit? I tried the following without success:

std::string myString;
myString.resize(300);
myString = "'The quick brown fox jumps over the lazy dog' is an English-language pangram (a phrase that contains all of the letters of the alphabet)";

Many thanks!

A: 

Try the following (in debug mode):

assert(!"Congratulations, I am in debug mode! Let's do a test now...")
std::string myString = "'The quick brown fox jumps over the lazy dog' is an English-language pangram (a phrase that contains all of the letters of the alphabet)";
assert(myString.size() > 120);

Does the (second) assertion fail?

Daniel Daranas
You might try that in debug or the assert call will be just ignored by your compiler.
ereOn
@ereOn Yes. I added an additional test to test that we're in debug mode.
Daniel Daranas
A: 

Are you sure?

kkekan> ./a.out 
'The quick brown fox jumps over the lazy dog' is an English-language pangram (a phrase that contains all of the letters of the alphabet)

There is no good reason why this should have happen!

Kedar
A: 

When printing, or displaying text, the output machinery buffers the output. You can tell it to flush the buffers (display all remaining text) by output a '\n' or using std::endl or executing the flush() method:

#include <iostream>
using std::cout;
using std::endl;

int main(void)
{
  std::string myString =
    "'The quick brown fox jumps over the lazy dog'" // Compiler concatenates
    " is an English-language pangram (a phrase"     // these contiguous text
    " that contains all of the letters of the"      // literals automatically.
    " alphabet)";
  // Method 1:  use '\n'
  // A newline forces the buffers to flush.
  cout << myString << '\n';

  // Method 2:  use std::endl;
  // The std::endl flushes the buffer then sends '\n' to the output.
  cout << myString << endl;

  // Method 3:  use flush() method
  cout << myString;
  cout.flush();

  return 0;
}

For more information about buffers, search Stack Overflow for "C++ output buffer".

Thomas Matthews