Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > undeclared indentify error

Reply
Thread Tools

undeclared indentify error

 
 
inkexit@yahoo.com
Guest
Posts: n/a
 
      12-09-2005
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;
}

 
Reply With Quote
 
 
 
 
Winbatch
Guest
Posts: n/a
 
      12-09-2005
Pretty simple, the argument ot the function is in_file, you are using
input_file.
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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;
> }
>



 
Reply With Quote
 
 
 
 
inkexit@yahoo.com
Guest
Posts: n/a
 
      12-09-2005
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;
}

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      12-09-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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).
 
Reply With Quote
 
inkexit@yahoo.com
Guest
Posts: n/a
 
      12-09-2005
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?

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      12-09-2005
(E-Mail Removed) wrote:

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


* binds tighter than .

You want (*item).color or
item->color;
 
Reply With Quote
 
=?iso-8859-1?Q?Ali_=C7ehreli?=
Guest
Posts: n/a
 
      12-09-2005
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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

 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      12-09-2005
(E-Mail Removed) 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;
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to indentify context root of url? jlc488 Java 4 12-14-2009 09:10 AM
Compile Error: ldap.c:424: error: ‘LDAP_OP T_X_TLS_PROTOCOL’undeclared (first use in this function) Xeno Campanoli Ruby 2 06-03-2009 09:02 AM
indentify if argument is char or float yaniv.dg@gmail.com C Programming 34 08-08-2006 04:54 PM
Undeclared Tag Error Jason Sauer XML 0 02-06-2004 09:36 PM
IE Error: Reference to undeclared namespace prefix Romeo Disca XML 1 08-31-2003 06:41 AM



Advertisments