Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,234 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,685 people online right now. Registration is fast and FREE... Join Now!




very simple login scheme

 
Closed TopicStart new topic

very simple login scheme, Is this code correct as my computer is buggered

Rating  1
josh06
3 Jan, 2007 - 04:15 AM
Post #1

New D.I.C Head
Group Icon

Joined: 27 Oct, 2006
Posts: 46


Dream Kudos: 175
My Contributions
Hi biggrin.gif . Last night. A thought bubble pooped into my head. A (really) simple login program. I cant really try it out on my own compiler cos my computers buggered and turns itself off when compiling a program . So I'd thought I'd seek your wisdom to tell me if my code is right!! Thanks - btw its just for fun and nothing secure - and ive just started learning c++

CODE

#include <iostream>
using namespace std;

int main() {
  char user[50];
  char password[50];

  cout<<"Welcome to the simple login scheme, please enter username and password";
  cin>>user;
  cin>>password;
  if (user == username && password == password) {
     cout<<"Successful Log-in";
     }
  else {
     cout<<"Please try again";
     }
}

So like is that code right - thanks!!!
User is offlineProfile CardPM
+Quote Post

josh06
RE: Very Simple Login Scheme
3 Jan, 2007 - 04:45 AM
Post #2

New D.I.C Head
Group Icon

Joined: 27 Oct, 2006
Posts: 46


Dream Kudos: 175
My Contributions
anyone?

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Very Simple Login Scheme
3 Jan, 2007 - 05:11 AM
Post #3

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
Since you are using character arrays to hold the user input, you would be well advised to make the comparison using the strcmp() function instead of the equality operator. Alternately, you could use string objects instead of character arrays to hold the user input (I would advise this).

The code you've written will indicate when a log in is not successful, but not why...it does not differentiate between an incorrect usrename or password.

Finally, the code itself will generate a couple of errors, as the variables username has not been declared. you are also comparing the user input for password to itself, so when done using the strcmp() function, it will always pass the comparison.

The idea is the right one.
User is online!Profile CardPM
+Quote Post

born2c0de
RE: Very Simple Login Scheme
3 Jan, 2007 - 05:38 AM
Post #4

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,914



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
Well, there are a few errors in your code. Have a look at the corrected code and see where you went wrong.
CODE

#include <iostream>
#include <string.h>

using namespace std;

int main() {
  char user[]="USER"; char pass[]="PASS";
  char username[50];
  char password[50];

  cout<<"Welcome to the simple login scheme, please enter username and password";
  cin>>username;
  cin>>password;
  if (strcmp(user,username)==0 && strcmp(pass,password)==0)
   {
     cout<<"Successful Log-in";
     }
  else {
     cout<<"Please try again";
     }

}

First of all, you must choose the CORRECT username and password combination so that the user entry can be tested with the original.

Secondly, Since you are using character arrays to store strings, they must be compared character by character. So you will need to use the strcmp() function.

Since you are using the standard header files, you can also use this alternative:
CODE

#include <iostream>
#include <string>

using namespace std;

int main() {
  string user="USER",pass="PASS";
  string username;
  string password;

  cout<<"Welcome to the simple login scheme, please enter username and password";
  cin>>username;
  cin>>password;
  if (user==username && pass==password)
   {
     cout<<"Successful Log-in";
     }
  else {
     cout<<"Please try again";
     }
}

This way you can compare the strings using the == and != operators and is easier to write and read, and is portable as well.

EDIT : Dang It...Amadeus was faster wink2.gif
User is offlineProfile CardPM
+Quote Post

josh06
RE: Very Simple Login Scheme
3 Jan, 2007 - 05:54 AM
Post #5

New D.I.C Head
Group Icon

Joined: 27 Oct, 2006
Posts: 46


Dream Kudos: 175
My Contributions
thanks ever so much - did i have the right idea about it tho?
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Very Simple Login Scheme
3 Jan, 2007 - 05:59 AM
Post #6

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,914



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
Yes, you had the right thought process, but you had a tough time trying to implement your idea in code.
Keep practicing till you can dream in code wink2.gif
User is offlineProfile CardPM
+Quote Post

josh06
RE: Very Simple Login Scheme
3 Jan, 2007 - 06:12 AM
Post #7

New D.I.C Head
Group Icon

Joined: 27 Oct, 2006
Posts: 46


Dream Kudos: 175
My Contributions
ok lol - thanks for your help biggrin.gif
User is offlineProfile CardPM
+Quote Post

arem026
RE: Very Simple Login Scheme
6 Sep, 2007 - 01:06 AM
Post #8

New D.I.C Head
*

Joined: 28 Aug, 2007
Posts: 22


My Contributions
does anyone here knows how to create a login scheme using C?
my username is bunyi
my password is conti

pls help me

User is offlineProfile CardPM
+Quote Post

Xing
RE: Very Simple Login Scheme
6 Sep, 2007 - 01:32 AM
Post #9

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
QUOTE(arem026 @ 6 Sep, 2007 - 02:36 PM) *

does anyone here knows how to create a login scheme using C?
my username is bunyi
my password is conti

pls help me

Sure lot of people know that. Where's your effort?
User is offlineProfile CardPM
+Quote Post

Bench
RE: Very Simple Login Scheme
6 Sep, 2007 - 01:53 AM
Post #10

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 630



Thanked: 16 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(arem026 @ 6 Sep, 2007 - 10:06 AM) *

does anyone here knows how to create a login scheme using C?
my username is bunyi
my password is conti

pls help me

As Xing said, you need to do some of the work yourself.

But really, if you're having trouble converting the C++ program in this thread to a C program, then you ought to start out with something simpler, and experiment with writing programs yourself. - You'll only learn from your own experiences & mistakes - you're not going to become a programmer by waiting for replies on a forum.
User is online!Profile CardPM
+Quote Post

no2pencil
RE: Very Simple Login Scheme
6 Sep, 2007 - 02:59 AM
Post #11

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,513



Thanked: 67 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
http://www.dreamincode.net/forums/index.ph...c=32491&hl=
He already has a thread with his questions. He is just kind of high-jacking this one now too.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Very Simple Login Scheme
6 Sep, 2007 - 04:27 AM
Post #12

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
Arem, please stick to your original thread. It helps with continuity.
User is online!Profile CardPM
+Quote Post

Closed TopicStart new topic
Time is now: 12/4/08 03:55PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month