Member Avatar for Oritm

Hello all,

I'm used to java programming and php. I'm trying to learn C++, but it's rather difficult for me.

Basic in java:

String message = "Test";
String temp = "This is a " + message;

temp now contains "This is a Test".


In c++ i tried everything, a + a dot (in php) etc.
Nothing seems to work.
I tried to search google but no results.

What i do know is i can use cout:

cout << "hello " << "world" << somevariable;

but i want to store my variable not to print it.

Thanks!

Recommended Answers

All 4 Replies

Its done almost like you posted it. Post what you have tried so that we can see what you might be doing wrong. You have to include <string>, and its lower-case "string", not "String". C++ is case sensitive, so you have to be careful about using capital letters.

In c++ i tried everything, a + a dot (in php) etc.
Nothing seems to work.

#include<string>

:)

Ciao,
Angelo

Member Avatar for Oritm

I also included time.h and string.

string Clogger::getTimeStamp()
{

	char dateStr [9];
	char timeStr [9];

	_strdate_s( dateStr);
	_strtime_s( timeStr );
	
	return dateStr + timeStr;
}

This doesnt work.. it's complaining about pointers..

edit: forgot the formatting

In C++ there are multiple varieties of strings. C style strings are null terminated char arrays. dateStr and timeStr in post #4 are probably going to be used as null terminated char arrays. Null terminated char arrays cannot be concatenated using the + sign. They must be concatenated using strcat(), or similar function, which I believe is in the header file called cstring, or string.h if you have an outdated compiler. cstring also has other functions that can manipulate C style strings. The standard string class declared and defined by the header file called string is part of the Standard Template Library. STL string objects are not nulll terminated char arrays. They can be concatenated using the + operator.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.