/*
Devon Naccarato, Alex Mieczkowski, 02/11/13
*/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int main()
{
// Recursion
char again;
// Main Program
do
{
int id=0, // Number of students
i=1, // Line number
counter=0; // Loop counter
string students, // Name of students
temp, // for comparison
first, // Student that goes first
last; // Student that goes last
bool cat=true; // Recursion boolean
ifstream inFile; // Read from file
ofstream outFile; // Write to file
// Ask for number of students
do
{
cat = true;
cout << "Enter number of students: ";
if (!(cin >> id) ||id < 5 || id > 30)
{
cout << "\nERROR - Invlid entry\n";
cin.clear();
fflush(stdin);
}
else
{
cat = false;
cin.clear();
fflush(stdin);
}
}while (cat == true);
// Start writing to file
outFile.open("students.txt");
// Ask for student names
for (counter=0; counter < id; counter++)
{
cout << "Please enter the students name: " << counter + 1 << "." ;
getline(cin, students, '\n');
outFile << students << endl;
students = temp;
if (students > temp)
{
temp = students;
students = first;
first = temp;
}
else if (students < temp)
{
students = last;
}
else
students = temp;
}
// Close file
outFile.close();
// Open student file
inFile.open("students.txt");
// Checks to see if file exists
if (!inFile)
{
cout << "File does not exist!";
}
// Print student names
cout << "\nYou entered: \n";
while (getline(inFile, students, '\n'))
{
cout << " " << i++ << ". "<< students << endl;
}
// Close file
inFile.close();
// Recursion
cout << "\n\nWould you like to run this program again?: [Y/N] ";
cin >> again;
}while (again == 'Y' || again =='y');
// Programmers
cout << "\n\nThis program was written by Devon Naccarato, and Alex Mieczkowski.\n\n";
system("pause");
return 0;
}//
This post has been edited by Skydiver: 19 February 2013 - 05:54 PM
Reason for edit:: Put code in code tags. Learn to do this yourself in the future.

New Topic/Question
Reply



MultiQuote




|