Simple Inventory Program - file manipulation help

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2007
Posts: 21
Reputation: picklesandmayo is an unknown quantity at this point 
Solved Threads: 0
picklesandmayo picklesandmayo is offline Offline
Newbie Poster

Simple Inventory Program - file manipulation help

 
0
  #1
Dec 18th, 2007
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:

  1. #include <fstream>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <cstdlib>
  5. #include <string.h>
  6. using namespace std;
  7.  
  8. const int MAX = 100;
  9. const int MAXINV = 100;
  10. const int ROWS = 4;
  11.  
  12. void view(char *string2, int * inv);
  13. void edit(char *string2, int * inv);
  14.  
  15.  
  16. void main()
  17. {
  18. fstream hardware;
  19. int *inv = 0;
  20. int c = 0;
  21. char input;
  22. char string1[MAX];
  23. char *string2[100][4];
  24. char *delim = ";";
  25. char *token;
  26. hardware.open("hardware.dat", ios::in | ios::out);
  27. if (hardware.fail())
  28. {
  29. cout << "File Open Failed.\n";
  30. exit(1);
  31. }
  32.  
  33. cout << "Menu\n" << endl
  34. << "Action: Enter: " << endl
  35. << "View Inventory V" << endl
  36. << "Edit Entry E" << endl
  37. << "Quit Q" << endl;
  38. cin >> input;
  39.  
  40. switch(input)
  41. {
  42. case 'V':
  43. case 'v':
  44. view(string2[100][4], (int*) *inv);
  45. break;
  46. case 'E':
  47. case 'e':
  48. edit(string2[100][4], (int*) inv);
  49. break;
  50. default:
  51. exit(1);
  52. }
  53.  
  54.  
  55. while (! hardware.eof())
  56. {
  57. cin.getline(string1, MAX);
  58. token = strtok(string1, delim);
  59.  
  60. while (token != NULL)
  61. {
  62.  
  63. cout << "token " << c << " is " << endl << token << endl;
  64. if (c >= ROWS)
  65. {
  66. c=0;
  67. inv++;
  68. }
  69.  
  70. string2[*inv][c] = token;
  71. token = strtok(NULL,delim);
  72. c++;
  73. }
  74. }
  75. hardware.close();
  76.  
  77.  
  78. }
  79. void view(char *string2[100][4], int * inv)
  80. {
  81. int i,j;
  82. for (i=0; i <= *inv; i++)
  83. {
  84. for (j=0; j <= 4; j++)
  85. {
  86. cout << string2[i][j] << endl;
  87. }
  88. }
  89. }
  90.  
  91. void edit(char *string2[100][4], int * inv)
  92. {
  93.  
  94. int num;
  95. int i,j;
  96. char string1[MAX];
  97. cout << "Enter Inv. Number: ";
  98. cin >> num;
  99.  
  100. for (j=0; j <= 4; j++)
  101. {
  102. cout << string2[num][j] << ";" << endl;
  103. }
  104.  
  105. cin.getline(string1, MAX);
  106. hardware.seekg(0,ios::beg);
  107. for (i=0; i <= *inv; i++)
  108. {
  109. for (j=0; j <= 4; j++)
  110. {
  111. cout << string2[i][j] << ";" << endl;
  112. }
  113. }
  114.  
  115. }

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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,485
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Simple Inventory Program - file manipulation help

 
0
  #2
Dec 18th, 2007
>>'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?
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 21
Reputation: picklesandmayo is an unknown quantity at this point 
Solved Threads: 0
picklesandmayo picklesandmayo is offline Offline
Newbie Poster

Re: Simple Inventory Program - file manipulation help

 
0
  #3
Dec 18th, 2007
I defined it in main(), do I have to define it in each function that uses the file?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,485
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Simple Inventory Program - file manipulation help

 
0
  #4
Dec 18th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 21
Reputation: picklesandmayo is an unknown quantity at this point 
Solved Threads: 0
picklesandmayo picklesandmayo is offline Offline
Newbie Poster

Re: Simple Inventory Program - file manipulation help

 
0
  #5
