Thread: Concatenation in C++

  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,793
    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,793
    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, 10: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, 06:47 PM
  4. queue concatenation
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 04-02-2002, 06:35 AM
  5. integer concatenation with string again...
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-11-2002, 06:36 PM