| | |
Simple Inventory Program - file manipulation help
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Nov 2007
Posts: 21
Reputation:
Solved Threads: 0
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:
Here's the code:
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
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:
c++ Syntax (Toggle Plain Text)
#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
Last edited by Ancient Dragon; Dec 18th, 2007 at 10:27 pm. Reason: reformat the error message so that we could see it all -- it was being partly hidden. Note: not a violation of any rules.
>>'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?
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?
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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) Last edited by Ancient Dragon; Dec 18th, 2007 at 10:54 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Nov 2007
Posts: 21
Reputation:
Solved Threads: 0
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:
C++ Syntax (Toggle Plain Text)
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); }
Last edited by Ancient Dragon; Dec 18th, 2007 at 11:39 pm. Reason: replaced icode with code tags. Please don't use icode with more than one line of code.
>>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:
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.
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.
Last edited by Ancient Dragon; Dec 18th, 2007 at 11:45 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Nov 2007
Posts: 21
Reputation:
Solved Threads: 0
now I am getting
at the line I just changed.
C++ Syntax (Toggle Plain Text)
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.
•
•
Join Date: Nov 2007
Posts: 21
Reputation:
Solved Threads: 0
All I really changed is what you posted. Here is the full code:
C++ Syntax (Toggle Plain Text)
#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:
>>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.
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); Last edited by Ancient Dragon; Dec 19th, 2007 at 12:38 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- memory management in wndows 2000 (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: Understanding bitwise
- Next Thread: what is make file
| Thread Tools | Search this Thread |
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting string strings studio temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






