undeclared indentify error

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;
}
 
W

Winbatch

Pretty simple, the argument ot the function is in_file, you are using
input_file.
 
I

inkexit

Duh. I wasn't sure if in_file was a keyword I had to use, or if I
should use the name of the file there.
Thanks a bunch!
I'm having one other problem with the program. I'm getting a bunch of
these errors:

error C2228: left of '.material' must have class/struct/union type
error C2228: left of '.color' must have class/struct/union type
error C2228: left of '.size' must have class/struct/union type
error C2228: left of '.weight' must have class/struct/union type

They are being generated by this function:

void add_item(widget my_widget[MAX_SIZE], widget *item, int *num_items)

{

//if user wants to add an item when the array is full, exit
if (*num_items >= MAX_SIZE)
{
cout << "\nSorry, too many items are in the database already.";
cout << "\nProgram now exiting...";
exit (1);
}
else
{
//add item struct to my_widget
my_widget[*num_items].color = *item.color;
my_widget[*num_items].size = *item.size;
my_widget[*num_items].weight = *item.weight;
my_widget[*num_items].material = *item.material;
(*num_items)++;
}
}



Should work woth this slightly different main:

int main()
{
//declare my_widget and program variables
ofstream out_file;
ifstream input_file;
widget item;
char operation_choice = 'x', modify_choice, color_choice;
int size_choice, weight_choice, num_items, item_num;
string material_choice, file_name;


cout << "Color? ";
cin >> color_choice;
cout << "Size? ";
cin >> size_choice;
cout << "Weight? ";
cin >> weight_choice;
cout << "Material? ";
cin >> material_choice;

//store all the new info in a widget struct called item
item.color = color_choice;
item.size = size_choice;
item.weight = weight_choice;
item.material = material_choice;

//add new item to my_widget using add_item function.
add_item(my_widget, &item, &num_items);

return 0;
}
 
R

Ron Natalie

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 is unknown here. Perhaps you wanted in_file?
By the way
(*input_file).eof()
would be equivalently written (more conventionally)
input_file->eof()

However, this is NOT the way to test for EOF on streams. The eof()
flag only becomes set AFTER the input is attempted that hits the end
of file (and fails).
 
I

inkexit

Thanks Ron. I think I've got that one taken care of now. Do you have
any advice on my second problem from my second post?
 
?

=?iso-8859-1?Q?Ali_=C7ehreli?=

void add_item(widget my_widget[MAX_SIZE], widget *item, int *num_items) [...]
{
//add item struct to my_widget
my_widget[*num_items].color = *item.color;
my_widget[*num_items].size = *item.size;
my_widget[*num_items].weight = *item.weight;
my_widget[*num_items].material = *item.material;
(*num_items)++;
}

Aside from the item->color correction, you probably want this:

{
my_widget[*num_items] = *item;
(*num_items)++;
}

Ali
 
R

red floyd

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



[code redacted]

Don't pass iostreams by address. Pass them by reference instead.
Use a vector instead of an arbitrarily sized widget array.

e.g.:

//
// returns the nubmer of items read
int import_db(std::vector<widget>& widget,
ifstream& in_file);
{
// yada yada yada

return num_items;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top