Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   undeclared indentify error (http://www.velocityreviews.com/forums/t450391-undeclared-indentify-error.html)

inkexit@yahoo.com 12-09-2005 01:21 AM

undeclared indentify error
 
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[i].color >> my_widget[i].size;
*input_file >> my_widget[i].weight >> my_widget[i].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;
}


Winbatch 12-09-2005 01:24 AM

Re: undeclared indentify error
 
Pretty simple, the argument ot the function is in_file, you are using
input_file.
<inkexit@yahoo.com> wrote in message
news:1134091316.588097.119620@g44g2000cwa.googlegr oups.com...
> 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[i].color >> my_widget[i].size;
> *input_file >> my_widget[i].weight >> my_widget[i].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;
> }
>




inkexit@yahoo.com 12-09-2005 01:49 AM

Re: undeclared indentify error
 
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;
}


Ron Natalie 12-09-2005 01:59 AM

Re: undeclared indentify error
 
inkexit@yahoo.com wrote:

> 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).

inkexit@yahoo.com 12-09-2005 02:04 AM

Re: undeclared indentify error
 
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?


Ron Natalie 12-09-2005 02:10 AM

Re: undeclared indentify error
 
inkexit@yahoo.com wrote:

> my_widget[*num_items].color = *item.color;


* binds tighter than .

You want (*item).color or
item->color;

=?iso-8859-1?Q?Ali_=C7ehreli?= 12-09-2005 03:37 AM

Re: undeclared indentify error
 
<inkexit@yahoo.com> wrote in message
news:1134092940.791107.165950@g14g2000cwa.googlegr oups.com...

> 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


red floyd 12-09-2005 03:58 AM

Re: undeclared indentify error
 
inkexit@yahoo.com wrote:
> 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;
}


All times are GMT. The time now is 05:27 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.