Access to private class members

Hello there, I am a complete new beginner here. I am trying to load a .txt file which contains a few variables and display them. I am not sure how to access private members so I have set them all to be public. how can I make my public class members to private and be able to access from my main.cpp script?

Here is what I have done so far.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class patient_data {
public :
string Name;
string DOB;
int Nscan;
string File;
int N;
public:
void setName (string name) {Name = name;}
void setDOB (string dob) {DOB = dob;}
void setNscan (int nscan) {Nscan = nscan;}
void setFile (string file) {File= file;}
void setN (int n) {N= n;}

};

int main ()
{
ifstream myfile("patient1.txt");
string Name;
string DOB;
int Nscan;
string File;
int N;
if (!myfile)
{
cout << ("Error: data.txt not found");

}

patient_data patient_data [5];
for(int i = 0; i < 5; i++)
if(myfile >> Name >> DOB >> Nscan >> File>> N)
{
patient_data [i].setName(Name);
patient_data [i].setDOB(DOB);
patient_data [i].setNscan(Nscan);
patient_data [i].setFile(File);
patient_data [i].setN(N);

cout << "Name: "<< patient_data[i].Name << " \n";
cout <<"Date of Birth:" <<patient_data[i].DOB << " \n";
cout << "Number of scan:"<<patient_data[i].Nscan << " \n";
cout << "fMRI: "<< patient_data[i].File << " \n";
cout << "N:"<<patient_data[i].N <<endl;
myfile.close();
}
}
One possibility:
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
47
48
49
50
51
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class patient_data {
    string Name;
    string DOB;
    int    Nscan;
    string File;
    int    N;
public:
    void set(string name, string dob, int nscan, string file, int n) {
        Name = name;
        DOB = dob;
        Nscan = nscan;
        File = file;
        N = n;
    }
    void print();
};

void patient_data::print() {
    cout << "Name: "            << Name  << '\n';
    cout << "Date of Birth: "   << DOB   << '\n';
    cout << "Number of scans: " << Nscan << '\n';
    cout << "fMRI: "            << File  << '\n';
    cout << "N: "               << N     << "\n\n";
}

int main () {
    ifstream myfile("patient1.txt");
    if (!myfile) {
        cout << ("Error: data.txt not found");
        return 1;
    }

    patient_data patients[5];
    string Name, DOB, File;
    int Nscan, N;
    for (int i = 0; i < 5; i++)
        if (myfile >> Name >> DOB >> Nscan >> File >> N)
            patients[i].set(Name, DOB, Nscan, File, N);

    myfile.close();

    for (int i = 0; i < 5; i++)
        patients[i].print();
    
    return 0;
}

Last edited on
First, please use code tags when posting code. See http://www.cplusplus.com/articles/jEywvCM9/


Why should the main() have to access private members of some class type?

Look at your code. How do you set the members of a patient? The main() does not access them. Your class has five public member functions (all names start with "set").

What if you had a public member function:
1
2
3
4
5
6
7
void patient::print() const {
  cout << "Name: "<< Name << " \n";
  cout <<"Date of Birth:" << DOB << " \n";
  cout << "Number of scan:"<<Nscan << " \n";
  cout << "fMRI: "<< File << " \n";
  cout << "N:"<<N <<endl;
}



PS. The myfile seems to close already after the first patient. That is probably a logical error.
Hi there, thank you for you help.
I have another question.
This patient1.txt also includes the name of another txt file called MRI.txt as following;

Ben
13/07/1950
1
MRI.txt
1


I would like to also display MRI.txt once the patient1.txt is loaded and put the variables in the MRI.txt into private classes. The contents of MRI.txt are as follow;


21/10/2016
St. Thomas' Hospital
36
0 fmri_vols_0.hdr
0 fmri_vols_1.hdr
0 fmri_vols_2.hdr
0 fmri_vols_3.hdr


Here's the code in my header file fMRI.h

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
#include <iostream>
#include <string>
#include <fstream>

class MRI_data {
    string ScanDate;
    string Hospital;
        int   N_scan;
        string HDR;
        
