By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,741 Members | 1,979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,741 IT Pros & Developers. It's quick & easy.

Structures and functions when writing/reading to file

P: 17
I am having difficulty defining the function prototypes and corresponding parameters when the function is called from main. Any help is greatly appreciated.

Expand|Select|Wrap|Line Numbers
  1. //The database
  2.  
  3. struct OpAmps {
  4.   char Name[20]; 
  5.   unsigned int PinCount; 
  6.   double SlewRate;
  7.  
  8. //The function prototypes
  9.  
  10. void Enter(OpAmps );
  11. int Save(OpAmps );
  12. void Load(OpAmps );
  13. void Sort(OpAmps );
  14. void Display(OpAmps );
  15.  
  16. //The arguments to be passed from main, where database_length is an unsigned long and OpAmp is of type OpAmps
  17.  
  18.     Enter(OpAmp[database_length], database_length);
  19.         break;
  20.  
  21.       case '2':
  22.         Save(OpAmp, database_length);
  23.         break;
  24.  
  25.       case '3':
  26.         Load(OpAmp, database_length);
  27.         break;
  28.  
  29.       case '4':
  30.         Sort(OpAmp, database_length);
  31.         break;
  32.  
  33.       case '5':
  34.         Display(OpAmp, database_length);
  35.  
  36. //the functions
  37.  
  38. void Enter(OpAmp, int length&);
  39. {
  40. // if the database is full, inform the user
  41.  
  42.     if (length=10)
  43.     {
  44.         cerr << "The database is full" << endl;
  45.     }
  46.  
  47. // if the database is not full, get the data from the user and alter the database length
  48.  
  49.     else cout << "Enter Operational Amplifier name: ";
  50.     cin >> OpAmp.Name;
  51.     cout << endl << "Enter number of pins: ";
  52.     cin >> OpAmp.PinCount;
  53.     cout << endl << "Enter slew rate: ";
  54.     cin >> OpAmp.SlewRate;
  55.     cout << endl;
  56.     length++;
  57.  
  58. }
  59.  
  60.  
  61. void Open(OpAmp, int length&);
  62. {  
  63.   fstream output_file;  // file stream for output
  64.  
  65.   // open the file
  66.   //<enter code here>
  67.   output_file.open(DATABASE_FILENAME, ios::out);
  68.  
  69.   // write length information to file
  70.   //<enter code here>
  71.  
  72.   // write data to file
  73.   //<enter code here>
  74.   output_file << Name << PinCount << SlewRate << endl;
  75.  
  76.   // close the file
  77.   output_file.close();
  78. }
  79.  
  80.  
  81. void Load(OpAmp, int length&);
  82. {  
  83.   fstream input_file;  // file stream for input
  84.  
  85.   // open the file
  86.   //<enter code here>
  87.   input_file.open(DATABASE_FILENAME, ios::in);
  88.  
  89.   // load database length information from file
  90.   //<enter code here>
  91.   input_file.seekg(0*sizeof(int), ios::beg);
  92.  
  93.   // load data from file
  94.   //<enter code here>
  95.   int NumberRead;
  96.   for (int i=0; i=length; i++)
  97.   {
  98.       input_file >> NumberRead
  99.  
  100.   // close the file
  101.   input_file.close();
  102.  
  103. void Display(OpAmp, int length&)
  104. {
  105.     int i = 0
  106.   // if the database is empty, inform the user
  107.   if (length=0)
  108.   {
  109.       cout << "The database is empty";
  110.   }
  111.   // if the database is not empty, display all the elements in the database
  112.   //<enter code here>
  113.   else
  114.   {
  115.       for (i=1;i=length;i++)
  116.       {
  117.           cout << OpAmp[i].Name << OpAmp[i].PinCount << OpAmp[i].SlewRate << endl;
  118.       }
  119.   }
  120. }
  121.  
  122.  
  123. //Sort is not defined at all yet, need help with the passing of the struct and the use of call by value/reference to change the value of database length as the struct entrys increase
  124.  
Mar 11 '07 #1
Share this Question
Share on Google+
10 Replies


DeMan
100+
P: 1,806
I think your greatest difficulty may be that you are trying to pass OpAmps through various functions. (and you seem to be missing at least one })

If you have only one of these structs (that is you want a "global(ish)" struct, you can make a static struct, and you don't need to pass it anywhere.

Otherwise, you can create an instance of the struct, but then you would need to be passing a variable name through to the methods

eg:
Expand|Select|Wrap|Line Numbers
  1. void Enter(OpAmp myOpAmp, int length&);
  2. {
  3. // if the database is full, inform the user
  4.  
  5.     if (length=10)
  6.     {
  7.         cerr << "The database is full" << endl;
  8.     }
  9.  
  10. // if the database is not full, get the data from the user and alter the database length
  11.  
  12.     else cout << "Enter Operational Amplifier name: ";
  13.     cin >> myOpAmp.Name;
  14.     cout << endl << "Enter number of pins: ";
  15.     cin >> myOpAmp.PinCount;
  16.     cout << endl << "Enter slew rate: ";
  17.     cin >> myOpAmp.SlewRate;
  18.     cout << endl;
  19.     length++;
  20.  
  21. }
  22.  
  23.  
Mar 11 '07 #2

P: 17
Thankyou, when i debug the code my main error is when passing the struct into the Save, Load and Display functions, i get the error, cannot convert parameter 1 from OpAmps[10] to 'OpAmps' how would i correct this?
Mar 12 '07 #3

DeMan
100+
P: 1,806
Have yougpt ypour implementation of Save Handy?
Mar 12 '07 #4

P: 17
This is the function prototype for Save

Expand|Select|Wrap|Line Numbers
  1. int Save(OpAmps, unsigned long);
This is the portion in main where save is called

Expand|Select|Wrap|Line Numbers
  1.       case '2':
  2.         Save(OpAmp, database_length);
  3.         break;
This is the function so far

Expand|Select|Wrap|Line Numbers
  1. // Save the database to the file specified by DATABASE_FILENAME. If the file exists it
  2. // simply overwritten without asking the user
  3. // Arguments:
  4. //   (1) the database
  5. //   (2) the length of the database
  6. // Returns: void
  7. //<enter code here>
  8. int Save(OpAmps OpAmp, unsigned long length)
  9. {  
  10.   fstream output_file;  // file stream for output
  11.  
  12.   // open the file
  13.   //<enter code here>
  14.   output_file.open(DATABASE_FILENAME, ios::out);
  15.  
  16.   // write length information to file
  17.   //<enter code here>
  18.  
  19.   // write data to file
  20.   //<enter code here>
  21.   output_file << OpAmp.Name << OpAmp.PinCount << OpAmp.SlewRate << endl;
  22.  
  23.   // close the file
  24.   output_file.close();
  25. }
Mar 12 '07 #5

Ganon11
Expert 2.5K+
P: 3,652
Here's one problem:

Your comments say the function returns void, but it is an int function. You should declare the function as void Save, or return a dummy value of 0.
Mar 12 '07 #6

P: 17
Ive already changed that in the code im working on, took me a while to realise it, my main problem is the error code i referred to in an early post, ive tried a few combinations of arguments to be passed but not getting anywhere
Mar 12 '07 #7

DeMan
100+
P: 1,806
I am guessing (by the Error) that you are trying to pass an array of opAmps into the function (but the function only expects a single instance). If you are trying to pass an array into the method, it needs to be declared to accept an array, so the simplest thing to do would be to to show the method parameters as

int Save(OpAmps[] OpAmp, int length);
Mar 12 '07 #8

P: 17
Having problems with the save function, the debugger gets down to the cout line for opening file and then just stops, i think i need to declare a pointer to the structure but then i get errors in the lines that write the name etc to the file
Expand|Select|Wrap|Line Numbers
  1. void Save(OpAmps OpAmp[], unsigned long length)
  2. {  
  3.   fstream output_file;  // file stream for output
  4.  
  5.   // open the file
  6.   //<enter code here>
  7.   output_file.open(DATABASE_FILENAME, ios::out);
  8.   if (!output_file.good())
  9.   {
  10.       cout << "Could not create file";
  11.       exit (1);
  12.   }
  13.  
  14.   cout << "Opened file to save data to" << endl;
  15.  
  16.   // write length information to file
  17.   //<enter code here>
  18.   output_file << length << endl << endl;
  19.  
  20.   // write data to file
  21.   //<enter code here>
  22.   int i;
  23.   for (i=0;i=length;i++);
  24.   {
  25.       output_file << OpAmp[i].Name << endl;
  26.       output_file << OpAmp[i].PinCount << endl;
  27.       output_file << OpAmp[i].SlewRate << endl <<endl;
  28.   }
  29.  
  30.   // close the file
  31.   output_file.close();
  32. }
Mar 14 '07 #9

P: 17
so i changed the for (i=0;i=length;i++) to (i=0;i<=length;i++) but the file created does not show the data entered by the user which are to be saved... help!!
Mar 14 '07 #10

Ganon11
Expert 2.5K+
P: 3,652
When you open DATABASE_FILENAME to store the info, is the file being created? If so, what are its contents after saving? If not, check what DATABASE_FILENAME contains, and consider exlicitly opening a file such as "DBOutput.dat" or possibly getting the filename from the user.
Mar 14 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.