Thread: Need a little help with code

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    30

    Need a little help with code

    Hi , im making a police database but im stuck on this code..... When i enter a wrong password and username for my program it still says "you are logged in" how do i changed that to say exit the program

    thanks.


    Code:
    if ((password == "ryan1234") || (username == "ryan"))
    {
    cout <<"You are logged on as :"<< "Ryan Burrows" << endl;
    }
    else 
    {
    cout <<"Please exit the program!" << endl;
    }

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You want to use && instead of ||.

    Also as a general practice, you should never store the password anywhere. Instead store a cryptographic hash of the password, and compare the stored hash to the hash of the input password.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    30
    i dont no how to lol im sort of new to this thing iv should of sed really

  4. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    30
    this is the code up to now.....


    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main ()
    {
    string username = "ryan";
    string password = "ryan1234";
     
    cout<<"Police Database Version 2.3\n" << endl;
    cout<<"Enter Username :";
    getline(cin, username);
    cout << "Login Password :";
    getline(cin, password);
     
    
    if ((password == "ryan1234") && (username == "ryan"))
    {
    }
    else
    {
    system("CLS");
    cout<<"Police Database Version 2.3\n" << endl;
    cout<<"Invalid username" << endl;
    }
    if ((password == "ryan1234") && (username == "ryan"))
    {
    }
    else
    {
    cout <<"Invalid password" << endl;
    }
    
    if ((password == "ryan1234") && (username == "ryan"))
    {
    cout <<"You are logged on as :"<< "Ryan Burrows" << endl;
    }
    else 
    {
    cout <<"Please exit the program!" << endl;
    }
     
                               
    system("pause");    
    }

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    30
    thanks , could you try and sort my problem i have out i did what you said with the && and now when i enter and wrong username and a right password it says wrong username and password if you know what i mean

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    I removed that post because it was a very week hash.

    If you want to do something when only the user name or only the password is wrong, then you need to have an appropriate if statement with a condition that one of them is wrong.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Jul 2012
    Posts
    30
    could you please give me a clue how to do it

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    For example:
    Code:
    if (password != "ryan1234")
      cout << "Invalid password" << endl;
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    Registered User
    Join Date
    Jul 2012
    Posts
    30
    thank so much it worked

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Please indent your code in the future.
    I'd recommend you google "computer cryptography tutorial", too. Learning this is a must due for security purposes today.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  2. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  3. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 07:09 AM
  4. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM