Name: ____________________________________________________ Alpha: _____________________

Describe help received: _________________________________________________________________

  1. [10pts] In the code below, what would normally cause the program to exit the while loop, and what exceptional condition would cause the program to never enter the while loop?

        ifstream fin("foo.txt");
        int count = 0;
        char c = 'x';
        while(fin) {
          if (c == '?')
            count = count + 1;
          fin >> c;
        cout << count << endl;
  2. [10pts] The code below is supposed to add up all the integers in a file. However, it doesn't compile. Annotate the code to fix this error so that it compiles.

        string fname;
        cout << "Enter file name: ";
        cin >> fname;
        ifstream fin(fname);
        int sum = 0, next;
        while(fin >> next) {
          sum = sum + next;
        cout << sum << endl;

    If the file named by the user consisted solely of the line


    what would the above program (once fixed!) output ?

  3. [80pts] Write a program that reads in a file like input.txt that contains the names of several students along with their hw, quiz and exam averages and prints out the names of the students followed by their overall average given the weighting 20%, 20% and 60%, respectively. For example, on input.txt the program should produce:

    Brown   71.8
    Green   93.8
    Jones   82.4
    Smith   86.2
    Worth   91.2

    Your program should work for any file in the same format, though you may hard-code the filename, rather than read it in from the user if you prefer.

Turn in: a printout of your source code & a screencaptrue of the output of your program on this input file: test.txt.