MHB The code should be:cout << secretID + spaceChar + lastName << endl;

  • Thread starter Thread starter ineedhelp420
  • Start date Start date
Click For Summary
The discussion centers on combining two strings in C++ to output "Barry Allen." The original code initializes two strings, `secretID` and `lastName`, and a character for space. To achieve the desired output, the correct approach involves using `push_back` for adding a character and `append` for adding a string. Specifically, `secretID.push_back(spaceChar)` adds a space, followed by `secretID.append(lastName)` to concatenate the last name. This distinction between `push_back`, which expects a character, and `append`, which expects a string, is crucial for correctly combining the strings.
ineedhelp420
Messages
1
Reaction score
0
Code:
#include <iostream>
#include <string>
using namespace std;

int main() {
   string secretID = "Barry";
   string lastName = "Allen";
   char spaceChar = ' ';

 /* Your solution goes here  */

   cout << secretID << endl;
   return 0;
}

I don't understand this. i need help
i'm suppose to out put "Barry Allen" but what do i push back?

Retype and correct the code provided to combine two strings separated by a space. Hint: What type of parameter does push_back expect?

Code:
   secretID.push_back(spaceChar);
   secretID.push_back(lastName);
 
Last edited by a moderator:
Technology news on Phys.org
You can use the following code.

Code:
 secretID.push_back(spaceChar);
 secretID.append(lastName);

The [m]push_back[/m] method adds a character to the end of the line, while [m]append[/m] adds another string.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 30 ·
2
Replies
30
Views
4K
  • · Replies 15 ·
Replies
15
Views
4K
Replies
12
Views
3K
  • · Replies 75 ·
3
Replies
75
Views
6K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
3
Views
1K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K