You are here:

C++/Hi David, 1 - Microsoft...


Hi David,  

1 - Microsoft Visual C++ 6.0 professional, on Windows Xp home

2 - I would to build a program that validates a line sequential file.  I will try to outline the general logic , to give you an idea.

a. text file with data in A: drive

b. read from text file into array

c. validate the data.  In this section I will send I am trying to validate a customer code (a 5 digit number) by carrying out a Modulus 11 check, using a weighting factor of 5432. Eg. the example below shows 24686 would be a valid customer number because the last digit is a 6.  

Cust no    2     4      6     8
  x       x     x      x     x
Weight     5     4      3     2

Product    10 + 16 +   18 +   16 = 60

d. read the valid data into the structure, and then to an output file

e. read the invalid data to another, seperate output file.

3.  I have three main problems that come up regardless of what I do. The first is when I try to read from the loop into the structure I get error C2440: cannot convert from 'char' to 'char[6]'.  I can't see why I'm getting this.

The second is I can't work out how to write the validated data to three seperate structures.  

The third is how do you write from the structure to the output file, I thought something like
"    output_file << "pointer to record in structure"  << endl;  "

but thats not working for me.  

Like I say, these are 3 mistakes im making over and over again, regardless of what i do. I've included what I hope would be a working example of my mistakes and nothing else, everything is commented and the actual loops are quite short.  If you can help, great.  



#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 and part numbers

  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 1 & 2
     output_file << code << endl;

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


return 0;

John Hamilton , Thank you for your question.

Please resubmit, showing all actual error messages and including line numbers, at least on the lines referenced by the error messages. Or use some unique strings to show which lines are referenced by which error messages.

It sounds to me that your first error is caused by trying to store an input byte into a pointer instead of into the variable pointed to by the pointer. I can be more specific after you resubmit (unfortunately, I don't have time to study your code in detail).

I don't understand your second error. What do you mean by "write the validated data to three seperate structures"? (Note: the correct spelling is "separate")


output_file << "pointer to record in structure" << endl;

does not work. You must store each variable separately.

David Spector


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]