<You are writing a program that will display names (first and last) in a column. The maximum size you are allowing for each full name is 30 characters per line. Assuming your program has a variable called theName, of type string, write the output statement needed to display the name right justified within the 30 spaces reserved. NOTE: You don’t know the actual name to display. That is, the statement should work with any name.>
Thanks for your response. Where you have stringName = first, last; I thought that might work but couldn't find anything to verify it. I also have setw(30). So now I can do this with more confidence.
Please don't include line numbers when you post code. This means that we can't just copy/paste the code into a compiler. If you use code tags then lines are given numbers.
As has been said above by salam_c, L16 doesn't do what you are expecting. In c/c++ , is an operator which means evaluate each expression separated by a comma from left to right and return as the result the value of the last expression evaluated (the rightmost one). So L16 means set theName to last.
For std::string, use + to concatenate. So L16 becomes
string theName = first + last;
You don't need setw() on L18 as only one item is displayed on the line.
The assignment is partly about setting 30 spaces and right-justified. I put the lines in so that when I add the errors people know what I am talking about. So I think that I need setw(30)
#include <iostream>
#include <iomanip>
#include <string>
usingnamespace std;
int main() {
string first = "fred", last = "flintstone";
cout << first << setw(30) << last << endl;
cout << setw(30) << first << setw(30) << last << endl;
return 0;
}
It contains the cout line you're having trouble with, and the absolute minimum of code necessary to make it work.
You're not restricted to writing one program at once. If you're having trouble with some concept, then make a side project to extract the essence of the problem.