CS 010 - Introduction to Computer Science I
Lab 3. Input and Output, Email access


Parts 1, 2, 3, 4, and 5 are required exercises for all students, and should be completed in the lab period

Parts 6 and 7 are challenge exercises (worth a small bonus!) to be attempted in whatever remaining time you have.

Grading
Attendance & participation: 2 points
Parts 1, 2, & 3: 2 points each
Parts 4 & 5: 1 point each
Parts 5 & 6 (bonus exercises): 0.25 points each

After this lab you should be:
       Familiar with the idea of code blocks.
       Able to get input from the keyboard.
       Able to access and send from your course email account.


Notes:

The input stream object cin is created when a C++ program includes the file <iostream>. cin is used to read from the standard input device, which is usually the keyboard.

The cout object is created when a C++ program includes the file <iostream>. cout is used to write output to the standard output, usually the monitor.

The string Library used in exercise 1 provides a number of very convenient functions for managing "strings", or character sequences. For the moment, all you need to know is:

The input stream object cin works very like the output stream object cout: it consists of a buffer (a region of memory set aside for a particular purpose) which holds the binary code representing each key pressed on the keyboard. For the moment, you can think of the cin buffer as storing each keystroke until the "Enter" key is struck, at which point it translates the collection of characters it has stored into the proper data type for the variable that has asked for input, using ">>", the stream extraction operator.

e.g.

      char one_letter;
      std::cin >> one_letter;
will take a single character (keyboard symbol) and store its code in the variable called one_letter, without any conversion.

      int one_integer;
      cin >> one_integer;
will take any number of numerical characters (0 - 9) and the plus or minus signs, and do whatever conversion is necessary to turn them into the appropriate integer representation, which will then be stored in the variable one_integer. 

      double one_float;
      cin >> one_float;
will likewise take any numerical characters, along with plus or minus signs or decimal point or exponent symbol (E or e), and convert them into a proper double data type for storage in the variable one_float


Part 1:

Is this program valid? If so, what does it do? If not, explain why and re-write it to be valid. (Make your predictions and explanations before entering, compiling and running the code!)

  #include <iostream>
  #include <string>
  int main()
  {
     {
        std::string s = "a string";
                 //this both declares and "initializes" the string s - i.e. gives it an initial value.

        {
           std::string x = s + ", really";
           std::cout << s << std::endl;
        }

        std::cout << x << std::endl;
     }

     return 0;
  }

Try placing the curly brackets in different places, i.e. create code blocks inside code blocks: you will find that if you declare a variable inside a code block, it is available for use only within that block.


Part 2:

Write a program that uses cin to obtain input from the user, and then uses cout to output that same data to the monitor. Think about how will you let the user know what you want them to enter.

Experiment with entering the wrong type of data at the keyboard (e.g. try to input character data into a variable of type double, etc.). What happens?


Part 3:

The following program is a personnel information registry system. Modify it so that it asks for and outputs both first and last names.

#include <iostream>
#include <string>
using namespace std;

int main()
{
string name;
int ID;

cout << "Enter your name ";
cin >> name;

cout << "Enter your ID number ";
cin >> ID;

cout << "Hello " << name << " or should I say " << ID << endl;

return 0;
}


Part 4:

Extend the program in part 3 to input and output other information. Include age, occupation, salary, and address. What data types are appropriate for each of these new variables?


Part 5:

Access your course email account. Using this account, send an email with your program from Part 4 as an attachment to your partner's course email account. Your partner should break your code, causing one or two syntax errors. They should then send back the broken code as an attachment to your email account. Fix the program, keeping a log of the syntax error and how you fixed it.


Part 6:

Write a program to count down from a number input by the user to -5.
So if the user inputs 3, the output will be:

3, 2, 1, 0, -1, -2, -3, -4, -5


Part 7:

Write a program which keeps on asking the user to input a number, until the user enters a number larger than 100.