-4

i have problem with multi strings in 1 char

//------------------------------------------------
char* P1P2P3 = { FirstName,LastName,Email };
    if (strcmp(buffer5, P1P2P3) == 0)
          {
           //OK
          }
    else
          {
           //Not OK
          }
//----------------------------------------------

this is my problem :

error C2440: 'initializing' : cannot convert from 'initializer-list' to 'char *'

i want it come like this

First Name+Last Name+Email

for ex :

First name : Max

Last name : TEST

Email : Max@gmail.com

it should come like this : MaxTESTMax@Gmail.com

Thanks guys

Rambo
  • 1
  • Did you mean to write `char* P1P2P3[] = ...`? – πάντα ῥεῖ Sep 03 '15 at 09:06
  • 1
    Welcome to stackoverflow. I'm afraid your question needs some more information to be able to receive answers: what exactly are FirstName, LastName and Email? Are they char* variables? If so, would you like to concatenate them? In that case [this question](http://stackoverflow.com/questions/1995053/const-char-concatenation) might help you. But either way, try to provide a little more context. Good luck. – qqbenq Sep 03 '15 at 09:09

3 Answers3

0

The only valid initialization for C-style strings is:

const char* const P1P2P3 = "literal string";

All other ways of making such strings can't be done by initializing.

A C-style strings way in C++ would be

char* P1P2P3 = new char[ENOUGH];
sprintf(P1P2P3, "%s%s%s", FirstName, LastName, Email);
//...
delete[] P1P2P3;

With C++ strings:

std::string P1P2P3 = std::string(FirstName) + LastName + Email;
if (buffer5 == P1P2P3) //...
stefaanv
  • 14,072
  • 2
  • 31
  • 53
0

option 1: use std::string instead of char*

std::string FirstName,LastName,Email;
 ...
std::string P1P2P3 = FirstName+LastName+Email;

option 2: use std::stringstream

std::stringstream sout;
sout<<FirstName<<LastName<<Email;
char* P1P2P3 = sout.str().c_str();

option 3: use sprintf to print string into buffer

option 4: use strcat to append 2 string, then again to append the 3rd.

SHR
  • 7,940
  • 9
  • 38
  • 57
0

Why not go with C++ way of doing it.

std::string P1P2P3 = FirstName + LastName + Email;
if (P1P2P3 == buffer5) {
    // OK
}
else {
    // Not OK
}
Shreevardhan
  • 12,233
  • 3
  • 36
  • 50