#include <iostream>
#include <fstream>
usingnamespace std;
class Circle{
public:
double radius;
string color;
};
constint n=3;
Circle readin();
int main(){
Circle circarr[n];
for(int i=0;i<n;i++){
cout<<i<<':'<<endl;
circarr[i]=readin();
cout<<circarr[i].radius<<" "<<circarr[i].color<<endl;
}
for(int i=0;i<n;i++){
if(circarr[i].color=="yellow")
cout<<i<<" is porn."<<endl;
}
return 0;
}
Circle readin(){
Circle circ;
ifstream infile("l16.txt");//Here the function only read the first, how to correct it?
infile>>circ.radius>>circ.color;
return circ;
}
it's happen that everytime you call readin(), the function declares a new 'infile' variable (cause it's local), so it reads in the file, returns 'circ', closes file. the next time you call readin(), it starts from the beginning of the file again and again. If im not wrong you should declare infile in the main() function and pass it to the readin() funtion.