#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int main()
{
// Recursion
char again;
// Main Program
do
{
int id=1, // Number of students
i=1, // Line number
counter=0; // Loop counter
string students, // Name of students
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: "; // Data entry qualification for number of students in class
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");
// For loop to retrieve names and compare
for (counter=1; counter <= id; counter++)
{
cout << "Please enter the students NAME: " << counter << "." ; // recieve input for names
getline(cin, students, '\n');
outFile << students << endl;
//Assign first and last to student to compare
if ( counter == 1)
{
first = last = students;
}
else // compare values after first iteration
{
if (first > students)
{
first = students; // reassign first
}
if (last < students)
{
last = students; // reassign last
}
}
}
// Display first and last students
cout << "First student in line : " << first << endl << "Last student in line : " << last << endl;
// 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. for CISC 192\n\n";
system("pause");
return 0;
}
I got the program running great just had to adjust some things in the for loop and change a couple logic statements. You were right easily done without any kind of sort just a couple well placed if statements!

New Topic/Question
Reply




MultiQuote



|