I don't understand what is wrong with a certain line.

Line 30 has an error when trying to compile in Dev C++ along the lines of "No match for "operator >>" followed by a lot of stuff I can't understand.


Here is the code up to the error line:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <iomanip>
#include <stdio.h>              
#include <stdlib.h>        
#include <fstream>
#include <string>
using namespace std;

struct agentlist {
  string name, surname;
  int code;
  double height;
  double weight;
} agentinfo[10];

    int n=0;

int main(){
    
    char directory;
    ifstream fin("Agent_List");
             if(!fin.fail()){
                             cout << "Cannot find data file.";
                             system("pause");
                             exit(0);
                             }
                             
    cout << "Welcome.\n";
                             
    fin >> n >> agentinfo[n].surname >> agentinfo[n].name >> agentinfo[n].code >> agentinfo[n].height >> agentinfo[n].weight >> endl;
                      while(!fin.eof())
                                        {
                                         fin >> n >> agentinfo[n].surname >> agentinfo[n].name
                                          >> agentinfo[n].code >> agentinfo[n].height >> agentinfo[n].weight;
                                        }
                                        cin.ignore();
                                        break;    


What I want this part of my code to do is take a data file (Agent_List) with data in the form of

1 Bond James 007 72 99
2 Bourne Jason 9 70 92
3 Grapefruit Bill 3 60 140

and then fill in my integer n and their respective struct objects with the data.

I've tried to be as specific and fair as possible in my question.. please offer some insight if you can.


-This is part of a homework assignment, but only a small piece of it and I have refrained from getting help thus far but I really don't understand what's wrong with my code.
Last edited on
The compiler does not know what is it an operator

fin >> endl;

because endl is a function.

Moreover this line is invalid in general and has undefined behavior because the order of evalution of functio arguments is undefined in C++.

fin >> n >> agentinfo[n].surname >> agentinfo[n].name >> agentinfo[n].code >> agentinfo[n].height >> agentinfo[n].weight >> endl;

So, for example agentinfo[n].surname can corresponds to agentinfo[0].surname or any other value of the subscript which will be entered.

Last edited on
Thank you for replying.

Simply removing endl solve this issue.
note that the extractor operator (>>) ignores while space so it puts the info into the variable until it hits a white space or a new line. for example
file:
helloworld

file >> mystring1 >> mystring2;
now we output this
std::cout << mystring1 << "\n second string is "<< mystring2;
output is
helloword second string is 

but if we add white space
file:
hello world
and now output the strings
hello second string is world

with this information you can now assume that there is no need to worry about an endl when taking information. also remember that if you do not change the value of n the array will rewrite over the information already in the place n.


Topic archived. No new replies allowed.