Results 1 to 4 of 4

Thread: VC++ variables inside strings

  1. #1
    Lively Member
    Join Date
    Apr 01
    Location
    UK - Liverpool
    Posts
    113

    VC++ variables inside strings

    Hi I'm just slowly getting to grips with C++ after years of clinging to VB. I seen a bit of code yesterday which looked something like:

    cout << "Total Cost is %i% pounds", Total_Cost;

    Where %i% inside the string is replace with the integer Total_Cost. As it turns out thats exactly what I need! I'm using a large string with many veriables inside, it's going to be a nightmare to manually split it all up. If I've typed the above code correctly, and people reconise it can you point me to where I can learn more?

    Cheers
    My software never has bugs. It just develops random features...

  2. #2
    Member
    Join Date
    May 08
    Posts
    36

    Re: VC++ variables inside strings

    lol I just saw this on Yahoo answers.

    To seperate variables from "literal stuff" you use <<'s

    Code:
    string name,language;
    name = "MountainDew";
    language = "C++";
    cout << "Hi I'm " << name << " and I'm coding in " << language << ".";
    //HI I'm MountainDew and I'm coding in C++.

  3. #3
    Moderator
    Join Date
    Jan 05
    Location
    Sydney
    Posts
    13,615

    Re: VC++ variables inside strings

    It should be noted that the << operator (with respect to std::cout) does not mean exactly "separate variables from literal stuff" but rather to write the right-hand side to the left-hand side (being stdout in this case).

    In a more general sense, the joining of strings is called concatenation, and std::string (like the string classes in various other languages) defines the + operator for this purpose:
    Code:
    std::cout << "Hi, my name is " + name + " and I'm using " + language + "." << std::endl;
    Note that this cannot be applied to two operands which are string literals, because the string literal represents a char array and not a std::string object:
    Code:
      $ cat test2.cpp
    #include <string>
    
    int main()
    {
      std::string test = "Hello " + "World";
      return 0;
    }
    
      $ g++ test2.cpp -o test2
    test2.cpp: In function ‘int main()’:
    test2.cpp:5: error: invalid operands of types ‘const char [7]’ and ‘const char [6]’ to binary ‘operator+’
    In this respect, the << operator is nice because it accepts anything that has a string representation.

  4. #4
    Noodly Appendage wossname's Avatar
    Join Date
    Aug 02
    Location
    #!/bin/bash
    Posts
    5,645

    Re: VC++ variables inside strings

    Quote Originally Posted by T0MMY
    I seen a bit of code yesterday which looked something like:
    cout << "Total Cost is %i% pounds", Total_Cost;
    I've not come across that coding style in C++ before but it looks very reminiscent of the way the printf() function works. Here's the equivalent printf() call...

    Code:
    printf("Total Cost is %d pounds\n", Total_Cost);
    So I'd advise you to use printf() instead if you are hell-bent on this placeholder method of string catting.

    In C++, I only ever use printf for quick and dirty debugging statements because the syntax is simpler and also you can rapidly find all your debugging-only statements later on by searching for " printf(" globally. Otherwise I use the awkward << syntax for outputting strings because it handles class types so well. There's nothing wrong with using printf() really but many people tend to frown upon it's use in C++ through a belief that it is somehow evil.
    I don't live here any more.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •