Saving

The program compiles without any errors, but when i run it, the information isn't saved to the .txt file. Any ideas as to what's wrong?

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
 #include "Save.h"
#include <fstream>
#include "QuestsAndAnswers.h"
#include <iostream>

std::ofstream CensusData;

void Save::Open(){
          CensusData.open("CensusData.txt");
}
void Save::SaveToFile(){
    QuestsAndAnswers QnA;

    CensusData << "Name: " << QnA.getFName() << " " << QnA.getLName() << "\n";
    CensusData << "Age: " << QnA.getAge() << "\n";
    CensusData << "Date Of Birth: " << QnA.getDOB() << "\n";
    CensusData << "Address: " << QnA.getAddress() << "\n";
}
void Save::Close(){

    CensusData.close();
}

//header

#ifndef SAVE_H
#define SAVE_H

class Save
{
    public:
        void SaveToFile();
        void Close();
        void Open();
};
#endif // SAVE_H

//QuestsAndAnswers

#include "QuestsAndAnswers.h"
#include <iostream>
#include <string>


QuestsAndAnswers::QuestsAndAnswers(){

    FName = "NULL";
    LName = "NULL";
    Age = 0;
    DOB = "NULL";
    Address = "NULL";
}
std::string QuestsAndAnswers::getFName(){

    return FName;
}
std::string QuestsAndAnswers::getLName(){

    return LName;
}
int QuestsAndAnswers::getAge(){

    return Age;
}
std::string QuestsAndAnswers::getAddress(){

    return Address;
}
std::string QuestsAndAnswers::getDOB(){

    return DOB;
}
void QuestsAndAnswers::SectionOne(){

    std::string fname;
    std::string lname;
    int age;
    std::string address;
    std::string dob;

    std::cout << "What is your First Name? \n";
    std::cin >> fname;
    FName = fname;
    std::cout << "What is your Last Name? \n";
    std::cin >> lname;
    LName = lname;
    std::cout << "How old are you? \n";
    std::cin >> age;
    Age = age;
    std::cout << "What is your Date of Birth? Example: 01/January/1995 \n";
    std::cin >> dob;
    DOB = dob;
    std::cout << "Where do you live? \n";
    std::cin >> address;
    Address = address;
    std::cout << " \n\n End of Section One! Press 'Enter' to move on to the next section \n" << std::endl;
}

//main

#include "Save.h"
#include "QuestsAndAnswers.h"
#include <iostream>

using namespace std;

int main()
{
    QuestsAndAnswers QnA;
    Save save;

    QnA.SectionOne();
    save.Open();
    save.SaveToFile();
    save.Close();
return 0;
}

Any reason why it doesn't save? /:

If it helps, i've singled out the save function and it saves normally, and i've also singled out the QuestsAndAnswers getVar functions and they work as well.. It just doesn't save to the text file when run together though.
Last edited on
In your SaveToFile() method, the QuestsAndAnswers object is local to your function and has nothing in it. You should pass a reference of your class to that function from where ever you fill the QnA object..
Have you checked that it's not saving to a different directory?

EDIT: Err, never mind.
What kooth said is probably right.
Last edited on
I think I've found the problem, but your Your QnA header file is missing so I couldn't test for it, but try changing your save::open to;


1
2
3
4
5
6
void Save::SaveToFile(QuestsAndAnswers &QnA){
    CensusData << "Name: " << QnA.getFName() << " " << QnA.getLName() << "\n";
    CensusData << "Age: " << QnA.getAge() << "\n";
    CensusData << "Date Of Birth: " << QnA.getDOB() << "\n";
    CensusData << "Address: " << QnA.getAddress() << "\n";
}


I think that should fix things for you since you say most everything else is working as expected.
Last edited on
here's the QnA.h file

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
 #ifndef QUESTSANDANSWERS_H
#define QUESTSANDANSWERS_H
#include <string>


class QuestsAndAnswers
{
    public:
        QuestsAndAnswers();
        std::string getFName();
        std::string getLName();
        int getAge();
        void setAddress( std::string address);
        std::string getAddress();
        std::string getDOB();
        void SectionOne();
        std::string FName;
    private:

        std::string LName;
        int Age;
        std::string Address;
        std::string DOB;
};

#endif // QUESTSANDANSWERS_H 
Anyone? /:
newbieg answered you.
By the way, why create a "Save" class if it contains no state ?
I tried newbieg's code but now i get stuck with another error,

C:\Users\Derv\Desktop\Census\Save.cpp|11|error: prototype for 'void Save::SaveToFile(QuestsAndAnswers&)' does not match any in class 'Save'|

then.. i attempt to make the prototypes match by doing this,
1
2
  void Save::SaveToFile(QuestsAndAnswers &QnA);
  
in the Save.h file but, i get hit with twwo errors which i dont understand /:

C:\Users\Derv\Desktop\Census\Save.h|8|error: 'QuestsAndAnswers' has not been declared|
C:\Users\Derv\Desktop\Census\Save.h|8|error: extra qualification 'Save::' on member 'SaveToFile' [-fpermissive]|


and what do you mean it contains no "state"? I'm new the whole c++ class thing
Your prototype is within your save class which is inside of the header, correct?
So look at the other prototypes, they do not need the extra save:: qualifyer.

Your header should now look like this;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//header

#ifndef SAVE_H
#define SAVE_H
#include "QuestsAndAnswers.h"

class Save
{
    public:
        void SaveToFile(QuestsAndAnswers &QnA);
        void Close();
        void Open();
};
#endif // SAVE_H 


Also now that we have included "QuestsAndAnswers.h" in your header you can now remove it from your save.cpp
Last edited on
Topic archived. No new replies allowed.