Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that reads a list of enrollments from a file and prints a class

ID: 3676570 • Letter: W

Question

Write a program that reads a list of enrollments from a file and prints a class roster for the teacher. Make the class roster line up as neatly as you can and still fit on the screen.

You can choose the actual values to use for your data. Make sure to do enough sets of data to well test your program! (Empty data set, normal data set, data with large values, data with short values, etc.)

Also, your program can't know ahead of time how many people are enrolled in the class...

Don't forget to read the file's name from the user and protect your program against any errors that may occur during the opening of the file.

Try to use functions to break up the program into more manageable pieces.

As an example, you might have the data file contain:

(Note how Happy doesn't give out his address or phone number.)

And the program interaction might look something like (the parts in this color are typed by the user):

(Note that we don't use all of the available data in our table -- on purpose!)

Don't forget about the occasion when there are no enrolled students in the file at all!

Explanation / Answer

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main(){
   cout << "Welcome to the Roster Generator Program!!!";
   char fileName[10];
   cout << "Please enter the name of your names file: ";
   cin >> fileName;
   ifstream inFile(fileName);
   while(!inFile){
       cout << "I'm sorry, I could not open '" << fileName << "'. Please enter another name: ";
       cin >> fileName;
       inFile.open(fileName);
   }
   cout << "File '" << fileName << "' opened successfully! ";
   cout << "Name |Major |Phone #| Town ";
   string name, major, phone, address, town, number;
   cout << "--------+-------+-------+------------ ";
   getline(inFile, name);  
   getline(inFile, number);
   getline(inFile, address);
   getline(inFile, town);
   getline(inFile, phone);
   getline(inFile, major);
   while(getline(inFile, name)){
       major = "";
       phone = "";
       town = "";
       getline(inFile, number);
       getline(inFile, address);
       getline(inFile, town);
       getline(inFile, phone);
       getline(inFile, major);
       cout << setw(10) << name << setw(10) << " | " << major << setw(10) << " | " << phone << setw(10) << " | " << town << " ";
   }
   return 0;
}