• Log in or sign up for an account here: https://www.sythe.org/login

quick help, easy script, minor error

dopeness

Newcomer
Joined
May 5, 2009
Sythe Gold
0
Messages
5
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?
 
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;
}
 
quick help, easy script, minor error
thx for the help, i figured it out tho ;d
 
Runescape Gamblingrunescape gambling
Back
Top