Solved

error C2110: cannot add two pointers

Posted on 2004-08-04
7
3,573 Views
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
0
Comment
Question by:claracruz
7 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 11713919
char result[100];

...

strcpy(result, lName);
strcat(result, ", ");
strcat(result, fName);
0
 
LVL 5

Expert Comment

by:lemmeC
ID: 11713936
How about this:

strcat(lName,',');
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;

0
 
LVL 14

Accepted Solution

by:
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);
0
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

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

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

Expert Comment

by:Axter
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.
0
 
LVL 5

Expert Comment

by:lemmeC
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
0
 
LVL 30

Expert Comment

by:Axter
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
0

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