reading a file into a program with a class

May 7, 2008 at 2:35am
ok for whatever reason I can't get anything to work properly.
All the tutors at my university have apparently left for summer already and I have two programs to resolve by friday. I am desperate. I need help please. here is my problem. I need c strings for 20 characters instead of c++ strings (my professor wants it that way) and i need to be able to read in a file instead of user input. I am not getting either to work. WHY??? Please help me. I was doing really well in this class, but I am just not understanding this...Here is what I have
Last edited on May 10, 2008 at 1:50am
May 7, 2008 at 6:14am
Hi...,
What the below piece of code does????

1
2
3
4
5
6
7
    void masterInfo::getname() 
    {
       cin >> id; 
       cin.ignore( 100, '\n');
       cin.getline( name, 21, '\n');
    
    }


Don't U think it will help U to keep track of ur problem if posted under a single heading instead of 2 different posts...
May 7, 2008 at 1:59pm
Well I had a bit different code and question and I thought it better to ask in a different post.
May 9, 2008 at 6:56pm
here is my code...I don't know why it doesn't work. could someone please help me I only have 1 day left in the semester. Thanks
Last edited on May 10, 2008 at 1:50am
May 9, 2008 at 7:57pm
It may be easier rather than posting your entire code, just to post the important bit.

I didnt quite understand your problem so i can only guess.
Is this the sort of thing you are looking for?

memblock = new char [21];
file.read (memblock, 21);

http://www.cplusplus.com/doc/tutorial/files.html
May 10, 2008 at 1:51am
Ok here is the problem. I can't get the file to read in it says it isn't open, any ideas why?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
ifstream itemFile;
    ifstream citemFile;
    
    
    cout << setiosflags(ios::fixed | ios::showpoint) << setprecision(2);  //set decimal places


    itemFile.open("master7.txt");
   if (!itemFile)
   {
      cout << "Can not open item file \"itemfile.txt\"" << endl; 
      system("pause");
      return 1;
   }


    
    //open outputfile
    ofstream fout("payroll_report.txt");
    if (!fout)
    {
    cout << "file not open";
    system("pause");
    }
    
    
    
   
    
         itemFile >> anid;

citemFile.ignore( 100, '\n' );

itemFile.get( aname, 21, '\n');

itemFile >>apayrate >> anumDep >> anempType;

employee.set( anid, aname, apayrate, anumDep, anempType );

cout << "Employee information: "<<endl;
        cout  << "id = " << employee.returnid( ) << endl;
        cout  << "name = " << employee.returnname( ) << endl;
       cout  << "pay rate = " << employee.returnpayrate( ) << endl;
        cout << "dependents = " << employee.returnnumDep( ) << endl;
       cout  << "type = " << employee.returnempType( ) << endl;
Topic archived. No new replies allowed.