error C2110: cannot add two pointers

Posted on 2004-08-04
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
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 500 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);
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now