Hello asxxx,
Building off what
zapshe has said.
For both of you. The ".read()" and .write()" functions are C++ functions, but used for binary files. You do not need that yet or want to go that direction right now.
In "main":
1 2 3
|
ofstream file;
file.open("File.txt", ios::out);
|
Line 1 is defined as an "ofstream". As the "o" implies it is already set as an output stream, so in line 3 the "ios::out" is not needed.
Save the "ios::in" and "ios::out" for a stream defined as a "fstream". There you have to tell it if it is for input, output or both.
An alternative to opening the file is:
ofstream file("File.txt");
Not only does this construct an object, but the ctor will open the file at the same time.
zapshe's example of writing to the file will work. You may want to consider making a function for the class to write to the file. Then all you need is to pass the file stream to the function. This way you do not need to use the "get" functions to access the private variables.
I have a thought to consider.
1 2 3 4 5 6 7 8
|
void GetData()
{
cout << "Input name: " << endl;
cin >> name;
cout << "Input ageL " << endl;
cin >> age;
}
|
Line 4 is formatted input. This means it will stop at a white space or "\n" whichever comes first. Leaving anything that is left for the next input. Now for
cin >> age;
it expects a number to be entered and it expects that number to be an "int. If the first character is not an "int" it will cause "cin" to fail and be unusable until you fix it. This is also true if you were to enter".1234". Had "age" been defined as a "double" this would work.
This would work better for your input.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
void GetData(std::string prompt)
{
std::cout << "\n " << prompt << '\n';
cout << "Input name: "; // <--- Removed the 'endl". Puts the input on the same line.
std::getline(std::cin, name);
cout << "Input age: ";
while (!(cin >> age))
{
std::cout << "\n Invalid input! Must be a number\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
cout << "Input age: " << endl;
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
// Clears the input buffer before the next getline.
}
|
I added some things that yo may not have thought of or learned yet. At some point you will learn that about half the code is dealing with problems or potential problems from user input.
To use this the function call is:
1 2
|
a.GetData(" First Name");
b.GetData(" Second name");
|
You can adjust the string to anything that you want.
Your screen will look like this:
First Name
Input name: Bob Smithe
Input age: 50
Second name
Input name: Jane Doe
Input age: 40
|
After that you can use what
zapshe has suggested or write an output function for the class. Your choice.
Andy
Edit:: formatting