I've been working on a project for a few days now, and I am running out of time by trying to research everything I am doing. My task for this particular program is to create an inventory program that reads from a file and then allows the user to manipulate the data and overwrite the file.


I am getting the following error and I don't know how to correct it:

c:\documents and settings\jayson\my documents\visual studio 2008\projects
\solution 3\assignment 3\hardware.cpp(113) : error C2065: 'hardware' : undeclared identifier

c:\documents and settings\jayson\my documents\visual studio 2008\projects
\solution 3\assignment 3\hardware.cpp(113) : error C2228: left of '.seekg' must have class/struct/union
type is ''unknown-type''

Here's the code:

#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string.h>
using namespace std;

const int MAX = 100;
const int MAXINV = 100;
const int ROWS = 4;

void view(char *string2, int * inv);
void edit(char *string2, int * inv);


void main()
{
	fstream hardware;
	int *inv = 0;
	int c = 0;
	char input;
	char string1[MAX];
	char *string2[100][4];
	char *delim = ";";
	char *token;
	hardware.open("hardware.dat", ios::in | ios::out);
	if (hardware.fail())
	{
		cout << "File Open Failed.\n";
		exit(1);
	}

	cout << "Menu\n" << endl
    	 << "Action:            Enter: " << endl
		 << "View Inventory     V" << endl
		 << "Edit Entry         E" << endl
		 << "Quit               Q" << endl;
	cin >> input;

	switch(input)
	{
		case 'V':
		case 'v':
			view(string2[100][4], (int*) *inv);
			break;
		case 'E':
		case 'e':
			edit(string2[100][4], (int*) inv);
			break;
		default:
			exit(1);
	}


	while (! hardware.eof())
	{
		cin.getline(string1, MAX); 
		token = strtok(string1, delim);

		while (token != NULL)
		{
		
			cout << "token " << c << " is " << endl << token << endl;
			if (c >= ROWS)
			{
				c=0;
				inv++;
			}

			string2[*inv][c] = token;
			token = strtok(NULL,delim);
			c++;
		}
	}
	hardware.close();


}
void view(char *string2[100][4], int * inv)
{
	int i,j;
	for (i=0; i <= *inv; i++)
	{
		for (j=0; j <= 4; j++)
		{
			cout << string2[i][j] << endl;
		}
	}
}

void edit(char *string2[100][4], int * inv)
{
	
	int num;
	int i,j;
	char string1[MAX];
	cout << "Enter Inv. Number: ";
	cin >> num;

	for (j=0; j <= 4; j++)
	{
		cout << string2[num][j] << ";" << endl;
	}

	cin.getline(string1, MAX);
	hardware.seekg(0,ios::beg);
		for (i=0; i <= *inv; i++)
	{
		for (j=0; j <= 4; j++)
		{
			cout << string2[i][j] << ";" << endl;
		}
	}
	
}

Is there a better way to do this? I've read half the C++ book over the past few days and my head is spinning :confused:

>>'hardware' : undeclared identifier
where did no define hardware ? You can ignore that second error message because it is related to the first one.

>>Is there a better way to do this?
Fix the error?

I defined it in main(), do I have to define it in each function that uses the file?

You can pass it as a parameter to the functions that need it. And pass it by reference, not by value void edit(char *string2[100][4], int * inv, fstream& hardware)

Thank you. What do I put in the call to the function? I tried several variations, the one with the least amount of errors is this:

switch(input)
	{
		case 'V':
		case 'v':
			view(string2[100][4], (int*) inv, fstream* hardware);
			break;
		case 'E':
		case 'e':
			edit(string2[100][4], (int*) inv, fstream* hardware);
			break;
		default:
			exit(1);
	}

>>edit(string2[100][4], (int*) inv, fstream* hardware);
That line is wrong -- don't put the data types in that line, they only go in the function prototypes and function header, should be this: edit(string2, inv, hardware); . You need to correct the line that calls view too.

Also don't confuse the address operator '&' and the reference operator '&'. The reference operator is used in function prototypes and function declarations to tell the compiler that the parameter is a c++ reference to some object. The address operator is used when calling a function to create a pointer to the object. The two are similar but not the same thing.

now I am getting

error C2664: 'view' : cannot convert parameter 1 from 'char *[100][4]' to 'char *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

at the line I just changed.

Since my eyesight is too poor to see your monitor I am unable to help you any more that that. Post more code so that someone can see what you are doing.

All I really changed is what you posted. Here is the full code:

#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string.h>
using namespace std;

const int MAX = 100;
const int MAXINV = 100;
const int ROWS = 4;

void view(char *string2, int * inv, fstream& hardware);
void edit(char *string2, int * inv, fstream& hardware);


void main()
{
	fstream hardware;
	int *inv = 0;
	int c = 0;
	char input;
	char string1[MAX];
	char *string2[100][4];
	char *delim = ";";
	char *token;
	hardware.open("hardware.dat", ios::in | ios::out);
	if (hardware.fail())
	{
		cout << "File Open Failed.\n";
		exit(1);
	}

	cout << "Menu\n" << endl
    	 << "Action:            Enter: " << endl
		 << "View Inventory     V" << endl
		 << "Edit Entry         E" << endl
		 << "Quit               Q" << endl;
	cin >> input;

	switch(input)
	{
		case 'V':
		case 'v':
			view(string2, inv, hardware);
			break;
		case 'E':
		case 'e':
			edit(string2, inv, hardware);
			break;
		default:
			exit(1);
	}


	while (! hardware.eof())
	{
		cin.getline(string1, MAX); 
		token = strtok(string1, delim);

		while (token != NULL)
		{
		
			cout << "token " << c << " is " << endl << token << endl;
			if (c >= ROWS)
			{
				c=0;
				inv++;
			}

			string2[*inv][c] = token;
			token = strtok(NULL,delim);
			c++;
		}
	}
	hardware.close();


}
void view(char *string2, int * inv, fstream& hardware)
{
	int i,j;
	for (i=0; i <= *inv; i++)
	{
		for (j=0; j <= 4; j++)
		{
			cout << string2[i][j] << endl;
		}
	}
}

void edit(char *string2, int * inv, fstream& hardware)
{
	
	int num;
	int i,j;
	char string1[MAX];
	cout << "Enter Inv. Number: ";
	cin >> num;

	for (j=0; j <= 4; j++)
	{
		cout << string2[num][j] << ";" << endl;
	}

	cin.getline(string1, MAX);
	hardware.seekg(0,ios::beg);
		for (i=0; i <= *inv; i++)
	{
		for (j=0; j <= 4; j++)
		{
			cout << string2[i][j] << ";" << endl;
		}
	}
	
}

>>char *string2[100][4];
That is not the same as in the function prototypes. char * is a single one dimensional array, what you have created is a three-dimensional array. The two are not compatible. What I think you want is this: char string2[100][4]; which is a two-dimensional array, and the function prototype would look like this: void view(char string2[100][4], int * inv, fstream& hardware); >>int *inv = 0;
That is a pointer that points to nowhere. Yet you are passing it to the functions and those functions are attempting to dereference address 0. That might well compile without complaint, but at runtime your program will crash and burn big time. What you are supposed to pass is a pointer to an integer.

int inv = 0;
...
view(string2, &inv, hardware);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.