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
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 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);
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Template syntax for variable length arrays 9 77
Arduino EDI - Programming language 3 115
Path to  STL Map header file 1 80
Need some help with design 15 15
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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

749 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