Hi all, I have a problem reading data from my file
for example this is my textfile

CODE  MARKER      //Names of columns in my table
PKY     KELLYS
ELM     ELMANS
OOC    OLIVER
QRT     QUATAR

A user is asked for code, when the code is entered, the corresponding name is displayed
eg when a user enters PKY, in the INPUT, KELLS is displayed on the screen

below is my code

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include "stu_data.h"
#include "markernames.h"
using namespace std;



main()
{


const int SIZE = 15;
char marker_code[3+1];
char marker_name[SIZE+1];
int i;
char request[20];


fstream inoutmarker("markernames.txt", ios::in |ios::out);
inoutmarker.seekg(0, ios::beg);


if (inoutmarker.fail())
{
cerr<<"markernames file error"<<endl;
return 1;
}


for(i=0; i< SIZE; i++)
{
inoutmarker >> marker_code >> marker_name;
}
cout<<"Please enter Marker Code: ";
cin >>request;


for(i=0; i < SIZE; i++)
if(strncmp(marker_code, request,3)== 0)
{
cout << marker_code << marker_name<<endl;
}
return 0;



inoutmarker.close();


system("PAUSE");
}

Hi all, I have a problem reading data from my file
for example this is my textfile

CODE  MARKER      //Names of columns in my table
PKY     KELLYS
ELM     ELMANS
OOC    OLIVER
QRT     QUATAR

A user is asked for code, when the code is entered, the corresponding name is displayed
eg when a user enters PKY, in the INPUT, KELLS is displayed on the screen

below is my code

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include "stu_data.h"
#include "markernames.h"
using namespace std;


main()
{

     const int SIZE = 15;
     char marker_code[3+1];
     char marker_name[SIZE+1];
     int i;
     char request[20];

     fstream inoutmarker("markernames.txt", ios::in |ios::out);
     inoutmarker.seekg(0, ios::beg);

          if (inoutmarker.fail())
             {
               cerr<<"markernames file error"<<endl;
               return 1;
             }

           for(i=0; i< SIZE; i++)
           {
            inoutmarker >> marker_code[i] >> marker_name[i];
           }
           cout<<"Please enter Marker Code: ";
           cin >>request;

               for(i=0; i < SIZE; i++)
                if(strncmp(marker_code, request,3)== 0)
                {
                 cout << marker_code << marker_name<<endl;
                }        
         return 0;


         inoutmarker.close();  

         system("PAUSE");  
} 

end quote.

What exactly is the problem? Do you get an error message? If so, what, where, and when? Does it compile? Does it run successfully and give bad results?

the code does compile, but after the user enters the input, it doesn't retrieve data from the txt file, and the screen just disappears

the code does compile, but after the user enters the input, it doesn't retrieve data from the txt file, and the screen just disappears

I can't run it without the input file called "markernames.txt" and the two ".h" files. However, without even seeing them, I see a problem here:

for(i=0; i< SIZE; i++)
{
inoutmarker >> marker_code[i] >> marker_name[i];
}

You've declared SIZE to have a value of 15 and you've declared marker_code as an array of 4 characters. It's going to blow up when i becomes 4. Do you ever get past this loop? Are there error messages?

I can't run it without the input file called "markernames.txt" and the two ".h" files. However, without even seeing them, I see a problem here:

for(i=0; i< SIZE; i++)
{
inoutmarker >> marker_code[i] >> marker_name[i];
}

You've declared SIZE to have a value of 15 and you've declared marker_code as an array of 4 characters. It's going to blow up when i becomes 4. Do you ever get past this loop? Are there error messages?

----------------------------------------------------------------------------------------------------------

U only need one of the header files "markernames.h", and below is it's contents:

#ifndef MARKER_H
#define MARKER_H

class markerdata
{
public:

private:
char marker_code[3+1];
char marker_name[30+1];

};

#endif

if u can, please copy the text file data into any editor and save it as "markernames.txt" and try to run it with the header file too.
thanks

Please use code tags when posting code to this board. You can read how to use them in the watermarks of the message box or in the anouncemts at the top of the board.

main() has return type int. You haven't given it any return type at all. I hope that's a typo.

marker_code and marker_name are single characters, not strings. If you are trying to store more than one string in an array, which makes sense when trying to do the task you describe, then you want to declare marker_code and marker_name both 2 dimensional arrays of char, not single dimensional arrays of char. Maybe something like

const int MAXNUM = 100;
const int CODELENGTH = 3
char marker_code[MAXNUM][CODELENGTH + 1];

which could accomodate up to 100 code symbols, each with up to 3 char variables each.

Your markernames.h file isn't used at all in your code so you might as well not include it in the program. The files name should probably correspond to the name listed in the ifndef statement, too. To use the header file to hold the input file contents you would could declare an array of markerdata objects like this:

markerdata input[MAXNUM];

The individual member variables of each input wouldn't be accessable unless you declared them with public access or unless you declare and define public accessor and mutator methods for th markerdata class. If they were declared with public access they could be accessed like

cin >> input.marker_code;

and if they remain private member variables with accessor methods and mutator methods, which would be the prefered approach, then the accessor and mutator methods could be used something like this:

input.getMarkerCode();

You want something like char marker_code[NUM_CODES_IN_FILE][LENGTH_OF_CODE+1]; You're only allocating space to store a single code at the moment, and your loops are only reading single characters (and overflowing the bounds) as a result.

Edit: Beaten

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.