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

  • MHB
  • Thread starter ineedhelp420
  • Start date
By using both, you can concatenate the two strings with a space in between. In summary, to output "Barry Allen" using the given code, you would use the push_back method with a space character and then append the last name string to the secretID string.
  • #1
ineedhelp420
1
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
  • #2
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.
 

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

What is ".push_back" in programming?

.push_back is a function in programming that is used to add an element to the end of an array or vector.

What is the syntax for using ".push_back"?

The syntax for using ".push_back" varies depending on the programming language, but generally it follows the format of "array/vector name.push_back(element)".

Can ".push_back" be used for any data type?

Yes, ".push_back" can be used for any data type as long as the array or vector is defined to hold that specific data type.

What is the difference between ".push_back" and ".push"?

.push_back is used to add an element to the end of an array or vector, while ".push" can be used to add an element to any specified position within the array or vector.

What are the advantages of using ".push_back"?

Using ".push_back" allows for easy and efficient addition of elements to the end of an array or vector without having to specify the size beforehand. This can save time and memory in programming.

Similar threads

  • Programming and Computer Science
Replies
30
Views
3K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
3
Replies
75
Views
5K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
3
Views
858
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
Back
Top