No Operator ">>" matches these Operands

closed account (SNhURXSz)
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>

using namespace std;

int main ()
{
int numStudents;

cout << "Enter class size: " << endl;
cin >> numStudents;

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");

if (!inputFile)
{
cout << "Error opening file.\n";
}
else
{
inputFile >> name;
while (!inputFile.eof())
{
inputFile >> name;
}
}

inputFile.close();

string temp;

for (int j=1;j<numStudents;++j)
{
temp = name[j];
int k;

for (k = j-1; k>=0 && name[k] > temp; k--)
{
name[k+1] = name[k];
}

name[k+1] = temp;
}

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.
Last edited on
inputFile >> name;
name is an array of strings.

Try inputting to each string in that array individually. Something like

1
2
3
4
5
6
int i = 0;
while (!inputFile.eof())
{
inputFile >> name[i];
++i;
}
closed account (SNhURXSz)
Thank you!
closed account (SNhURXSz)
Ok so here is my revised code:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main ()
{
int numStudents;

cout << "Enter class size: " << endl;
cin >> numStudents;

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;

for (k = j-1; k>=0 && name[k] > temp; k--)
{
name[k+1] = name[k];
}

name[k+1] = temp;
}

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 goal is to match my professors output exactly.
When I test my code against his I get the following errors:

(I do not know how to upload pictures on this forum so I have a TinyPic link)
[IMG]http://i43.tinypic.com/15ot3pe.png[/IMG]

or

http://i43.tinypic.com/15ot3pe.png

What am I doing wrong? I am stumped!

****My code is on the left. My professors is on the right.
Last edited on
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?
Topic archived. No new replies allowed.