undeclared indentify error

Discussion in 'C++' started by inkexit@yahoo.com, Dec 9, 2005.

  1. Guest

    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;
    }
    , Dec 9, 2005
    #1
    1. Advertising

  2. Winbatch Guest

    Pretty simple, the argument ot the function is in_file, you are using
    input_file.
    <> wrote in message
    news:...
    > 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;
    > }
    >
    Winbatch, Dec 9, 2005
    #2
    1. Advertising

  3. Guest

    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;
    }
    , Dec 9, 2005
    #3
  4. Ron Natalie Guest

    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).
    Ron Natalie, Dec 9, 2005
    #4
  5. Guest

    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?
    , Dec 9, 2005
    #5
  6. Ron Natalie Guest

    wrote:

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


    * binds tighter than .

    You want (*item).color or
    item->color;
    Ron Natalie, Dec 9, 2005
    #6
  7. <> wrote in message
    news:...

    > 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
    =?iso-8859-1?Q?Ali_=C7ehreli?=, Dec 9, 2005
    #7
  8. red floyd Guest

    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;
    }
    red floyd, Dec 9, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Romeo Disca
    Replies:
    1
    Views:
    2,601
    Dennis E. Hamilton
    Aug 31, 2003
  2. Jason Sauer

    Undeclared Tag Error

    Jason Sauer, Feb 6, 2004, in forum: XML
    Replies:
    0
    Views:
    431
    Jason Sauer
    Feb 6, 2004
  3. Replies:
    34
    Views:
    738
  4. jlc488
    Replies:
    4
    Views:
    2,311
    jlc488
    Dec 14, 2009
  5. Xeno Campanoli
    Replies:
    2
    Views:
    240
    Brian Candler
    Jun 3, 2009
Loading...

Share This Page