transactionInfo = date + " - " + _strtime( time ) + "New Customer - 1001 - Olly";

Ok so I've been trying to find it everywhere in C++ reference guides but it seems I can't do the above ^^ (says I cannot add 2 pointers).

date is a char[10] and time is char[9], plus the other 2 stringy bits.

I know + can be used as an operator with strings (but usually like string1 += string2) or whatever but not as I want it to be used...

Anyone? :)

Recommended Answers

All 6 Replies

If transactionInfo is a std::string, you could do something like:

transactionInfo = static_cast<std::string>(date) + " - "+ _strtime( time ) + "New Customer - 1001 - Olly";

As you can see time is already used in C++, so you might want to use another variable name. Also _strtime() is deprecated, use _strtime_s() instead. (that sounded like Visual Studio..)

Did the bottom 2 things:

#include <string>

string transactionInfo;
char currentDate[10]; // cin >> current Date = 01/10/2008
char currentTime[9];

transactionInfo = currentDate + " - " + _strtime_s( currentTime ) + "New Customer - 1001 - Olly";

Thats what I have ^^ stumped. I know its because I am trying to put a char array into a string, but no idea how to go about it.

Fortunately, it's so simple:

std::string s;
    s = "Base ";
    s += "next item. ";
    s += "The end.";

Fortunately, it's so simple:

std::string s;
    s = "Base ";
    s += "next item. ";
    s += "The end.";

Thanks.

You say so simple... Simple would mean it should allow me to do as I have done. As its what I have done, but in multiple lines.
Pretty lame you can't do it on one line. :)

Why?

using std::string;
    string s;
    s = string("Base ")+string("next item. ")+string("The end.");

I prefer to do it step by step.

Use stringstream if you want to append values to a string buffer on one line then extract the string later.

#include <iostream>
#include <sstream>
#include <string>

using std::stringstream;
using std::string;
using std::cout;
using std::cin;
using std::endl;
using std::flush;

int main(){

    stringstream ss (stringstream::in | stringstream::out);
    string testing = "testing!";
    ss << "Testing " << 123 << testing;
    string result = ss.str();
    cout << result << endl;
    cin.get();
    return 0;
}

I think this may also be possible with string buffer class, though I haven't experimented with it yet @_@.

-Alex

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.