Daily Archives: January 10, 2012

C++ || Whats My Name? – Using a Char Array, Strcpy, Strcat, Strcmp, & Strlen

Here is another actual homework assignment which was presented in an intro to programming class.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

Character Arrays
Functions
Strcpy
Strcat
Strcmp
Strlen

This program first prompts the user to enter their name. Upon receiving that information, the program saves input into a character array of size 15. The program then asks if the user has a middle name. If they do, the user will enter a middle name. If they dont, the program proceeds to ask for a last name. Upon receiving the first, [middle], and last names, the program will use strcpy and strcat to copy/append the first, [middle], and last names into a completely new char array titled “fullname.” Lastly, if the users’ first, [middle], or last names are the same, the program will display that data to the screen via cout. The program will also display to the user the number of characters their full name contains using strlen.

NOTE: On some compilers, you may have to add #include < cstring> in order for the code to compile.


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

CHAR ARRAY
Unlike integer arrays, a loop is not necessary in order to input data into a char array. This is highlighted on lines 32, 76, 79, and 125.

FUNCTIONS
Notice lines 18, 19, 69 and 119. When dealing with arrays, in order to pass variables to different functions, you need to include brackets[] letting the compiler know that you are passing an array variable. If you do not declare the functions as so, you will get a compiler error.

STRCPY/STRCAT
These functions are highlighted on lines 81-85, and 127-129. Read the comments within the code on those selected lines, they are helpful.

STRCMP
This compares two strings together to determine if they are the same. This is displayed on lines 92, 96, 100, and 136 when comparing the first, [middle], and last names together for sameness.

STRLEN
This finds the length of the current string, as noted on lines 89 and 133.

Once compiled, you should get this as your output:
Note: The code was compiled five separate times to display the different outputs its able to produce


Please enter your first name: My
Do you have a middle name?(Y/N): y
Please enter your middle name: Programming
Please enter your last name: Notes

Your full name is My Programming Notes
The total number of characters in your name is: 18
-----------------------------------------------------------------------

Please enter your first name: Programming
Do you have a middle name?(Y/N): N
Please enter your last name: Notes

Your full name is Programming Notes
The total number of characters in your name is: 16
-----------------------------------------------------------------------

Please enter your first name: Notes
Do you have a middle name?(Y/N): y
Please enter your middle name: Notes
Please enter your last name: Notes

Your full name is Notes Notes Notes
The total number of characters in your name is: 15
Your first and middle name are the same
Your middle and last name are the same
Your first and last name are the same
-----------------------------------------------------------------------

Please enter your first name: My
Do you have a middle name?(Y/N): N
Please enter your last name: My

Your full name is My My
The total number of characters in your name is: 4
Your first and last name are the same
-----------------------------------------------------------------------

Please enter your first name: My
Do you have a middle name?(Y/N): z

Please press either 'Y' or 'N'
Program exiting...