text output?

This is a discussion on text output? within the C++ Programming forums, part of the General Programming Boards category; i am wondering if there is a better (easier) way of outputting a large amount of text? Let’s say i ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    7

    text output?

    i am wondering if there is a better (easier) way of outputting a large amount of text? Let’s say i have a drink menu that tells the user about the drink and how to make it? Each case statement would have about 200 - 300 words? Do i have to put them all in cout statements in order to print them?
    Last edited by jorel43; 07-13-2010 at 08:34 PM. Reason: amount clarity

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    No. Just save things in a string variable and print that.

    Code:
    std::string menu( "..." );
    
    // ...
    
    std::cout<< menu; 
    
    // ...
    
    std::cout<< menu;
    Alternatively, you could #define the menu:

    Code:
    #define MENU "coke\n"\
    "water"
    
    // and print as before
    And a third option would be to load from file if it's especially long.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    7
    thanks for response. if i put my text in a string, how would i be able to format it? you know, keep spaces and stuff?

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    7
    oh and one more thing, if i read from a file, i still have to format it right? i mean it would come up as the same thing as a string?

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    7
    basically this is a smple of my string (text in french), i would like it to look like that when it is outputted (or nicer) ? i hope this helps to understand what i am looking to do better, thanks.


    Code:
    string Drinks = "# 1"
        "Mai Tai"
        "Il est temps de montrer comment faire et que j'apprecie la meilleure boisson que vous aurez jamais ..."
        "Il est connu comme un Mai Tai et est une boisson delicieuse tropicale qui"
        "est parfait pour toute occasion, sous le soleil .. Enjoy;)"
        "Ce qu'il vous faut:" 
        " "
        "Light Rhum, rhum brun (je recommande Myers rhum brun), triple sec, jus d'ananas, jus d'orange, melange aigre-doux, grenadine," 
        "jus de lime. Cherry et tranche d'ananas sont garnitures facultatives. En outre, un choix de parapluie belle dent est esthetiquement agreable." ;

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    You'll have to use "control characters".

    Eg. '\n' for newline. "first line\nsecond line".
    '\t' for tab.

    You can split it into multiple lines (with multiple quotes) like you have done, but the compiler will just treat them as one huge string. So you'll still need '\n's.
    Code:
        "# 1"
        "\tMai Tai\n"
        "\tIl est temps de montrer comment faire et que j'apprecie la meilleure boisson que vous aurez jamais ...\n"
        "\tIl est connu comme un Mai Tai et est une boisson delicieuse tropicale qui\n"
        "\test parfait pour toute occasion, sous le soleil .. Enjoy;)\n"

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    2,698
    If your file is formatted properly then you can read the data in with getline() and all the formatting should remain the same.

  8. #8
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    I think I see your problem.
    So you want a string that half ways through migh, for example, say how much money you have left?
    The easiest way to do this would be to write a function for it... something like:

    Code:
    #include <string>
    #include <iostream>
    
    #include <boost/lexical_cast.hpp>
    
    const std::string barMenu( double moneyLeft ) {
      return std::string() +
        "one\n" +
        "two\n" +
        "Money left: " + boost::lexical_cast<std::string>(moneyLeft) + "\n"
        "Choice.\n";
    }
    
    
    int main( void ) {
      std::cout<< barMenu(10.50F) << "\n"; 
    
      return 0;
    }
    If you don't have boost installed consider using stringstreams.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 02:17 PM
  2. text output in text editor
    By realjag in forum C++ Programming
    Replies: 5
    Last Post: 10-29-2005, 06:51 PM
  3. Replies: 3
    Last Post: 05-25-2005, 01:50 PM
  4. sorting characters and output to text file
    By odb1 in forum C++ Programming
    Replies: 1
    Last Post: 10-10-2003, 04:46 PM
  5. Recording console window output to a text file?
    By HolySmiter in forum C++ Programming
    Replies: 3
    Last Post: 02-24-2002, 02:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21