Stuck again and need help

I cannot get the code button to work.

HI,
This is my assignment.

<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.>

Here is what I have so far:

#include <iostream>
#include <string>
#include <iomanip>

int main()
{

string theName;
string first;
string last;

//What I would like to do here is to input the first and last name into one variable but I have not found a way to do that.

cout << "Enter first and last name" << endl;
cin >> first >> last>> endl; //I think

string theName = first, last;

cout << theName << setw(30) << setw(30);

//question do I need to use ostream right?
> I cannot get the code button to work.
Then manually type in the code tags, and paste your code between them.
[code]
// code goes here
[/code]

> string theName = first, last;
The comma operator doesn't join strings together.

> //question do I need to use ostream right?
Try putting the setw before the string.
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.
I am still having a problem with running the code. I've got errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#include <iostream>
#include <string>
#include <iomanip>

8  int main()
9    {

11       string theName;
12        string first;
13        string last;

15      cout << "Enter first and last name" << endl;
16      cin >> first >> last >> endl;

18        string theName = first, last;

20       cout << theName << setw(30)<< endl;

22        return 0;
}

I'm getting errors on lines 11, 12, 13, 16, and 18 
OK I fixed all of the errors and I get a console window. I forgot to add using namespace std; and there were some double entries.


I ran the code an got a console window. I tried entering a name in for first name and couldn't, does anybody know why?

Thanks,
Mark
Last edited on
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.


Last edited on
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)
Last edited on
Here is my current code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main()
    {

        string theName;
        string first;
        string last;

        cout << "Enter first and last name" << endl;
        cin >> first >> last ;

         theName = first + last;

        cout << theName << setw(30) << endl;

        return 0;
}
Perhaps start with
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <iomanip>
#include <string>
using namespace 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.
I cannot get the code button to work.
For whatever reason it doesn't work in initial posts, but it should work in replies. Perhaps the admin will eventually find time to fix it.

You can type in "code tags" by hand like this:
[code]
// put your code in here
int main() 
{ 
  /* blah */ 
} 
[/code]

If you type that into the post it shows up as:
1
2
3
4
5
// put your code in here
int main() 
{ 
  /* blah */ 
} 

You can do that even in initial posts where the code button doesn't work. Hopefully that's easier than putting line numbers in by hand.
Last edited on
So I think that I need setw(30)


With cout, setw() affects what comes after the setw(), not what comes before. So:

 
cout << setw(30) << theName << endl;


Also you might want to consider concatenating the first and last names with a space:

 
theName = first + ' ' + last;

Topic archived. No new replies allowed.