quick help, easy script, minor error

Discussion in 'Programming General' started by dopeness, Jan 11, 2010.

quick help, easy script, minor error
  1. Unread #1 - Jan 11, 2010 at 9:36 PM
  2. dopeness
    Joined:
    May 5, 2009
    Posts:
    5
    Referrals:
    0
    Sythe Gold:
    0

    dopeness Newcomer

    quick help, easy script, minor error

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        char Password = 'frogger';
        char myPass;
        
        cout<<"enter your password please" << endl;
        cin >> myPass;
        
        if(myPass == Password)
        {
                cout << "successfule log in" << endl;
        }
        else
        {
                cout<< "Entered an incorrect password" << endl;    
        }
        system("PAUSE");
        return 0;
    }
    it always returns false, why?

    am i doing something wrong?
     
  3. Unread #2 - Jan 11, 2010 at 10:41 PM
  4. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    quick help, easy script, minor error

    Be more definitive. The program returns false, or the if statement? The program should ALWAYS return 0 unless there is a runtime error.

    I suggest using scanf() in place of cin in general, by the way, though it hasn't got much to do with your issue here.

    What you're actually doing wrong is using individual characters rather than strings - I'm surprised you're not getting compile errors. A string is a collection of characters, like so:
    Code:
    char pass[] = "foo";
    or
    Code:
    char* pass = "foo";
    When comparing strings, it is best to use the strcmp() function, which is found in the cstring header:
    Code:
    #include <cstring>
    For info on strcmp(), see here: http://www.cplusplus.com/reference/clibrary/cstring/strcmp/

    Here's my version:
    Code:
    #include <cstdio> // Contains basic IO functions
    #include <cstring> // Contains strcmp() function
    
    int main()
    {
        char* actualpass = "foo"; // the actual password
        char inputpass[80]; // string which will store the user's input password
        printf("Enter the password: ");
        scanf("%s", &inputpass);
        if(strcmp(actualpass, inputpass)==0)
            printf("Correct!\n");
        else
            printf("Incorrect!\n");
            
        return 0;
    }
    
     
  5. Unread #3 - Jan 12, 2010 at 7:00 AM
  6. dopeness
    Joined:
    May 5, 2009
    Posts:
    5
    Referrals:
    0
    Sythe Gold:
    0

    dopeness Newcomer

    quick help, easy script, minor error

    thx for the help, i figured it out tho ;d
     
< [Aimbot]Arcanists[Possible payment?] | Problem For School - Finding Average >

Users viewing this thread
1 guest


 
 
Adblock breaks this site