[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


error C2110: cannot add two pointers

Posted on 2004-08-04
Medium Priority
Last Modified: 2008-03-17
Hello experts,

what is the best way to achieve the following;

      char name[20];
      char lName[20];
      char fName[20];

                cout << "\nEnter Customer's LAST Name : ";
      cin >> lName;
      cout << "\nEnter Customer's FIRST Name : ";
      cin >> fName;

If user inputs the following

                                  Last Name: Browne
                                  First name: Jack

I need name to output the following

                       name: Browne, Jack.

How do I add to two together and include the comma.

Thank you
Question by:claracruz
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 48

Expert Comment

ID: 11713919
char result[100];


strcpy(result, lName);
strcat(result, ", ");
strcat(result, fName);

Expert Comment

ID: 11713936
How about this:

strcat(lName, fName); //lName has to be big enough to hold the entire string

Alternately, use the STL string class for fName and lName.

string fName, lName;
string Name;

cout << "\nEnter Customer's LAST Name : ";
cin >> lName;
cout << "\nEnter Customer's FIRST Name : ";
cin >> fName;

Name = lName + ',' + fName;

cout << Name;

LVL 14

Accepted Solution

Daniel Junges earned 2000 total points
ID: 11714408
Other solution would be:

char name[20];   <-- ensure the size is big enough
char lName[20];
char fName[20];

sprintf(name, "%s,%s", fName, lName);
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 30

Expert Comment

ID: 11715214
>>strcat(lName, fName); //lName has to be big enough to hold the entire string

I'm sure the above is a typo.
strcat requires double quotes.
LVL 30

Expert Comment

ID: 11715243
Since you're programming in C++, I agree with lemmeC alternate method, which uses std::string.

It's safer, and you don't have to worry about the maximum size of the name.

Expert Comment

ID: 11715340
>>I'm sure the above is a typo.
>>strcat requires double quotes.

Right Axter.

Probably the same holds for the alternate part as well.
Name = lName + '', '' + fName;

By the way, the exact same solution also appears in http://www.msoe.edu/eecs/cese/resources/stl/string.htm
LVL 30

Expert Comment

ID: 11715508
>>Probably the same holds for the alternate part as well.
>>Name = lName + '', '' + fName;

If you need the extra space after the comma, then you do need double quotes.
If you only need one character, you can use single quotes.
name = lName + ',' + fName; //This does work for std::string

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question