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
#include
#include
#include
#include
#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"<> 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<
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?
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
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?
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
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[i] and marker_name[i] 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[i] 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[i].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[i].getMarkerCode();
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
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
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953