Thread: writing a Multi-Line string

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    4

    writing a Multi-Line string

    What is the best way to write a few lines of strings together?

    i.e.

    Line2
    Line2
    Line3


    I attempted it this way but it doesn't seem to work.

    Code:
    printf ( "Line1" );
    printf ( "Line2" );
    printf ( "Line3" );

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    printf("Line1\nLine2\nLine3\n");
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to account for the newlines, e.g.,
    Code:
    puts("Line1\nLine2\nLine3");
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Lemme guess: "Line1Line2Line3"

    printf doesn't add a newline('\n'). You have to include it, or them:
    Code:
    printf ( "Line1\n" );
    printf ( "Line2\n" );
    printf ( "Line3\n" );
        
        or
    
    printf ( "Line1\nLine2\nLine3\n" );
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading a string of hex values from command line
    By peeweearies in forum C Programming
    Replies: 7
    Last Post: 02-24-2009, 02:25 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  4. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM