CS 1440 Lab 6

Cover sheetPrevious ActivityNext Activity


Activity 6-3: Input intricacies

Activity Goals

Activity Procedure

int  input

We need a program called getints.C that asks the user to enter 3 int values.  It should read those 3 values into 3 int variables.  Then output the three values read from the keyboard.

Here's a program that you can use. (Be sure to edit the header comment.)

// Abbreviated header for donated code....
//
// Program:     ...fill it in....
// Author:      ...fill it in....
// Date:        ...fill it in....

#include <iostream>

int main()
{
  int a, b, c;

  cout  <<  "Please enter three (3) "
        <<  "ints ";

  cin   >>  a;
  cin   >>  b
        >>  c;

  cout  <<  "a =  "   <<  a  <<  ",  "
        <<  "b =  "   <<  b  <<  ",  "
        <<  "c =  "   <<  c  <<  endl;

  return 0;
}

Now compile the program and run it several ways to see what happens.

1.      When prompted, key in:            1  2  3
It should show you 1 and 2 and 3 as the values.

2.      When prompted key in:             123
It should "hang."  Why?  Why isn't the first variable given a 1, the second 2, the third 3?  Oh, so the spaces are used to separate the numbers.  So, this hangs because it's only read one number and is expecting two more.  (Ctrl-C to kill hanging program -- or you can enter two more numbers!)

3.      When prompted key in:             11              12                  13
So, many spaces are as good as one in separating values.  Importantly, notice that all these spaces are "skipped," discarded, thrown away by cin.  And this happens automatically for us.

4.      When prompted key in:             11
                   12
                   13
(That's the number 11 followed by an Enter key followed by 12 and an Enter key, etc.)  So, the Enter key (a newline in C++ terminology) is also ignored. 

5.      When prompted key in:             11   12   13   
And make sure to add some spaces after the 13 and before you hit Enter.
What do you think happens to those spaces after the 13?  Are they skipped or discarded?  The answer is no.  White space BEFORE a number is discarded, but the white space AFTER a number is untouched.  So those spaces can still be read (or discarded) by cin.

double input

For this part we need a program called getdoubles.C that asks the user to enter 3 double values.  It should read those 3 values into 3 double variables.  Then output the three values read from the keyboard.

We won't give you the code this time, but you can use the Unix command:   cp  getints.C  getdoubles.C   and use an editor to make appropriate changes.

Now run the program several ways to see what happens.

1.      When prompted, key in:            1  2  3

2.      When prompted key in:             123

3.      When prompted key in:             1              2                  3

4.      When prompted key in:             11
                   12
                   13

5.      When prompted key in:             1.3   2   3.5   

So, double input works a lot like int input in terms of how white space is treated.

char input

For this part we need a program called getchars.C that asks the user to enter 3 char values.  It should read those 3 values into 3 char variables.  Then output the three values read from the keyboard.

We won't give you the code this time, but you can use the Unix command:   cp  getints.C  getchars.C   and use an editor to make appropriate changes.

Now run the program several ways to see what happens.

1.      When prompted, key in:            1  2  3

2.      When prompted key in:             123

3.      When prompted, key in:            A  b  C

4.      When prompted key in:             A              11                  C

5.      When prompted key in:             A
                   11
                   C

6.      When prompted key in:             1.3   2   3.5   

So, how does char input work?  As with int and double, leading white space is ignored.  But 11 is now two digit characters (not the number eleven) and 1.3 is three characters of input. 

string input

For this part we need a program called getstr.C that asks the user to enter 3 string values.  It should read those 3 values into 3 string variables.  Then output the three values read from the keyboard.

We won't give you the code this time, but you can use the Unix command:   cp  getints.C  getstr.C   and use an editor to make appropriate changes.

Now run the program several ways to see what happens.

1.      When prompted, key in:            1  2  3

2.      When prompted key in:             123

3.      When prompted, key in:            A  b  C

4.      When prompted key in:             A              11                  C

5.      When prompted key in:             A
                   11
                   C

6.      When prompted key in:             1.3   2   3.5   

7.      When prompted key in:             Hi  There  Jay

So, how does string input work?  Again, white space separates strings.  However, notice that since strings are collections of characters, 11 is read as one string containing two characters.

 

Activity Followup

Turn in written answers to the following questions.

1.      Run your getints program with 3  4.5  6 as input.  Explain what is happening.

2.      True  or  False.
cin >>  to a character variable skips leading white space.

3.      True  or  False.
cin >>  to a character variable skips trailing white space.

4.      True  or  False.
cin >>  to a int variable skips leading white space.

5.      True  or  False.
cin >>  to a int variable skips trailing white space.

6.      True  or  False.
cin >>  to a string variable skips leading white space.

7.      True  or  False.
cin >>  to a string variable skips trailing white space.