    public:
        void get(string ScanDate, string Hospital, int N_scan, string HDR) {
            ScanDate = ScanDate;
            Hospital =Hospital;
            N_scan = N_scan;
            HDR = HDR;
        }
        void print();
    };
    
    void MRI_data::print() {
        cout << "Date of Scan: " << ScanDate  << '\n';
        cout << "Place of scan "   << Hospital  << '\n';
        cout << "Number of scans: " << N_scan << '\n';
        cout << "HDR values: "            << HDR  << "\n\n";
    }



And here's my main.cpp

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
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <string>
#include <fstream>
#include "patient_data.h"
#include <fstream>
#include "fMRI.h"
using namespace std;

int main () {
    ifstream myfile;
    string filename;
    cout<<"Please Enter file Name:"<<endl;
     cin>>filename;
    myfile.open(filename);
    if (!myfile) {
        cout << ("Error: data.txt not found");
        return 1;
    }
    
    patient_data patient[5];
    string Name, DOB, File;
    int Nscan;
    
    for (int i = 0; i < 4; i++)
        if (myfile >> Name >> DOB >> Nscan >> File)
            patient[i].get(Name, DOB, Nscan, File);
    
    for (int i = 0; i < 1; i++)
        patient[i].print();
    
    myfile.close();
    
    
    ifstream myMRIFile;
    myMRIFile.open("File");
    if (!myfile) {
        cout << ("Error: MRI.txt not found");
        myMRIFile.close();
        return 1;
    }
    MRI_data MRI[10];
    string ScanDate;
    string Hospital;
    int   N_scan;
    string HDR;
    
    for (int i = 0; i < 10; i++)
        if (myMRIFile >> ScanDate >> Hospital >> N_scan >> HDR)
            MRI[i].get(ScanDate, Hospital, N_scan, HDR);
    
    for (int i = 0; i < 1; i++)
        MRI[i].print();


    return 0;
}


I am getting an error when opening the MRI.txt file.
Thank you for your help in advance.
Last edited on
closed account (E0p9LyTq)
I am getting an error when opening the MRI.txt file.

as in "Error: MRI.txt not found" maybe?

If that is the error your .txt file is not where the program expects to find it. Usually in the same location as your .exe file.

If you are using an IDE such as Visual Studio, and running the program from within the IDE requires the .txt file to be where your source files are located.
I am using Xcode and I have both text files saved in the same locations. The patient1.txt file is being read but the MRI.txt is not loading at all. Is there any other way to load the variable "File" which corresponds to "MRI.txt" ?
closed account (E0p9LyTq)
Look real close at line 35: myMRIFile.open("File");.

You are trying to open a filed named "File". Not a variable that contains "MRI.txt".

myMRIFile.open("MRI.txt");
I have done that because the file name could change depending on the input file "patient1.txt".

Eg. "patient2.txt" may have "MRI2.txt" . The name of the MRI.txt changes according to the user input of "patient.txt".

So the aim is to display the following but the data are stored in two separate .txt files and file "MRI.txt" comes from the "patient.txt".

Ben
13/07/1950
1
MRI.txt
1

21/10/2016
St. Thomas' Hospital
36
0 fmri_vols_0.hdr
0 fmri_vols_1.hdr
0 fmri_vols_2.hdr
0 fmri_vols_3.hdr



closed account (E0p9LyTq)
I have done that because the file name could change depending on the input file "patient1.txt".

Then call it File, not "File".

Look at lines 11, 13 & 14.
Last edited on
Thank you very much! :)
1
2
3
4
5
6
7
8
9
    string File;
    for (int i = 0; i < 4; i++)
        if (myfile >> Name >> DOB >> Nscan >> File)
            patient[i].get(Name, DOB, Nscan, File);
    
    myfile.close();
    
    ifstream myMRIFile;
    myMRIFile.open("File");

You do read a filename from myfile and store it in string File.
However, you do attempt to open a file that is literally named "File".

If you want to use the name stored in the variable, then you have to use the
variable:
myMRIFile.open( File );


You do have a second issue. You do read data of four patients. Each presumably has their own MRI data. However, you don't read MRI for each patient. You do it after all patients. If the string File still holds a valid name, it is the name from the last patient's entry.
Your could:
1
2
3
4
5
6
for (int i = 0; i < 4; i++) {
  if (myfile >> Name >> DOB >> Nscan >> File) {
    patient[i].get(Name, DOB, Nscan, File);
    // read MRI data for patient[i] from File
  }
}
Last edited on
Topic archived. No new replies allowed.