c ++ – Student Text Information System


Here are some observations that can help you improve your code.

Do not abuse using std namespace

Putting using std namespace At the top of each program is a bad habit that you would do well to avoid.

Returns something useful from subroutines

Almost every routine is declared as returning empty. Something is wrong there. For example, if add a recording fails for any reason such as invalid input data, it would be nice if the function returns a value indicating this fact.

Eliminate global variables

My rewrite of this code does not use any global variables, so they are clearly neither fast nor necessary. Their removal allows your code to be more readable and maintainable, two important features of a well-written code. Global variables introduce complex links that are difficult to spot and error.

Use a better name

The name flow control is not really useful because it's much more than just reading – all the treatment, including the output, is done there. Names like line are well in context but my file It's not such a good name. The names of variables and functions must indicate to the reader Why they exist, ideally without other necessary comments. So instead of spacer () I would probably name it print_empty_lines () for example.

Use string concatenation

The menu includes these lines:

std :: cout << << Welcome to the student database version 1.101 "<<  n  n;
std :: cout <<<------------------------------------------->"<< ## ;;
std :: cout << What would you like to do? << & # 39;  n & # 39;

Each of these is a separate call to operator << but they do not have to be. Another way of writing would be:

std :: cost
<< "Welcome to the student database version 1.101  n"
<< "<-------------------------------------------> not "
<< "What would you like to do?  N"
// etc.

This reduces the entire menu to a single call to operator << because consecutive strings in C ++ (and in C, for that matter) are automatically concatenated into a single string by the compiler.

Do not duplicate important constants

The file name is currently hard coded (see next suggestion), but worse than that, it is done in five completely independent places. Better to create a constant:

static const char * FILENAME = "namelist.txt";

Consider the user

Instead of having a hard-coded file name, it may be worthwhile to allow the user to control the file's name and location. For that, it would be wise to use a command-line argument, and then pass the file name to the functions as needed.

Better use of objects

The only member function of student is write to which is not a good function name and can also be a static function because of its writing. Instead, I would suggest something like this:

bool appendTo (const std :: string & filename) const {
std :: ofstream datastore {"datastore.txt", std :: ios_base :: app};
if (data store) {
data store <<
"Number:" << number
" nName:" << first name << & # 39; << name
" nDOB:" << birthday
" nAge:" << age
" nGrade:" << note
" not *"
" not ----------------------------------------------- ---  not ";
}
bool result {datastore};
return the result;
}

Note that now, it takes the filename as a parameter and returns true if the writing was successful. This is also const because it does not modify the underlying student Data structure.

Understand the uniqueness of primary

The end of flow control () Is it:

                main (); // restart the process;
}

However, this is not legal C ++. You can not take the address of primary nor call him. This may seem to work with your compiler but it's explicitly do not guaranteed by the standard and is indefinite behavior which means that the program can do something and that it is not predictable. If you have to repeat something, use a loop.