I cannot get this program to loop its only pulling the first name from my text f
ID: 3532856 • Letter: I
Question
I cannot get this program to loop its only pulling the first name from my text file no matter how many students I enter and displaying it as the first and last name. Please Help I will rate five stars if someone can help me.
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream inputFile;
int students;
int count;
char names[25];
char first[25];
char last[25];
inputFile.open("F:\LineUp.txt");
inputFile >> names;
cout << "Enter in the number of students in the class: ";
cin >> students;
cin.ignore(25,' ');
while (students < 1 || students > 25)
{
cout << "Enter in a number that is no less than 1 and no more than 25: ";
cin >> students;
}
for (int count = 0; count <= students; count++)
{
if (count == 1)
{
strcpy(first, names);
strcpy(last, names);
}
if (strcmp(names,first) < 0)
{
strcpy(first, names);
}
if (strcmp(names,last) > 0)
{
strcpy(last, names);
}
}
cout << "The first student is " << first << endl;
cout << "The last student is " << last << endl;
inputFile.close();
system("PAUSE");
return 0;
}
Explanation / Answer
you only got the first name from the file with this line:
inputFile >> names
if you want more names, try making a loop.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.