fork download
  1. // Attached: Lab 2 - Program #1
  2. // ===========================================================
  3. // File: Lab2_Program1
  4. // ===========================================================
  5. // Programmer: Elaine Torrez
  6. // Class: CMPR 121
  7. // ===========================================================
  8.  
  9. #include <iostream>
  10. #include <string>
  11. using namespace std;
  12.  
  13. // ==== main ====================================================
  14. //
  15. // This program uses string variables to read a user's name
  16. // and state, displays character counts, and compares names.
  17. //
  18. // =============================================================
  19. int main()
  20. {
  21. // Variable declarations
  22. string firstName = "";
  23. string lastName = "";
  24. string fullName = "";
  25. string city = "";
  26.  
  27. // Input
  28. cout << "Enter your first name: ";
  29. getline(cin, firstName);
  30.  
  31. cout << "Enter your last name: ";
  32. getline(cin, lastName);
  33.  
  34. cout << "Enter the state you live in: ";
  35. getline(cin, city);
  36.  
  37. // Processing
  38. fullName = firstName + " " + lastName;
  39.  
  40. // Output
  41. cout << "Hi " << fullName << ". So you live in " << city << "." << endl;
  42.  
  43. cout << "Your first name has " << firstName.length()
  44. << " characters," << endl;
  45.  
  46. cout << "and your last name has " << lastName.length()
  47. << " characters." << endl;
  48.  
  49. if (firstName == lastName)
  50. {
  51. cout << "Your first and last names are the same." << endl;
  52. }
  53. else
  54. {
  55. cout << "Your first and last names are different." << endl;
  56. }
  57.  
  58. cout << "Press any key to continue . . .";
  59. cin.get();
  60.  
  61. return 0;
  62. } // end of main()
  63. // =============================================================
  64.  
Success #stdin #stdout 0.01s 5328KB
stdin
Elaine
Torrez
California
stdout
Enter your first name: Enter your last name: Enter the state you live in: Hi Elaine Torrez. So you live in California.
Your first name has 6 characters,
and your last name has 6 characters.
Your first and last names are different.
Press any key to continue . . .