1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <iomanip>
using namespace std;
void getStudent (); //Function Prototype, studentID, name,courseCode,
//courseCredits, courseGrade, gpa
int main ()
{
cout << "This program gets student information from an existing File. " << endl;
getStudent (); //Calling the Function
return 0;
}
void getStudent()
{
//This function reads students from file and prints to screen
string fileName, name, courseCode, courseCredits;
string studentID, courseGrade;
ifstream inFile;
cout << "Please enter name of file you wish to open : ";
getline (cin, fileName);
//Open the file
inFile.open(fileName.c_str()); //This statement opens the file
//Check if it opened
if (inFile.fail()) //Check for successful Open, .fail()
{
cout << "Error! Failed to open file named " << fileName << endl;
}
else
{
cout << "File " << fileName << " was successfuly opened" << endl;
cout << "_____________________________________________________________________" << endl;
cout << endl;
system("cls"); //Clear screen after succesful open of the file
int idNum;
cout << "Enter Student Identification Number :";
cin >> idNum;
cout << "Student ID Number : " << idNum << endl;
system("cls"); //Clear screen
//Extracting information from file
getline(inFile, studentID);
getline(inFile, name);
getline(inFile, courseCode);
getline(inFile, courseCredits);
getline(inFile, courseGrade);
cout << "Student Name :" << name << endl;
cout << "Student ID Number : " << studentID << endl;
cout << endl;
cout << "________________________________________________________"<< endl;
cout << endl;
cout << "Course Code \t Course Credits Course grade " << endl;
cout << " " << courseCode << setw(16) << courseCredits << setw(18) << courseGrade << endl;
//Close file
inFile.close();
}
}
|