@
DCisthebest
I think you have put in a good effort for your code, but there are some improvements to be made.
First up, avoid using
goto
, use loops instead. I am not saying it should be banned, there are situations where expert might use it, but beginners should avoid it .
Also avoid global variables - they can get messy quickly and cause subtle problems. Try to keep the scope of variables as local as possible.
Next, try to use functions. Compound statements (code between braces) as found in loops,
if
and
switch
or
case
are good candidates for functions. Use of functions makes the code easier to understand because it provides a little bit of abstraction. If there was a function named
GetUserData
, and it works - then this becomes easy to understand:
1 2 3
|
if (LoginSuccess) {
GetUserData();
}
|
The details of
GetUserData
appear after
main()
, so the reader of the code in
main
can think about things at a higher level, rather than be bogged down in detail.
There is a bit of a rule that functions should be no longer 40LOC - and that includes
main()
. Some go for even less. The point is that functions should do 1 well defined thing.
If you name your functions and variables well, then the code becomes self documenting and should almost read like sentences and paragraphs. Or if you like, it tells the story of what the code does. Names like a, b , c ,d aren't good examples of variable names in this context. Consider
FirstName
,
MiddleName
etc. Someone who knows very little about the subject of your code should be able to read the code and not be confused.
Avoid
using namespace std;
- you already have
std::
for most of the code - just need to add it for string as well. There is lots written on the net as to why this is a good idea.
From a style POV it is good to declare 1 variable per LOC. If it is not an STL thing (like std::string say) then you should initialise the variable at the same time as declaration. Comments can be put in to describe things like expected range of valid values.
With variables that are going to have yes / no values consider using a
bool
type. Consider using
SignUpOption
rather than
yes
/
no
.
Try to avoid having magic numbers like 32 in your code. Make them const variables instead, then use the variable name from then on:
1 2 3 4 5 6
|
const unsigned short AgeLimit = 32;
constexpr unsigned short AgeLimit = 32; // in c++11 we can use constexpr
if (answer < AgeLimit) {
// Do stuff
}
|
With your do loop on line 48, there doesn't seem to be a way for the user to exit early.
Duoas is spending a lot of effort & time writing an
excellent FAQ for beginners, there is a Topic about it at the moment. Here is an excellent paper on variable naming that is linked in Duoas' article- well worth reading:
Any way good luck with your coding - I hope that I have helped a bit :+)