1
votes

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!

4
Strings don't have a size limit (at least, not one as small as 100, there might be an implementation limit in the megabytes). The bug is elsewhere in your code. Write a short program which demonstrates the problem, and post that. Is your terminal 100 characters wide? The truncation might be happening on output (although I don't know what terminals truncate instead of wrapping). - Steve Jessop
The string shouldn't be cut off. How are you examining/displaying it? - Mike Seymour
How do you know it is cut off? Did you print it out, or looked in a debugger (which one?)? - Nemanja Trifunovic
oops, of course it was just the debugger cutting it off (xcode). I'm just getting started with xcode/c++, so thanks a lot for the quick replies. Steve, please write your comment in an answer so i can accept it, the "Is your terminal 100 characters wide?" make me go "oops". - iddqd
it's OK, you're allowed to just answer your own question, or edit the question to say that it's solved. My comment was a comment because it's all questions and "maybes", I'm glad to have helped but I don't claim to have actually answered the question. Excellent username, by the way, didn't recognise it until I was typing it. - Steve Jessop

4 Answers

1
votes

Of course it was just the debugger cutting it off (xcode). I'm just getting started with xcode/c++, so thanks a lot for the quick replies.

0
votes

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!

0
votes

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?

0
votes

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".