You are here:

C++/how to split a record into structure fields?



Ok, sorry for not giving you enough information last time.  I will send you what should be a complete program, its not long and ive tried to label and comment everything so it should be easy to follow.  Its just im not sure what bits you need and what you dont to see if you can answer.  

I will lay out the question again

1) Im using Microsoft Visual C++ 6.0 professional

2)  I have written a for loop to validate a 5 digit customer code number using modulus 11 checks.  I would like to put valid numbers into 3 seperate structures. So, for example if 24686 is a valid number then I would write it to the customer code field in structure a, the customer code field in structure b and the customer code field in structure c.  I hope this explains what im trying to do ok.

//**** PROGRAM

//John Hamilton

   #include <iostream.h>
   #include <fstream.h>
   #include <stdlib.h>
   #include <string.h>
   #include <iomanip.h>
   #include <ctype.h>


//define the structures

   struct I_R_RECORD {        
  char code[6];  //customer code

   struct D_RECORD {       
  char code[6];  //customer code

   struct C_RECORD {      
     char code[6]; //customer code
//define union

   union myUnion {
  struct I_R_RECORD struct1;
  struct D_RECORD   struct2;
   struct C_RECORD   struct3;

   const int MAX = 150;
   union myUnion aUnion;

     int main()

//create an instance of the structures, and a pointer to the structures
  struct I_R_RECORD I_STR, *ptr_I;
   struct D_RECORD D_STR, *ptr_D;
   struct C_RECORD C_STR, *ptr_C;

//assign the pointers the structures addresses

  ptr_I = &I_STR;
  ptr_D = &D_STR;
  ptr_C = &C_STR;


//declare input and output files

   ifstream input_file;
  ofstream output_file;
   fstream  outFile, prnFile;

//open input file(test data)"A:532939TD.DAT", ios::in|ios::nocreate);
  if( //check for open failure
     cout << endl << "Could not open the file"
         << endl;

//open output file (validated data)"A:532939VF.DAT", ios::out);
     cout << endl << "File not open"
         << endl;

//open output file (unvalidated data)"A:532939UV.TXT", ios::out);
     cout << endl << "File not open"
         << endl;

//open printer for output"lpt1", ios::out);
//Test for file and printer open failure

   if(!input_file.is_open() | !output_file.is_open() | !outFile.is_open() | !prnFile.is_open())
     cout << endl << "Could not open the file or printer"
        << endl;

//declare arrays

  char  array [MAX];
   char  *array_ptr;
  int num=0, end = 0;
  int total=0;
  int weights[] = { 5,4,3,2},*wptr=weights;      // weights for customer codes
   int pweights[] = { 6,5,4,3,2}, *pwptr=pweights;// weights for part numbers

// read data from file to array

  while(input_file.peek() !=EOF)
     input_file.getline(array, MAX);

//modulus checks customer codes

  for(num = 0; num < 5; num++)
     array[num] = toupper(array[num]);
  for(array_ptr = array; *(array_ptr+1); array_ptr++)
     num = *array_ptr - '0';
     num *= *wptr;
     total += num;

  end = total % 11;
   end = 11 - end;
  if ((array[4] -'0') == end)
     aUnion.struct1.code =  array[4];  //**** error c2440: '=' cannot convert from 'char' to 'char [6]'
     output_file << code << endl;      //**** error c2065: 'code' undeclared identifier

  else if ((array[4] -'0') != end)
       outFile << code << endl;          


return 0;



--------------------Configuration: otago_MODIFIED - Win32 Debug--------------------
C:\Documents and Settings\John\My Documents\Computeach\otago_MODIFIED.cpp(173) : error C2440: '=' : cannot convert from 'char' to 'char [6]'
       There are no conversions to array types, although there are conversions to references or pointers to arrays
C:\Documents and Settings\John\My Documents\Computeach\otago_MODIFIED.cpp(175) : error C2065: 'code' : undeclared identifier
Error executing cl.exe.

otago_MODIFIED.obj - 2 error(s), 0 warning(s)

any suggestions gratefully recieved



John, Here are some errors I found:

1. In the line

output_file << code << endl;    //**** error c2065: 'code' undeclared identifier

the variable "code" is not declared. It is true that you have structure member declarations for "code", but you are not permitted to omit the structure name. You might try something like this:

output_file << I_STR.code << endl;

2. The constant MAX is probably too small. If you feed a file containing long lines into your program, it will probably crash.

3. The line

aUnion.struct1.code = array[4]; //**** error c2440: '=' cannot convert from 'char' to 'char [6]'

Is incorrect. The right-hand-side is a character (the fifth character in array 'array'), while the left-hand-side is an array of six characters. If you are trying to store the contents of 'array' into 'code', you might try something like this:

lstrcpyn(&I_STR.code, array, 6);

4. You may be misunderstanding the concept of Union. A Union is a memory location or memory block that contains one or more variables that share the space. For example, if I declare

union U
short S;
long L;
U u;

Then the variable "u" represents 4 bytes of memory that can be addressed as a long (u.L) or as a short (u.S). If you store into one of these variables, you must assume that the other one is now undefined. Unions are very rarely used, and then only when memory space is at a premium.



All Answers

Answers by Expert:

Ask Experts


David Spector


Highly knowledgeable in the C++ language, Visual C++ (MSVC), Windows API, documentation and other quality-assurance techniques, and debugging. Knowledgeable in MFC, COM, GUI design, and object-oriented design.


I have been a software engineer since 1965. I have been published. My specializations have been: biomedical programming, compiler implementation, and many kinds of Windows programming. I don't do Databases or other business-oriented stuff.

Windows?/DOS Developer's Journal, ACM SIGPLAN Notices, and Computer Science Press.

ICCP Systems Programming Certification
Master's degree equivalent in Computer Science

©2017 All rights reserved.

[an error occurred while processing this directive]