-2

I have below line of code:

char sPostData[500] = "{\"name\":\"Test Unique Name 1\",\"salary\":\"123456\"}";

where instead of Test Unique Name 1 I want to pass a variable value something like below:

string name = "JOHN";
char sPostData[500] = "{\"name\":" + name + "\",\"salary\":\"123456\"}";

But doing so it gives below error:

Error (active)  E0520   initialization with '{...}' expected for aggregate object   

Error   C2440   'initializing': cannot convert from 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' to 'char []'  

How can I resolve this issue and pass in the variable value in between. Thanks

xception
  • 4,241
  • 1
  • 17
  • 27
S Andrew
  • 5,592
  • 27
  • 115
  • 237
  • @S Andrew You should use C string functions as memmove and memcpy. – Vlad from Moscow Jun 18 '19 at 08:51
  • 2
    while I totally agree with the answer from Basile below if you are really using json syntax I believe if you have more or nontrivial data to transfer in this format you should instead learn to use an already existing implementation like rapidjson – xception Jun 18 '19 at 08:56
  • 1
    ... or _JSON for Modern C++_, they are both very good - and will save you from a lot of grief later. Since you're using Windows, you are likely to put some non-UTF8 characters in your JSON by mistake and then have a really hard time understanding why your JSON isn't accepted by the receiver. – Ted Lyngmo Jun 18 '19 at 09:18
  • If coming from Python, **developing such a code under Linux will make your life much easier** (in particular, since Linux is today UTF-8 and could have more libraries suitable for your needs). See also http://utf8everywhere.org/ – Basile Starynkevitch Jun 18 '19 at 11:32

1 Answers1

6

Use some std::string. So something similar to (the quoting is a bit wrong, you'll correct it)

std::string PostData =
  std::string{"{\"name\":"} + name + "\",\"" + salary + "\":\"123456\"}";

Later use PostData.c_str() instead of your sPostData....

Read also about raw string literals. In your case, they are helpful.

Learn also more about std::ostringstream. For example:

std::ostringstream os;
os << "{\"name\":" << name << "\",\"" << salary
   << "\":\" << pay << "}" << std::endl;
std::string PostData = os.str();

But you really should use some good JSON library, like jsoncpp. You probably should look into its 2nd example.

If you are coding something related to HTTP, learn more about that protocol, then consider using some HTTP client library like libcurl and/or HTTP server library like libonion or Wt or a framework like POCO or Qt.

BTW, IMHO, a Linux distribution is a better development platform (even for cross-compilation) than Windows is in your particular case.... (good UTF-8 support, good HTTP support, good JSON support) and many embedded hardware (e.g. RaspBerry PI) are Linux friendly.

See also http://utf8everywhere.org/

Beware that C++ is a very difficult language. See this and read http://norvig.com/21-days.html for a more general insight.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Here name is the variable which I want to use. I think you made salary as variable. Sorry I am bit confused with all `{` & `""` 's – S Andrew Jun 18 '19 at 08:56
  • Yes, I am also confused. The good solution is to use a JSON library – Basile Starynkevitch Jun 18 '19 at 08:58
  • That makes a lot of escaped `"`... OP and you should both check if you didn't forget to close your strings, particulary in the first one `" {\"name\":\" "` – DrosvarG Jun 18 '19 at 08:58
  • 1
    @SAndrew `std::string name = "JOHN";` `std::string PostData = std::string{"{\"name\":" + name + "\",\"salary\":\"123456\"}"};` – Rostin Jun 18 '19 at 08:58
  • Actually the reason I cannot use json is because I am passing this data to `Winhttp post request` which accepts post data in `LPVOID` – S Andrew Jun 18 '19 at 09:01
  • LPVOID is not a standard C++ type. You'll better code in standard C++11 or C++14, and use appropriate extra libraries *with care and caution* – Basile Starynkevitch Jun 18 '19 at 09:05
  • The quoting don't really matter (and if it does, OP will fix that). OP needs to use a JSON library – Basile Starynkevitch Jun 18 '19 at 09:13
  • We don't have to do the entire work for a so poorly asked -and non-researched- question. OP should do *some* of the work. If OP want me to do all his work, I would bill him. And professionally, I am expensive :-) – Basile Starynkevitch Jun 18 '19 at 09:15
  • Apologies but I understand and I am studying few other things in this. I am very new to c++ and I come from python background. – S Andrew Jun 18 '19 at 09:23
  • I think you missed one `/"`, it should be `std::string PostData = std::string{"{\"name\":\""} + name + "\",\"" + salary + "\":\"123456\"}";`. But anyways thanks for help. – S Andrew Jun 18 '19 at 10:55