I
inkexit
I'm getting these two error mesages when I try to compile the below
source code:
error C2065: 'input_file' : undeclared identifier
error C2228: left of '.eof' must have class/struct/union type
The code below was snipped from a larger program for
anyone-who-might-want-to-help-me's convienience. That's why there are
varibales declared in the main that are not used in the program, and
why the main has such drastic indentation. However, I have tried to be
sure to include everything needed to understand the problem.
Thanks very much in advance,
-R
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
const int MAX_SIZE = 50;
struct widget
{
char color;
int size;
float weight;
string material;
};
void import_db(widget my_widget[MAX_SIZE], ifstream *in_file, int
*num_items)
{
int i = 0;
*num_items = 0;
while( !(*input_file).eof()) //THIS IS THE LINE THAT GENERATES THE
ERRORS
{
*input_file >> my_widget.color >> my_widget.size;
*input_file >> my_widget.weight >> my_widget.material;
//must check for EOF
if( !(*input_file).eof())
{
i++;
}
}
*num_items = i;
}
int main()
{
//declare my_widget and program variables
ofstream out_file;
ifstream input_file;
widget my_widget[MAX_SIZE], item;
char operation_choice = 'x', modify_choice, color_choice;
int size_choice, weight_choice, num_items, item_num;
string material_choice, file_name;
do
{
cout << "\nEnter the name of the file to import: ";
//grab the filename
getline(cin, file_name);
//try to open the file
input_file.open(file_name.c_str());
}while(!input_file);
import_db(my_widget, &input_file, &num_items);
//close file
//from now on, all operations done on my_widget array in local
//memory
input_file.close();
break;
}
return 0;
}
source code:
error C2065: 'input_file' : undeclared identifier
error C2228: left of '.eof' must have class/struct/union type
The code below was snipped from a larger program for
anyone-who-might-want-to-help-me's convienience. That's why there are
varibales declared in the main that are not used in the program, and
why the main has such drastic indentation. However, I have tried to be
sure to include everything needed to understand the problem.
Thanks very much in advance,
-R
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
const int MAX_SIZE = 50;
struct widget
{
char color;
int size;
float weight;
string material;
};
void import_db(widget my_widget[MAX_SIZE], ifstream *in_file, int
*num_items)
{
int i = 0;
*num_items = 0;
while( !(*input_file).eof()) //THIS IS THE LINE THAT GENERATES THE
ERRORS
{
*input_file >> my_widget.color >> my_widget.size;
*input_file >> my_widget.weight >> my_widget.material;
//must check for EOF
if( !(*input_file).eof())
{
i++;
}
}
*num_items = i;
}
int main()
{
//declare my_widget and program variables
ofstream out_file;
ifstream input_file;
widget my_widget[MAX_SIZE], item;
char operation_choice = 'x', modify_choice, color_choice;
int size_choice, weight_choice, num_items, item_num;
string material_choice, file_name;
do
{
cout << "\nEnter the name of the file to import: ";
//grab the filename
getline(cin, file_name);
//try to open the file
input_file.open(file_name.c_str());
}while(!input_file);
import_db(my_widget, &input_file, &num_items);
//close file
//from now on, all operations done on my_widget array in local
//memory
input_file.close();
break;
}
return 0;
}