Very clean. Well-designed. Grade: A.
JSYK, be careful with those underscores. Personally, I like it, and what you are doing here is perfectly fine. But change one of those to something like "_People" or play with it at namespace level and you're driving into 'reserved' territory.
Also, for this kind of stuff,
using namespace std is just fine, but you should be aware that some people will hate you for it anyway.
http://www.cplusplus.com/faq/beginners/using-namespace-std/
I'm not exactly sure why you put it between #include directives. I have a personal habit of alphabetizing my #includes:
1 2 3 4 5 6
|
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
|
When the list gets long it makes it a lot easier to manage. IMHO.
You have an extra (unused) variable in there. With streams, you are just as well to construct them when you are ready to use them:
75 76 77 78
|
int main()
{
AddressBook myAddBook;
ofstream writetofile("AddressBook.dat", ios::app);
|
You can fix it to accept
any number of inputs with only a little extra prompting:
79 80 81 82 83 84 85 86
|
while (true)
{
string nameFromUser, addressFromUser, numberFromuser;
cout << "Give me a name (or just press Enter to finish): ";
getline(cin, nameFromUser);
writetofile << nameFromUser << endl;
if (nameFromUser.empty()) break;
|
Finally, you can get actual numbers from the user in a way similar to how you get text. As I just gave in another post, a useful little routine:
1 2 3 4 5 6 7 8 9 10
|
#include <iostream>
#include <limits>
template <typename T>
std::istream& inputline( std::istream& ins, T& value )
{
ins >> value;
ins.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );
return ins;
}
|
You can use it:
79 80 81 82 83
|
while (true)
{
string nameFromUser;
string addressFromUser;
unsigned numberFromUser;
|
90 91 92
|
cout << "Give me a number: ";
inputline(cin, numberFromUser);
writetofile << numberFromUser << endl;
|
Things to think about.
Good job!
[edit] Responses to
Wyboth
You should only rarely need to explicitly
close() your streams -- let RAII do that. OP's code is correct.
Also, the
typedef changes the meaning of the code. Your 'AddressBook' type is not the same as his 'AddressBook'. OP's code is correct.
Don't give
this->one_size_fits_all rules without reason. It is
not good practice; places where they do it to fix something in their code shows that there is something wrong with their code structure or that they've been infected with suits.
Also, why the injunction for camelCase? Because you like it? That's not a good enough reason to disparage his naming style. Personally, iFindCamelCaseExceptionallyHardToRead. As he isn't doing anything wrong with his naming, and no alternative exists that is superior, I don't think there's any reason to "highly recommend" it, outside of your own personal preferences. If you are going to suggest them, make sure to be clear that it is your preference, and not something industry-normal, as per his Q.