Concatenation in C++

This is a discussion on Concatenation in C++ within the C++ Programming forums, part of the General Programming Boards category; Hello, I want to convert this java statement to C++: String a; int b=1; double c=2.32; String d="abc"; a=b + ...

  1. #1
    Unregistered
    Guest

    Concatenation in C++

    Hello,

    I want to convert this java statement to C++:

    String a;
    int b=1;
    double c=2.32;
    String d="abc";

    a=b + c + d;

    Thank you very much!

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    An easy way is sprintf()
    char a[] = "These numbers to follow";
    int b=1;
    double c=2.32;
    char d[255];

    sprintf(d,"%s%i%f",a,b,c);
    cout << d;

  3. #3
    Unregistered
    Guest
    Thank you very much.
    Can sprintf support string?

    May I change to:

    string a= "These numbers to follow";
    int b=1;
    double c=2.32;
    string d;

    sprintf(d,"%s%i%f",a,b,c);
    cout << d;

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    The main language doesnt have a string data type. You can have a wrapper class with the operators overloaded to mimic a string, but the data of this type is always held in a charector array as shown above.

    I wouldnt bother with string wrappers to much at this stage, leave them for when your happier with the C language.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    1

    Yes, we have strings

    ANSI C++ has a string data type ready to use. Besides, we also have string streams, that would fit exactly what you want to do. Here is a sample:

    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    int main()
    {
    	char a[] = "These numbers to follow"; 
    	int b=1; 
    	double c=2.32; 
    	char d[255]; 
    	ostringstream oss;
    
    	oss << a << b << c;
    	cout << oss.str() << endl;
    	
    	// Optionally ...
    	string e = oss.str();
    	// Play with string e here before printing out
    	cout << e << endl;
    }
    Rayon

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Concatenation Using Pointers No strcat()
    By oviang in forum C Programming
    Replies: 4
    Last Post: 12-07-2007, 09:31 AM
  2. printing arrays with concatenation
    By derek23 in forum C Programming
    Replies: 1
    Last Post: 07-17-2005, 03:02 AM
  3. concatenation
    By F*SH in forum C++ Programming
    Replies: 34
    Last Post: 11-13-2002, 05:47 PM
  4. queue concatenation
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 04-02-2002, 05:35 AM
  5. integer concatenation with string again...
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-11-2002, 05:36 PM

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