Homework #2 - Changing Variables With "cin"

10 Points

Purpose:  This assignment is to give you practice with changing a program and compiling it.  It assumes that you've finished the first homework and have the program "hello.cpp" in your home directory (where you end up when you logon to the linux computers).  Follow the directions below to complete the assignment.

Instructions:

1. Login to the System

    You can do this by following the directions on the course web page (first lesson) also located here.  Then come back to this page

2.  List the programs in your home directory to make sure hello.cpp is present, i.e., type "ls" either in PuTTY or in the Chameleon terminal window (the black window).

3.  Copy the file hello.cpp to hw2.cpp, i.e., type "cp hello.cpp hw2.cpp"

4.  Open hw2.cpp, i.e., type:  "vi hw2.cpp" or if you running PuTTY or press the Open icon in the toolbar and choose hw2.cpp in Chameleon.

5.  Edit hw2.cpp by changing your hw2.cpp program to appear like it does below.

// hw2.cpp - CS1210 second homework program
// Author: Put your name here
// Date: Put today's date here

#include <iostream>

using namespace std;

int main() {
     string firstName, lastName;

     // get the user's name
     cout << "What is your first name: ";
     cin >> firstName;

     cout << "What is your last name: ";
     cin >> lastName;

     // write out the greeting
     cout << "Hello ";
     cout << firstName;
     cout << lastName;
     cout << endl;

     return 0;
}

6.  Compile hw2.cpp, i.e., type:  "g++ hw2.cpp"

7.  Test the program, i.e., type:  "a.out"

A correct program will ask you for your first name and last name and then respond with a message, "Hello your-first-name your-last-name".

Note that the program, as shown, causes your name to run together with no space in between.  Spend a few minutes trying to fix this problem (if you can't fix the problem after 10 minutes, move to step 8).

8.  Submit your program for grading, i.e., type: "submit" and answer the questions as in the homework 1.

9.  Print a copy of your program to turn it in at the beginning of class on the due date shown on the course web page schedule.  You can print the program in our classroom from Chameleon's "print" menu. Alternatively, you can print from PuTTY to the printer in ENS 242 using the command:  "lpr program_file_name" (e.g. "lpr hw2.cpp").