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

x
?
Solved

error C2110: cannot add two pointers

Posted on 2004-08-04
7
Medium Priority
?
3,589 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
[X]
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
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 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);
0
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

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

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