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. // Constants for the array sizes
  29. const int NAME_SIZE = 30; // size of each individual name array
  30. const int FULL_SIZE = 100; // size of the arranged name array
  31.  
  32. int main()
  33. {
  34. // DATA DICTIONARY
  35. // NAME_SIZE - size of the first, middle, and last name arrays (constant)
  36. // FULL_SIZE - size of the arranged name array (constant)
  37. // firstName - the user's first name
  38. // middleName - the user's middle name
  39. // lastName - the user's last name
  40. // fullName - the arranged name: last, first middle
  41. char firstName[NAME_SIZE];
  42. char middleName[NAME_SIZE];
  43. char lastName[NAME_SIZE];
  44. char fullName[FULL_SIZE];
  45.  
  46. // Get the three names
  47. cout << "Enter your first name: ";
  48. cin.getline(firstName, NAME_SIZE);
  49. cout << "Enter your middle name: ";
  50. cin.getline(middleName, NAME_SIZE);
  51. cout << "Enter your last name: ";
  52. cin.getline(lastName, NAME_SIZE);
  53.  
  54. // Build the arranged name: Last, First Middle
  55. strcpy(fullName, lastName);
  56. strcat(fullName, ", ");
  57. strcat(fullName, firstName);
  58. strcat(fullName, " ");
  59. strcat(fullName, middleName);
  60.  
  61. // Display the arranged name
  62. cout << "\nArranged name: " << fullName << endl;
  63.  
  64. return 0;
  65. }
Success #stdin #stdout 0s 544KB
stdin
bob
john
carl
stdout
Enter your first name: Enter your middle name: Enter your last name: 
Arranged name: carl, bob john