Public Group

# C++ Primer Plus - string construction question

This topic is 2859 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Im a new programmer and I have been trying to make my way through C++ Primer Plus and I have a question about one of the programming exercises at the end of chapter 4.

The questions says to write a program that asks the user to enter his or her first name and then last name, and that then constructs, stores, and displays a third string, consisting of the user’s last name followed by a comma, a space, and first name. Use char arrays and functions from the cstring header file.

I guess my problem is trying to use the method that I think the book wants me to use. I did come up with a program but it seems sort of strange and im thinking that I probably did this the hard way and missed the point that the book was trying to teach (if that makes any sense).

Anyway, here is what I came up with. Could someone tell me if this is what the book had in mind or if I was just lucky that it compiled at all? For example, it seems weird that I would have to plan to make the char array holding l_name (last name) to hold 20 elements so I could add f_name and l_name only to turn around and place it in a new variable.

#include <iostream>#include <cstring>//#include <string>#include <conio.h>int main(){	using namespace std;	char f_name[10];	char l_name[20];	char full_name[20];	cout << "Please enter your first name: ";	cin.getline(f_name, 10);	cout << "Please enter your last name: ";	cin.getline(l_name, 20);	strcat(l_name, ", ");	strcat(l_name, f_name);	strcpy(full_name, l_name);	cout << full_name;	_getch();	return 0;}

I also get quite a few warnings and im wondering if I could do something to avoid this as well. Im using VC++ Express and it says that I should replace strcat and strcpy with strcat_s and strcpy_s but the book doesnt mention anything about them so im not sure what the difference is.

Thanks all

##### Share on other sites
The difference is that strcat and strcpy are now deprecated in favor of the secure versions which check that the data can actually fit in the buffers.

In your example, full_name should be size 32 so it can fit both first name, ", ", and last name.

Using strcpy/strcat, you should do something like this:

strcpy_s(full_name, 32, last_name);strcat_s(full_name, 32, ", ");strcat_s(full_name, 32, first_name);

The easiest thing to do is (instead of the above):

sprintf_s(full_name, 32, "%s, %s", last_name, first_name);

http://msdn.microsoft.com/en-us/library/8ef0s5kh%28v=VS.80%29.aspx

If you don't want to use the secure versions of the functions for some reason, you can #define _CRT_SECURE_NO_DEPRECATE to get rid of the associated warnings.

Edit: Updated this to reflect that I had forgotten about the extra space in the buffer for ", ".. oops.

[Edited by - 1863 on August 27, 2010 11:59:52 AM]

##### Share on other sites
You should definitely consider using the std::string class for this. You don't have to specify any buffer size. It will dynamically resize when it needs more memory:
#include <iostream>#include <string>int main()  {  using namespace std;  string firstName;  string lastName;  cout << "Please enter your first name: ";  cin >> firstName;  cout << endl << "Please enter your last name: ";  cin >> lastName;  string fullName = lastName + ", " + firstName;  cout << endl << fullName << endl;  }

edit: You already commented out the string include in your example, so maybe this is not new information :)

##### Share on other sites
I'm not really familiar with that book, so I don't know exactly how it's going about teaching the language. With that said, bare character arrays (or bare arrays in general) are not commonly used in "modern c++". As has been mentioned, strings and vectors are preferred when dealing with arrays and strings. :)

I usually mention this book to beginners:

Programming -- Principles and Practice Using C++

##### Share on other sites
C++ books usually start off by teaching you the "C" way of doing things. Presumably because you'll most likely run into some of this code "in the wild", and they want you to understand it (it's good to know). But, as others have said, it's not the way things should be done in C++. std::string should be used, instead.

As an exercise, try typing more than ten characters for your first name. A little hint: the program won't like it.

This has to do with the fact that the remaining characters are still left in the cin stream (you only extract 10 from the stream). You can "fix" this by testing cin.fail() and, if it failed, use cin.clear() to clear the error and cin.ignore() to ignore the rest of the stream. Also, you need to make sure your full_name buffer is large enough to accept both the f_name, l_name, and the two characters added (", ").

A little more robust version:
#include <iostream>#include <cstring>//#include <string>#include <conio.h>#include<limits>int main(){	using namespace std;	char f_name[10];	char l_name[20];	char full_name[32];		cout << "Please enter your first name: ";	cin.getline(f_name, 10);	if (cin.fail())	{ 		cin.clear();		cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');	}	cout << "Please enter your last name: ";	cin.getline(l_name, 20);	strcpy_s(full_name, l_name);	strcat_s(full_name, ", ");	strcat_s(full_name, f_name);	cout << full_name;	_getch();	return 0;}

##### Share on other sites
Thanks for all the advice. The next problem in the book is actually to write code using the string class. It seems like a pretty good book because it teaches the old way of doing things as well as the new which I like because I hope to be able to follow and learn from older game source code one of these days. The book also mentioned the problem of input still sitting in the queue and how to deal with it.

Thanks a ton for this part as well
	strcpy_s(full_name, l_name);	strcat_s(full_name, ", ");	strcat_s(full_name, f_name);

I knew there was a better way of doing what I originally wrote.

Thanks for the help everyone.

[Edited by - Planemo on August 27, 2010 10:23:14 AM]

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631434
• Total Posts
3000051
×