fork download
  1. //Ryan Shahriyarpour CS1A Chapter 10 Question 7
  2. /*****************************************************************************
  3.  *
  4.  * Name Arranger
  5.  *
  6.  *********************
  7.  *
  8.  * This program asks for the user's first, middle, and last names, each
  9.  * stored in its own character array. It then builds a fourth array holding
  10.  * the name arranged as: last name, a comma and a space, first name, a
  11.  * space, then the middle name.
  12.  * For example, "Carol Lynn Smith" becomes "Smith, Carol Lynn".
  13.  *
  14.  * INPUT
  15.  * firstName - the user's first name
  16.  * middleName - the user's middle name
  17.  * lastName - the user's last name
  18.  *
  19.  * OUTPUT
  20.  * The name arranged as "Last, First Middle"
  21.  *
  22.  ****************************************************************************/
  23.  
  24. #include <iostream>
  25. #include <cstring>
  26. using namespace std;
  27.  
  28. int main()
  29. {
  30. // DATA DICTIONARY
  31. // NAME_SIZE - size of the first, middle, and last name arrays (constant)
  32. // FULL_SIZE - size of the arranged name array (constant)
  33. // firstName - the user's first name
  34. // middleName - the user's middle name
  35. // lastName - the user's last name
  36. // fullName - the arranged name: last, first middle
  37. const int NAME_SIZE = 30;
  38. const int FULL_SIZE = 100;
  39. char firstName[NAME_SIZE];
  40. char middleName[NAME_SIZE];
  41. char lastName[NAME_SIZE];
  42. char fullName[FULL_SIZE];
  43.  
  44. // Get the three names
  45. cout << "Enter your first name: ";
  46. cin.getline(firstName, NAME_SIZE);
  47. cout << "Enter your middle name: ";
  48. cin.getline(middleName, NAME_SIZE);
  49. cout << "Enter your last name: ";
  50. cin.getline(lastName, NAME_SIZE);
  51.  
  52. // Build the arranged name: Last, First Middle
  53. strcpy(fullName, lastName);
  54. strcat(fullName, ", ");
  55. strcat(fullName, firstName);
  56. strcat(fullName, " ");
  57. strcat(fullName, middleName);
  58.  
  59. // Display the arranged name
  60. cout << "\nArranged name: " << fullName << endl;
  61.  
  62. return 0;
  63. }
Success #stdin #stdout 0s 544KB
stdin
billy
bob
joe
stdout
Enter your first name: Enter your middle name: Enter your last name: 
Arranged name: joe, billy bob