Topical Information

Here are a few questions to help you clarify your understanding of basic looping with the while structure.

Question Set Information

Questions

  1. What is wrong in the following code? (You can assume that all variables are declared and adequately named.)

    
       c = 0;
       while (c < 10);
           cout << c << ' ';
           c = c + 2;
       cout << c << endl;
    
    
        There can be no semi-colon after the while's condition (or we'll loop
        the empty statement).  From the indention, it appears that the programmer
        wanted the first cout and the increment of c to both occur inside
        the loop — but only the cout will even with the erroneous
        semi-colon removed; so we'll have to add braces around this pair:
    
        c = 0;
        while (c < 10)
        {
            cout << c << ' ';
            c = c + 2;
        }
        cout << c << endl;
    
    
  2. Someone else has written a function with the prototype: 'void menu(void);'. Their function will print this output on the screen for the user:

    
       1) enter X
       2) enter Y
       3) Add Them
       4) Quit
    
       Choice:  _
    

    (Where the _ is the cursor's position.)

    You are to write all code necessary to both read and process the user's choice from this menu. (i.e. You are NOT to code the menu function — since someone else already did that for you! You may, however, want to call it...)

    
            bool quitting;
            char choice;
    
            quitting = false;
            while ( ! quitting )
            {
    
                menu();
                cin >> choice;
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
                choice = tolower(choice);
    
                if ( '1' == choice ¦¦ 'x' == choice )
                {
                    // input x
                }
                else if ( '2' == choice ¦¦ 'y' == choice )
                {
                    // input y
                }
                else if ( '3' == choice ¦¦ 'a' == choice
                          ¦¦ 't' == choice )
                {
                    // total x and y
                }
                else if ( '4' == choice ¦¦ 'q' == choice )
                {
                    quitting = true;
                }
                else
                {
                    // invalidity message
                }
            }
    
    
  3.  
    TRUE FALSE  A while loop is a post-test loop.
    TRUE FALSE  A while loop is an indefinite loop.
  4. Show what is output by the following loop:

    
       i = 1;
       while (i >= -10)
       {
           cout << i << ' ';
           i = i - 2;
       }
       cout << i << endl;
    
    
            1 -1 -3 -5 -7 -9 -11
    
    
  5. Write code to read in a user's test score. Make sure the score is non-negative before you continue the program! (Don't concern yourself with [non-]numeric-ness of the entry...)

    
            cout << "prompt";
            cin >> score;
            while ( score < 0 )
            {
                cerr << "error message";
                cout << "re-prompt";
                cin >> score;
            }
    
    
  6. Write code to read in a user's test score. Make sure the score is actually numeric before you continue the program! (Don't concern yourself with the data being reasonable or not...)

    
            cout << "prompt";
            cin >> score;
            while ( cin.fail() )
            {
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
                cerr << "error message";
                cout << "re-prompt";
                cin >> score;
            }
    
    
  7. Write code to read in a user's test score. Make sure the score is both non-negative and actually numeric before you continue the program!

    
            cout << "prompt";
            cin >> score;
            while ( cin.fail() ¦¦ score < 0 )
            {
                if ( cin.fail() )
                {
                    cin.clear();
                    cin.ignore(numeric_limits<streamsize>::max(), '\n');
                    cerr << "failure error message";
                }
                else
                {
                    cerr << "domain error message";
                }
                cout << "re-prompt";
                cin >> score;
            }
    
    
  8. You are given this function:

    
        bool toss(void);
    
    

    which returns true when a coin comes up heads and false when the coin lands tails up.

    Use this function in writing a loop to tell the user how many times their virtual coin came up heads and how many times it came up tails. You should keep count during a number of tosses which they will specify to you before-hand.

    (Hints: This will require a branch to be nested inside your looping structure...)

    
            short count, heads;    // same type as tosses...
    
            heads = 0;
            count = 0;
            while ( count < tosses )
            {
                if ( toss() )
                {
                    heads = heads + 1;
                }
                count = count + 1;
            }
    
            cout << "During the " << tosses << " coin tosses we got "
                 << heads << " heads and " << tosses - heads << " tails.\n";