Hi all, this is my first time posting on this site. I am in need of some help.
I have to write a program that prompts the user to enter the number of students in the class, then loops to read in that many names. I have to get the names from a file "LineUp.dat" - Once all the names have been read in, I need to report which student would be at the front of the line and which student would be at the end of the line.
Here is my code so far:
#include <iostream>
#include <string>
#include <fstream>
while (numStudents > 25)
{
cout << "Invalid Input: Class size is restricted to 25" << endl;
cin >> numStudents;
}
while (numStudents < 1)
{
cout << "Invalid Input: Class must have at least 1 student" << endl;
cin >> numStudents;
}
string name[25];
for (int student=1;student <= numStudents; student++)
{
ifstream inputFile;
inputFile.open("LineUp.dat");
cout << name[0] << " should be at the front of the line." <<endl;
cout << name[numStudents-1] << " should be at the end of the line." <<endl;
return 0;
}
My problem:
if (!inputFile)
{
cout << "Error opening file.\n";
}
else
{
inputFile >> name;
while (!inputFile.eof())
{
inputFile >> name;
}
}
When I try to input the names from a file (inputFile >> name) I receive an error: No Operator ">>" matches these Operands
I have no clue what I am doing wrong. Any help with this is much appreciated!
Thank you in advance!
***I believe I have all the necessary header files I need.
if (numStudents < 1)
{
cout << "Invalid Input: Class must have at least 1 student" << endl;
}
else if (numStudents > 25)
{
cout << "Invalid Input: Class size is restricted to 25" << endl;
}
string name[25];
for (int student=1;student <= numStudents; student++)
{
ifstream inputFile;
inputFile.open("LineUp.dat");
int i = 0;
while (!inputFile.eof())
{
inputFile >> name[i];
++i;
}
inputFile.close();
}
string temp;
for (int j=1;j<numStudents;++j)
{
temp = name[j];
int k;
When the user enters an invalid class size, your code doesn't stop. That's the point of detecting an invalid class size. So you can do something about it. Why did you think that was there, if you just go ahead and try to process an invalid class size anyway?