Dec 18th, 2007
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:

  1. switch(input)
  2. {
  3. case 'V':
  4. case 'v':
  5. view(string2[100][4], (int*) inv, fstream* hardware);
  6. break;
  7. case 'E':
  8. case 'e':
  9. edit(string2[100][4], (int*) inv, fstream* hardware);
  10. break;
  11. default:
  12. exit(1);
  13. }
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,485
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Simple Inventory Program - file manipulation help

 
0
  #6
Dec 18th, 2007
>>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.
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 21
Reputation: picklesandmayo is an unknown quantity at this point 
Solved Threads: 0
picklesandmayo picklesandmayo is offline Offline
Newbie Poster

Re: Simple Inventory Program - file manipulation help

 
0
  #7
Dec 18th, 2007
now I am getting

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

at the line I just changed.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,485
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Simple Inventory Program - file manipulation help

 
0
  #8
Dec 19th, 2007
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 21
Reputation: picklesandmayo is an unknown quantity at this point 
Solved Threads: 0
picklesandmayo picklesandmayo is offline Offline
Newbie Poster

Re: Simple Inventory Program - file manipulation help

 
0
  #9
Dec 19th, 2007
All I really changed is what you posted. Here is the full code:

  1. #include <fstream>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <cstdlib>
  5. #include <string.h>
  6. using namespace std;
  7.  
  8. const int MAX = 100;
  9. const int MAXINV = 100;
  10. const int ROWS = 4;
  11.  
  12. void view(char *string2, int * inv, fstream& hardware);
  13. void edit(char *string2, int * inv, fstream& hardware);
  14.  
  15.  
  16. void main()
  17. {
  18. fstream hardware;
  19. int *inv = 0;
  20. int c = 0;
  21. char input;
  22. char string1[MAX];
  23. char *string2[100][4];
  24. char *delim = ";";
  25. char *token;
  26. hardware.open("hardware.dat", ios::in | ios::out);
  27. if (hardware.fail())
  28. {
  29. cout << "File Open Failed.\n";
  30. exit(1);
  31. }
  32.  
  33. cout << "Menu\n" << endl
  34. << "Action: Enter: " << endl
  35. << "View Inventory V" << endl
  36. << "Edit Entry E" << endl
  37. << "Quit Q" << endl;
  38. cin >> input;
  39.  
  40. switch(input)
  41. {
  42. case 'V':
  43. case 'v':
  44. view(string2, inv, hardware);
  45. break;
  46. case 'E':
  47. case 'e':
  48. edit(string2, inv, hardware);
  49. break;
  50. default:
  51. exit(1);
  52. }
  53.  
  54.  
  55. while (! hardware.eof())
  56. {
  57. cin.getline(string1, MAX);
  58. token = strtok(string1, delim);
  59.  
  60. while (token != NULL)
  61. {
  62.  
  63. cout << "token " << c << " is " << endl << token << endl;
  64. if (c >= ROWS)
  65. {
  66. c=0;
  67. inv++;
  68. }
  69.  
  70. string2[*inv][c] = token;
  71. token = strtok(NULL,delim);
  72. c++;
  73. }
  74. }
  75. hardware.close();
  76.  
  77.  
  78. }
  79. void view(char *string2, int * inv, fstream& hardware)
  80. {
  81. int i,j;
  82. for (i=0; i <= *inv; i++)
  83. {
  84. for (j=0; j <= 4; j++)
  85. {
  86. cout << string2[i][j] << endl;
  87. }
  88. }
  89. }
  90.  
  91. void edit(char *string2, int * inv, fstream& hardware)
  92. {
  93.  
  94. int num;
  95. int i,j;
  96. char string1[MAX];
  97. cout << "Enter Inv. Number: ";
  98. cin >> num;
  99.  
  100. for (j=0; j <= 4; j++)
  101. {
  102. cout << string2[num][j] << ";" << endl;
  103. }
  104.  
  105. cin.getline(string1, MAX);
  106. hardware.seekg(0,ios::beg);
  107. for (i=0; i <= *inv; i++)
  108. {
  109. for (j=0; j <= 4; j++)
  110. {
  111. cout << string2[i][j] << ";" << endl;
  112. }
  113. }
  114.  
  115. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,485
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Simple Inventory Program - file manipulation help

 
0
  #10
Dec 19th, 2007
>>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);
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC