Write a C++ program that prompts the teacher to enter the total number of students in a class, and then loops to read in that many full names. All the read-in names must be recorded in the order that they were entered.
Once all the names have been read in, the program reports which student should be at the front of the line, and which one should be at the end of the line. You may assume that no two students have the same name.
Also, the list of the students’names must be displayed on screen for the teacher to check back of her record.
Input validation: the number of students must be in the range from 3 to 30, inclusively.
#include <iostream>
#include <string>
#include <fstream>
usingnamespace std;
int main()
{
ofstream outputFile;
ifstream inputFile;
int initial, number, count;
string name, front, end;
outputFile.open("StudentList.txt");
cout<<"Enter how many students are in the class: ";
cin>>number;
fflush(stdin);
for (initial =1; initial <=number; initial++)
{
cout<<"Enter full name of student "<<initial<<" : ";
getline (cin, name);
outputFile << name << endl;
fflush(stdin);
{
if (count == 1)
{
front=name;
end=name;
}
if (name<front)
front=name;
if (name>end)
end=name;
}
}
cout << "The first person " << front << endl;
cout << "The last person " << end << endl;
outputFile.close();
inputFile.open("StudentList.txt");
while (getline(inputFile, name))
{
cout<<name<<endl;
}
inputFile.close();
fflush(stdin);
cin.get();
return 0;
}
When I run this, it does everything I want it, except it doesn't give me the first person in line, it does give me the last person just not the first person.
Put a break point on if (name<front) and see what happens on the first pass. Once you see that it isn't doing what you thought it would do (as it stands) figure out how to make that work.
Another hint is to look at your if (name>end) and figure out why that one works as is.
#include <iostream>
#include <string>
#include <fstream>
usingnamespace std;
int main()
{
ofstream outputFile;
ifstream inputFile;
int initial, number, count;
string name, front, end;
outputFile.open("StudentList.txt");
cout<<"Enter how many students are in the class: ";
cin>>number;
fflush(stdin);
for (initial =1; initial <=number; initial++)
{
cout<<"Enter full name of student "<<initial<<" : ";
getline (cin, name);
outputFile << name << endl;
fflush(stdin);
if (name<front)
front=name;
if (name>end)
end=name;
}
cout << "The first person " << front << endl;
cout << "The last person " << end << endl;
outputFile.close();
inputFile.open("StudentList.txt");
while (getline(inputFile, name))
{
cout<<name<<endl;
}
inputFile.close();
fflush(stdin);
cin.get();
return 0;
}
This is what I call the min/max error. Lets say you were trying to find the smallest number out of a series of numbers, lets call whatever the current number you are comparing the current and the smallest out of those numbers smallest.
1 2 3 4 5 6 7 8 9 10 11 12 13
//initialize smallest...
int smallest = -1;
//get user input [5],[10],[15] these are what were entered....
//compare the first one to the smallest
if (smallest < current) // 5...
//do nothing
else
smallest = current;
//... Now check 10, then 15. Do you see the problem?
*edit* For some reason I overlooked what was in the count == 1 block... that actually fixes your problem, however, the reason why you had to remove the count == 1 block is because you try to use count and it is never initialized.
What I was getting at though was this and is now the problem you are seeing. if front default constructed to "" then when you check to see if name is < front, it will never be true because "" will always be less than any other string but itself.
instead of if (count == 1) just replace with if (initial == 1)
Hope this helps