-5

I want to return a string from a function in C++ that is created from varying strings and an integer. I want to create a string in the form "ABCDFFFF00230000EFGH". "ABCD" is defined as a string in command[0], "0023" is an integer defined by answer, and "EFGH" is defined as a string in command[4].

I have tried:

return "{0}FFFF{1}0000{2}".format(command[0], answer, command[4]);

But this gives an error:

member reference base type 'const char [18]' is not a structure or union

I have tried:

return (command[0], "FFFF", answer, "0000", command[4]);

But this only returns the value of command[4].

I have tried:

return (command[0] + "FFFF", answer + "0000" + command[4]);

I have also written this code in Python:

return "{0}FFFF{1}0000{2}".format(command[0], answer, command[4])

Which gave the right answer.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Tori Harris
  • 563
  • 2
  • 7
  • 21
  • 2
    1) Did you try using [`std::sprintf`](https://en.cppreference.com/w/cpp/io/c/fprintf)? C++ is different language from python. Why did you expect it to behave in the same way? 2) Additionally, consider learning from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead of coding randomly. – Algirdas Preidžius Jul 21 '18 at 10:06

1 Answers1

5

Use a string stream (requires #include <sstream> and #include <iomanip>). This probably isn't exactly right with the formatting but it will get you started.

std::ostringstream oss;
oss << command[0] << "FFFF" << setfill('0') << std::setw(4) << answer << "0000" << command[4];
return oss.str();

However, don't just try random things that work with Python as they're very unlikely to work in C++. Do some proper research.

AStopher
  • 4,207
  • 11
  • 50
  • 75
john
  • 85,011
  • 4
  • 57
  • 81
  • I have no wish to get into an edit war with you, please stop rolling back improvements to your answer. Like I said before on another of your answers, you cannot stop users editing your posts as per the Stack Overflow terms and you cannot ask users to stop doing so. By posting content you are transferring the copyright to Stack Overflow and you no longer own it. – AStopher Jul 21 '18 at 12:43
  • @cybermonkey But they aren't improvements. For example BTW does not mean the same as however. The meaning is just different. I don't wish to get into a edit war either but you should stop editing my posts. Correct clear mistakes of course but don't edit it for style, or change the meaning with your edits. I don't really care about the legal niceties but I would have thought common courtesy would be that you would respect my wishes over my post. Maybe I don't own the post, but neither do you. You have no more rights than me. – john Jul 22 '18 at 06:43
  • Style is a valid reason for editing posts and is encouraged if it improves the post, which it did. – AStopher Jul 22 '18 at 11:54
  • @cybermonkey Style is always a matter of opinion. It's only your view that your edits improved the style. I like posts with my name attached to be written in my writing style. But in any case you actually changed the meaning of my post. You substituted one word for another which has a different meaning. – john Jul 22 '18 at 20:30