CS 010 - Introduction to Computer Science I
Lab 8. Formatting Output and Reference Parameters

Parts 1, 2, 3, 4 should be completed in the lab period

Grading
Attendance & participation: 2 points
Parts 1, 2, 3, 4 : 2 points each

After this lab you should be:
        More familiar with file I/O and formatting.
        More familiar with using call-by-reference parameters.


Part 1:

Write a program that asks the user to input a name followed by 4 quiz grades and writes them all to an output file named "scores.txt". Use a function that will ask the user for the name and quiz scores and then writes them to the output file. Open the file in main and pass the stream object to the function. Remember, a stream object must be a reference parameter.

You should not worry about formatting the output at this time. Just remeber to put a space or newline between each piece of data. The quiz scores should range between 0 and 10 pts.


Part 2:

Modify the program in part 1 to allow the user to enter data for multiple students. Only modify main for this. Do not alter the function. So, you should call the function each time the user is to enter a new student's scores. In main, you should ask the user if they want to add another student's data.


Part 3:

Add a function to your program that will write to the screen the contents of the output file, "scores.txt". You should format the output to look like the following:

name        Quiz 1   Quiz 2   Quiz 3   Quiz 4
----        ------   ------   ------   ------
John          10        7       10        9
Mary           9        8       10        9
Matthew        8        7        9       10

You should call this function after the user chooses not to enter any more data. Before you call this function, you should close the file object connected to "scores.txt" for writing (output) and then connect the file to an object for reading (input). Then pass this new object to your new output function.


Part 4:

What is the output of the following program if the user enters the values 4 and 3 for x and y?

#include<iostream>
using namespace std;

void swap(int a, int b);

void check(int& c, int& d);

int main()
{
   int x;
   int y;

   cout << "Enter two integers\n";
   cin >> x >> y;

   check(x,y);

   cout << endl << x << " <= " << y;

   return 0;
}

void swap(int a, int b)
{
   int temp = a;
   a = b;
   b = temp;
}

void check(int& c, int& d)
{
   if (c > d)
   {
      swap(c,d);
   }
}

Write this program (copy and paste) and then trace it through the debugger. What happens to the values of x and y when you step through the swap function? Why?

Without changing main or the function definitions, rewrite this program so that it will always have the correct output. In other words, the last cout statement will always make sense. Trace your corrected program using the debugger. What happens now to the values of x and y when stepping through the swap function?