1

I'm new to C++. So I made this program that's supposed to be a password thingy. The problem is that when I input the correct password, which is "bobby", it goes straight to the else and not the if.

What is the problem ?

My Code :

#include <iostream>
using namespace std;

int main()
{
char password[] = "bobby";
char passinput[50];
char num[50];

top: 
cout << "Please enter your password: " << endl;
cin >> passinput;
if(passinput==password)
{
    cout << "Correct" << endl;
    cin >> num; 
}
else
{
    cout << "Incorrect, try again" << endl;
    goto top;
}
}
  • http://www.cplusplus.com/reference/string/string/compare/ – Camille G. Feb 20 '14 at 15:40
  • 6
    `goto` [considered harmful](http://en.wikipedia.org/wiki/Considered_harmful). Don't use it. – Maroun Feb 20 '14 at 15:40
  • 1
    @MarounMaroun: "Considered harmful" essays [considered harmful](http://meyerweb.com/eric/comment/chech.html). But yes, you rarely want `goto`, and you certainly don't want it here. – Mike Seymour Feb 20 '14 at 16:12
  • 1
    this question is marked as "c++" not "c", so you should better stick with c++ where possible: use std::string for storage, std::string::getline() for reading from stdin and generally no "go to". – Peter Feb 20 '14 at 16:37
  • `So I made this program that's supposed to be a password thingy.` That is hardly a technical description, is it... – Lightness Races in Orbit Feb 20 '14 at 21:10
  • @MikeSeymour, I think we should consider ""Considered harmful" essays considered harmful" essays harmful... – Griwes Feb 20 '14 at 21:24

4 Answers4

6

Use strcmp to compare c-style strings
Or use std::string instead, since you mark the question c++:

string password("bobby");
string passinput;
string num;
spiritwolfform
  • 2,263
  • 15
  • 16
  • 1
    +1 for recommending `std::string`. It may be useful to work out how `char*` works exactly sometime during the learning process, but in C++ you would hardly ever use that. – CompuChip Feb 20 '14 at 15:45
  • 1
    Whilst this might be a sensible suggestion, it doesn't actually answer the question, which is why the code doesn't work – mjs Feb 21 '14 at 09:32
4

if(passinput==password) simply compares the starting addresses of two strings. Since both strings are at different location, the condition passinput==password evaluated as false and your if body is not executed.
To compare two c-style strings, you can use the srtcmp standard library function in <string.h>.

if(!strcmp(passinput,password)) //strcmp returns 0 if strings are equal. ! is used to make that 0 (false) to 1 (true)
{
    cout << "Correct" << endl;
    cin >> num; 
}
Grizz
  • 320
  • 1
  • 11
haccks
  • 104,019
  • 25
  • 176
  • 264
2

Use string::compare or == only when comparing the equality of C++ strings, otherwise you are only comparing the addresses of the two C-style strings.

To do this in C++, use

std::string password;
std::string passinput;
std::string num;

// if(password.compare(passinput) == 0) also would work
// but as noted in the comments, is overkill for what you
// are doing
if (password == passinput)
{
    cout << "Correct" << endl;
    cin >> num; 
}
else
{
    cout << "Incorrect, try again" << endl;
    goto top;
}

To do this in C (which is currently the style of string you are using), see @haccks answer. For the sake of convenience, I've added the part of his answer that describes how to do this below.

To compare two C-style strings, you can use srtcmp standard library function in .

// strcmp(passinput, password) returns 0 for
// a successful match.  The ! (negation)
// converts 0 (false in c) to true
if(!strcmp(passinput,password))
{
    cout << "Correct" << endl;
    cin >> num; 
}
Josh Durham
  • 1,632
  • 1
  • 17
  • 28
0

Try :

if (strcmp (passinput,password) ==0)
{
...
}

strcmp returns 0 if strings are same, a value greater than zero when the first character that does not match has a greater value in first string than in second string and a value less than zerowhen its the opposite.

Include cstring/string.h