Thread: Implement a simple “password” system that takes a password in the form of a number. M

  1. #1
    Registered User
    Join Date
    Jun 2014
    Posts
    4

    Implement a simple “password” system that takes a password in the form of a number. M

    Hi there I'm having a problem with my code:
    Question: Implement a simple “password” system that takes a password in the form of a number. Make it so that either of two numbers is valid, but use only one if statement to do the check.
    Code:
    #include <iostream>
    
    
    
    using namespace std;
    
    
    
    
    int main ()
    {
    int password;
    
    
    cout << "Enter password\n";
    cin >> password;
    if ( password == "123" || "456" ) // line 13
    {
        cout << "access granted\n";
    }
    else
    {
        cout << "Incorrect password\n";
    }
    }
    Error:\C++\main\main.cpp|13|warning: comparison with string literal results in unspecified behaviour [-Waddress]|
    D:\C++\main\main.cpp|13|error: ISO C++ forbids comparison between pointer and integer [-fpermissive]|

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The reason for the compiler complaints is that "123" is a string literal, not an integral value.

    Although you didn't ask about it, I'll point out another major flaw in your code.

    Although the compiler didn't complain about it the construct
    Code:
      if ( password == some_constant || some_other_constant)
    is not a test of whether password is equal to one of some_constant or some_other_constant. It is actually equivalent to
    Code:
      if ( (password == some_constant) || (some_other_constant != 0))
    If you want to test if password is equal to some_constant or equal to some_other_constant, you need to do this;
    Code:
      if ( (password == some_constant) || (password == some_other_constant))
    I've left brackets around the two comparisons for clarity - they are not actually required since == (compare for equality) has higher precedence than || (logical or).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple password system
    By ex-mortis in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2012, 10:56 AM
  2. Replies: 2
    Last Post: 01-07-2009, 10:35 AM
  3. system() clear password (XP)
    By B-Con in forum C Programming
    Replies: 4
    Last Post: 05-27-2004, 01:12 AM
  4. How can I display the system password
    By fundu725 in forum C Programming
    Replies: 4
    Last Post: 08-04-2002, 10:56 PM
  5. Password form in C program
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 12-29-2001, 06:16 AM