473,770 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(widge t 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_choic e = '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_st r());
}while(!input_f ile);
import_db(my_wi dget, &input_file, &num_items);

//close file
//from now on, all operations done on my_widget array in local
//memory
input_file.clos e();
break;
}
return 0;
}

Dec 9 '05 #1
7 5017
Pretty simple, the argument ot the function is in_file, you are using
input_file.
<in*****@yahoo. com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.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(widge t 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_choic e = '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_st r());
}while(!input_f ile);
import_db(my_wi dget, &input_file, &num_items);

//close file
//from now on, all operations done on my_widget array in local
//memory
input_file.clos e();
break;
}
return 0;
}

Dec 9 '05 #2
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_choic e = '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_wid get, &item, &num_items);

return 0;
}

Dec 9 '05 #3
in*****@yahoo.c om wrote:
void import_db(widge t 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).e of()
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).
Dec 9 '05 #4
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 '05 #5
in*****@yahoo.c om wrote:
my_widget[*num_items].color = *item.color;


* binds tighter than .

You want (*item).color or
item->color;
Dec 9 '05 #6
in*****@yahoo.c om 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;
}
Dec 9 '05 #7
<in*****@yahoo. com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.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

Dec 9 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4840
by: Saurabh Aggrawal | last post by:
Hi, if (iter->m_name.compare(pstrName) == 0) { // Provide our object. *ppunkItem = iter->m_pUnknown; // Addref our object... iter->m_pUnknown->AddRef(); break; }
9
16219
by: W. Van Hooste | last post by:
Just starting with C, can somebody explain why this does not work or point me in the right direction? I wrote some tools and did some coding but cant seem to get this one. I DID declare my FILE *fp! and still get... isource2.c: In function `getval': isource2.c:5: `FILE' undeclared (first use in this function) Here is my Example project:
5
4138
by: Marc Gustafson | last post by:
This function is executed when a application file is double-clicked to open in a VC++ application. Specifically, this function is enabled by RegisterShellFileTypes () in the application's InitInstance. However, under MFC 7.1 for VC++ .NET 2003, CFrameWnd::OnDDEExecute has a bug that repeatedly generates this error message in the debugger Error: failed to execute DDE command ''.
0
1496
by: Stephanie Doherty | last post by:
Hello World, I am trying to use a _spawnl function like this (and I have included the process.h file): _spawnl(_P_WAIT,iporgfile,iporgfile,NULL); It compiles with the following errors: error C2065: '_P_WAIT' : undeclared identifier
4
22957
by: Peter Rothenbuecher | last post by:
Hello, when I try to compile the following code: /* This fragment of code is taken from an online tutorial */ #include<stdio.h> #include<fcntl.h> #include<stdlib.h> float bigbuff;
6
9892
by: Peter Rothenbuecher | last post by:
Hello, when I try to compile the following code with g++ -o client client.c #include<sys/socket.h> #include<stdio.h> #include<stdlib.h> #define ADDRESS "mysocket"; #define MAXLEN 200;
7
32388
by: michigaki | last post by:
hello, we are having problems in compiling a 'slightly' altered x264. We are always receiving a 'helloWorld' undeclared (first use this function) error. We may be commiting a very simple error but we just have to ask after looking through possible solutions that have not worked. So far, we are able to successfully compile the x263 source (an unaltered one from the VideoLAN downloads section) using the instructions found at...
5
12537
by: vmagana | last post by:
First of all I would like to indicate that I am a newbie a programming. I am having a problem compiling a sample source code that I downloaded from microsoft. When I try to build the program I get an error that 'STORAGE_PROPERTY_QUERY' undeclared identifier. This structure is defined in the ntddstor.h file and is included. I dont understand why it does not see this structure. OS: Windows XP, Visual C++ 6.0, latest SDK and changed the...
7
6723
by: Adam01 | last post by:
Im using cygwin to test the code of a server I am writing. I've included sys/types.h, sys/socket.h, netdb.h, and arpa/inet.h. And this is the output.. ../../../sockets.cpp: In constructor `network_class::network_class()': ../../../sockets.cpp:64: error: aggregate `addrinfo hints' has incomplete type a nd cannot be defined ../../../sockets.cpp:69: error: `getaddrinfo' undeclared (first use this functio n)
0
9617
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10099
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10037
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.