#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>
usingnamespace 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.
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..
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.