In this project we go through how to code a simple program that allows a user to make the selection to either register a new user or log into the program using existing credentials. The project uses several easy beginner C++ concepts including reading and writing to and from text files using the fstream module, and printing data to the terminal window and reading data back from the terminal using the IOStream module!
If you like this video be sure to leave a like and subscribe for tons more great content! Let me know in the comments if you have any questions or what you want so see more of!
Timestamps: 00:00 Intro and Showcase 01:39 Importing Modules and Project Setup 03:15 User selects whether to log in or register new user 04:59 Register a new user, write to a new text file 08:13 Log in as existing user 11:00 Creating the log in checker, reading from text files 15:45 Testing and project outro
what's up you guys welcome back to the
channel in today's video I'm going to
show you how to make a simple user login
and registration form using C plus in
Microsoft Visual Studio
[Music]
so this project is pretty simple but
pretty cool and it has a lot of real
world application because a lot of
programs and projects that you're going
to be building are going to have login
applications where you want to store
user data and you also want to be able
to call that back in the future so the
basic way we're going to write this
project the way it shakes out is we're
going to create the overall project
structure to essentially prompt the user
to make a decision whether they want to
register or whether they want to log in
as an existing user and if they decide
they want to register we'll just prompt
them for username and a password and
then we'll store it to a text file but
if they want to enter if they want to
log in in already registered user then
we'll still prompt them for username and
password but then we'll check for a text
file with that set of information in our
project and if there is one we'll just
prompt successful login and if there
isn't we'll say login failed and will
prompt them to do it again so those are
the basic steps of the project it's is
not going to take very long and it's
going to use a lot of the concepts that
we've already covered in C plus plus so
if anything in this video feels like
it's going too fast for you feel free to
ask the questions that you have in the
comments on this video or go back and
check out my C plus plus tutorials
playlist on the channel and it should
fill you in on anything you're confused
about in this video but without any
further Ado let's get into the tutorial
all right so I'm going to go ahead and
just quickly set up the project to
include some things that we've already
seen and we're going to include the
package I O stream and we're going to
include two others we're going to
include the package fstream which I O
stream I should say and fstream are
basically to handle the like movement of
functions and inputs and outputs onto
the console window so it just basically
improves the interaction back and forth
between the user in the terminal window
with like line out and line in kind of
stuff
and I'm going to include one more module
for this video uh for this project
that's just stream and it's basically
going to
um uh string sorry and it's basically
going to
um allow us to kind of use like some
special characters like new line and
stuff um so those are what all we need
to include and then um as always we're
going to say using namespace uh standard
if you have Microsoft Visual Studio then
when you boot up a new project this is
probably done for you already I just
want to fill in the full project quickly
just so that everyone knows where
everything comes from okay so then as
always we're going to create the main
function and uh this uh question was
asked in a previous video why is it int
Main and that's because uh the main
function which runs by default in your C
plus plus file the default return value
that comes out of the main function is
an integer value so that's the reason
it's always int Main
and in here basically we're going to
start out by asking the user what their
choice is whether they want to register
as a new user or whether they'd like to
log in as an already registered user and
so to do that we're just going to create
a integer that I'll call choice and
we're going to print out instructions to
the terminal that basically say
select a choice
one um to actually let's say select a
choice and then put this backslash uh
this new line character which is
backslash n programmers familiar with
other languages are probably familiar
with backslash and that's pretty
standard and we'll say one
register and then we'll do another
backslash n so this will be on a new
line now and we'll say two
login and then we'll ask them for their
response on another new line so then
we'll say
um backslash your choice and then a
colon and a space and that's where
they'll actually enter the number that
they want okay and then on that uh next
line we'll say c n is going to equal
choice so whatever they just entered is
going to be Choice it doesn't have to be
down here that's one nice thing about C
plus plus formatting this code is going
to run the same whether you put this on
its own line down below or you add it as
a continuation of this line because I
try to keep the text pretty big for my
users so that's a little bit easier to
watch on YouTube My viewers I'm going to
put it down on this next line but you
can play around with that if you want
and put it all in one line okay so now
we're going to actually start using that
variable choice so we're going to say if
Choice equals one which means the user
selected that they want to register a
new user okay
so if choice is equal to one I'm going
to put these curly brackets in and this
is going to be where we say what to do
if you said you wanted to register a new
user we're going to Define two variables
username and password which are the two
things that every uh user is going to
have so that shouldn't surprise you
and then we'll say C out and we're just
going to prompt them just like we did in
selecting a choice we'll say select a
username colon and then I'm not going to
put any new lines in this because that's
such a simple one we'll just have them
respond on the same line and then we'll
say C in and put that in the username
variable and then we'll do the same
exact thing for C out so I'm going to
copy all of this code and the only
difference now is we're going to put it
in a password instead of username and
we're going to say select a password and
then CN and CN is storing the value that
is just entered by the user here into a
variable
and C out is just displaying whatever we
say onto the console window so that's
what cnnc out are I've talked about that
in the past but just want every video to
be able to stand on its own and then
here's where the F stream comes in handy
we're going to be dealing with files for
this project text files and so we need
that uh file stream so we'll say of
stream file okay and so this is
basically creating a new text file for
whatever user we just registered so
we're going to do file dot open and then
we're going to name the file so what
we're doing here is
file.open
username and then we're just going to
add the dot txt a suffix to the file
because basically we're just going to
store these files in our project folder
with whatever username we just entered
so if you entered Pete there's going to
be a peat.txt file in your project
that's going to have inside it the
username and password that was just
entered
alright so that and then make sure you
get your semicolon formatting is
important and then we're going to say
file
username
and end L and then password so this is
where we're actually putting information
in the file this is basically writing
this information into the file and then
we just do file Dot close that's how
easy it is to create and store
information in a text file with C plus
the formatting is a little funky but the
actual code is pretty straightforward
you just create the file with file.open
and you name it with whatever's in these
parentheses and then you just file and
then these arrows and then that's
storing this information inside that
file and then close it up and it's saved
and what we'll do after that is we'll
return to main so that way you can
register as many users as you want until
you decide that you want to log in okay
so this is it for registering new users
now what we want to do is we want to
come outside of these curly brackets
because if you remember remember these
curly brackets were for if choice is
equal to one so we're going to take this
line and now we're going to say what to
do if choice is equal to 2. so we come
down here and instead of if now it's
going to be an else if Choice equals two
just like that and now we need more
curly brackets
and in here we're going to create a new
variable that's going to say whether
we've logged in successfully or not so
we're going to create a Boolean that
I'll call status and we're going to run
this function that I'll call
logging in like that okay and basically
what I'm going to say is if not status
okay so this is basically saying if we
haven't successfully logged in yet and
you can see I'm getting the red squiggly
in Microsoft Visual Studio because I
haven't created the function yet that's
obviously what we're going to be doing
next but I want to kind of talk through
how this logging in mechanism is going
to work in the outside world so
basically we're going to keep trying to
log in until we get a status of one
which is successful login so to do that
we'll say if not status and then new
curlies and we're going to say C out and
we'll just tell them incorrect
in formation
try again just like that and then we
will end the line and semicolon and then
I'm going to throw this system pause in
there it just kind of is like a break to
slow down your code because things move
really fast in the computer world and
this is just good practice after like
you print something on the screen it
gives a user kind of time to read and
follow along before they uh before they
have to make another decision and
continue interacting
but then we'll make this else because
the only way the only two things you can
have for a Boolean are true or false so
we're basically saying if not and then
this is the same thing as saying else if
status like this there's just no reason
to type this in because the only thing
you could have other than well it
wouldn't be out here on its own it would
be you know in these parentheses but
there's no reason to type it that way
because the only thing other than not
having status that you could have is
having status so we're gonna kind of
take this same format of system pause
and returning a variable
except now instead we're going to be
returning one and we're just going to
say
login successful
just like that okay
so this is all we need to do for
processing the login except we have to
create the actual logging in function
obviously so let's go ahead and Define
this logging in all right and what I'm
going to do is I'm actually going to
come up near the top of my project just
under using namespace standard and I'm
going to create a new function that
returns a Boolean and call it logging in
just like we said and in here I'm
putting my curlies for the code that
goes in this and let's create our
variables so we're going to have
actually four variables in this function
we're going to have let me make sure
that's why I called it down here just
logging in just like that yep okay
groovy
alrighty so in this function we're going
to have four strings we're going to have
the username and password from the text
file and then we're also going to have
our entered username and password that
I'll say user and pass I'm make them
shorter
and so basically what we're doing is
we're asking the the user to put in
their information and then we check
through the text files in our project to
see if we have a text file with that
username and then we're going to check
if we do have a text file under that
username we check to see if the password
matches what they've entered as the
password and if it does then we're going
to return a 1 and say successful I
should say because this is a Boolean and
I know we did return 0 and return one
down here
um that that's because int main is
supposed to have an integer
um for a Boolean you say return true
false but honestly in the world of
programming true is a one and false is a
zero so if I say true false or one zero
sometimes
um that's just a quick aside some they
mean the same thing
um so anyways
here's Wonderwall uh so see out uh we're
gonna ask the user to enter the username
and password because now we're building
the function that is them trying to log
in
all right and then whatever they enter
as the username C in to this user uh
function
um actually I guess we'll put the we'll
put the ones they enter into username
and password so that it matches this uh
previous one we did with select a
username and password we put those in
username and passwords we'll do the same
thing for this
and then we'll say enter password in the
same spot and uh then we'll see in that
one into our password variable and now
what we're going to do is if stream and
it's not actually if stream it's input
file Stream So to talk a little bit
because I know I said that's why we need
to include fstream down below
um when we did the uh o f stream that's
output file stream and then up here
we're doing input file stream so that's
why if stream and down below it was uh
of stream okay so a little bit of
background info for you and then we're
going to do read and then username Plus
dot txt so this is where it comes in
handy the format that we've done this is
we've stored every username as
username.txt we know that's the format
of username and password files because
we just wrote it that way so now we can
take the entered username and basically
say okay we've got two lines in there we
know because we defined it and the first
one is the username and then we're going
to do a second get line that is read and
then password right because that's the
order that we put them in there
and so then we can say if the user is
equal to username and then this uh the
way you write and in C plus plus I'm not
sure we've covered this before is and
and the way you say equals for uh
conditions is equals equals
it's C plus plus so why wouldn't
everything be doubled
um but basically we want to check that
the username matches and the password
matches
um and I guess frankly this might seem
like Overkill because the only way you'd
get to a username file is if username
matched but
this is better practice make sure all
the information lines up and then what
we'll say is if everything does line up
then we'll return true which means
you've successfully logged in and this
is not a capital T this is not python
Pete
okay and so then uh what we'll say
outside of those curlies is else and
then we'll put in here what we want to
do if it's not true and so else will be
return false like that okay and so this
is it this is all the code we need look
at that that was
oh that was about 66 lines around there
with some spaces but it was only 15
minutes to build that project and now
let's take a look at
um what we built so let's go ahead and
build that and debug it and I'm going to
make this text big for you guys because
I know a lot of my projects they're too
small let's register some users so let's
start by making a
um Peter it's a good name and select a
password we'll say Batman and then let's
register another user and we'll call
this one Jack and Jack's password is
going to be loser that's my Hilton
brother so he can take it all right and
now let's go ahead and log in and let's
try to log in as Peter but let's try a
wrong password let's say Spider-Man
okay and it tells us incorrect
information try again press any key to
continue so we hit one and close the
window alrighty now if we run that again
let's try this time logging in correctly
alright so select a choice register
login let's log in enter username Pete
enter username oh I guess it's uh not
Pete is it it was Peter got to be better
than that gotta get up pretty early in
the morning
all right let's try logging in again
enter username Peter and password was
Batman all lowercase like that login
successful
so this is where you would use that is
logged in function in your greater
overall project to limit functionality
so until you'd successfully logged in in
your project you would limit their
functionality you wouldn't let them
navigate to other pages
if it was something where they had to
have uh like been successfully
registered and logged in to do anything
in your project okay so that's a pretty
easy pretty basic C plus project that
has some real world application to it
and as we get more and more into C plus
I do hope we Branch out we build more
elaborate projects but I always try to
make sure that on the channel every
stepping stone to get to whatever
project we're building is available to
you so there's no steps that you feel
like you couldn't catch up with so if
you have any questions about what you
saw in this video or something specific
you'd like to see in a future video be
sure to let me know about in the
comments below if you love the content
on the channel you want to become a
super supporter feel free to check out
my patreon link in the description of
this video don't forget to leave a like
on the video subscribe to the channel
for tons more great content and as
always thanks for watching and good luck
with your code thanks bye