• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C++ homework help - please!

RGN

Diamond Member
Can i do this? it does not seem to work....


class EmployData
{
private:

getData();
getFile();
//closeFile(); //not created yet
ifstream in_file; //function header?
char Data[546]; // create an array for data from file
public: //temporary so this compiles
void showData(void);
};

EmployData::getFile()
{
in_file.open("SEmploy.txt&quot😉;
cout << &quot;in getFile&quot; << endl;

if (in_file.fail())
{
cout << &quot;the file failed to OPEN please check the file
and try again. &quot; << endl;

exit(1);
}
}


EmployData::getData()
{
//ifstream SEmploy.txt;

//char Data[546];

in_file.get(Data, 546);


char strNum[6], fstName[50], lstName[50];
const char toksep[] = &quot; \n&quot;;
int nNum;
char *ptr;

cout << &quot;in getData&quot; << endl;

ptr = strtok(Data, toksep);
strcpy(strNum, ptr);
nNum = atoi(strNum);
ptr = strtok(NULL, toksep);
strcpy(lstName, toksep);
ptr = strtok(NULL, toksep);
strcpy(fstName, toksep);
}

void EmployData::showData(void)
{
cout << Data[2] << endl;

}



I'm really only concerned as to why I can't seem to get the file open...
 
ok, I figued it out... kinda of I'm having some trouble with that string function now...
 
Ming:
Here is my main, its in a *.cpp file.

#include <iomanip.h>
#include <iostream.h>
#include <conio.h>
#include <fstream.h>
#include <stdlib.h>
#include <string.h>
#include &quot;proj2.h&quot;

void main()
{
EmployData a;
a.getFile();
a.getData();
a.showData();
cout << &quot;hello&quot; << endl;
getch();

//return 0;
}


it compiles and even runs but the problem lies in this section of code:

EmployData::getData()
{
//ifstream SEmploy.txt;

//char Data[546];

in_file.get(Data, 546);


char strNum[6], fstName[50], lstName[50];
const char toksep[] = &quot; \n&quot;;
int nNum;
char *ptr;

cout << &quot;in getData&quot; << endl;

ptr = strtok(Data, toksep);
strcpy(strNum, ptr);
nNum = atoi(strNum);
ptr = strtok(NULL, toksep);
strcpy(lstName, toksep);
ptr = strtok(NULL, toksep);
strcpy(fstName, toksep);
}

------

strNum and fstName, lstName arrays are not getting populated... and so the rest doesn't work...
 
well, your code seems ok to me but

this ---> in_file.get(Data, 546);

confuses me!

what is the 546 doing there??
 
well, I toughtit would read 546 characters from the file that was opened to store in the Data array... no?

I then wanted to pull from the Data array into the str arrays
 
depends of what kind of file do you have assuming you know that.

If it's just a line of data....then use fin.getline(Data,546) this will read until the &quot;/n&quot; or up to 546.

you can also use >> to read the data but it will SKIP the SPACE and LINE BREAK
 
Actually it reads the whole line, BUT only that line...

which looks like this BTW, 57915 Calahan Harry
 
Back
Top