0

I want to find whether a password entered by the user is valid or invalid. For this case, a valid password is one which contains at least one uppercase letter, one lowercase letter, one number and one dollar sign ($). When I search for the dollar sign the compiler throws an error:

ISO C++ forbids comparison between pointer and integer

This is how I search for the dollar sign:

for(int i = 0; i < x.length(); i++) {
  if(x[i] == "$") {
    dlr = 1;
  }
}

And this is the complete program:

#include <iostream>
#include <string.h>

using namespace std;

int main () {
  string x("");
  cin >> x;
  bool upr = 0;
  bool lwr = 0;
  bool dlr = 0;
  bool num = 0;
  for(int i = 0; i < x.length(); i++) {
    if(isupper(x[i])) {
      upr = 1;
    }

    if(islower(x[i])) {
      lwr = 1;
    }

    if(x[i] == "$") {
      dlr = 1;
    }

    if(isdigit(x[i])) {
      num = 1;
    }
  }
  if(upr == lwr == dlr == num == 1) {
    cout << "The password is valid";
  } else {
    cout << "The password is invalid";
  }
  cout << dlr;
  return 0;
}
  • 3
    Hint: `"$"` and `'$'` are two very different things. – cHao May 30 '18 at 20:23
  • 1
    '$' single quotes not double – Samer Tufail May 30 '18 at 20:23
  • Those password requirements are awful, so hopefully you can fix that unless this is a purely academic exercise. – tadman May 30 '18 at 20:23
  • There's a separate problem: "upr == lwr == dlr == num == 1` doesn't do anything sensible. – Pete Becker May 31 '18 at 00:16
  • [@tadman](https://stackoverflow.com/users/87189/tadman) yes, this is just an exercise. – Mysterious User May 31 '18 at 15:04
  • [@Pete Becker](https://stackoverflow.com/users/1593860/pete-becker) when all conditions are met, all of upr, lwr, dlr and num should be equal to 1. – Mysterious User May 31 '18 at 15:20
  • @MysteriousUser: C++ operators don't work like that. It'll work like `(((upr == lwr) == dlr) == num) == 1`, which will end up true if an even number of conditions is false. (If *all four* are false, for example, then you have `(((false == false) == false) == false) == 1` -> `((true == false) == false) == 1` -> `(false == false) == 1` -> `true == 1`. (BTW, this in't C. Booleans are `true` and `false`, not 1 and 0.) Try `if (upr && lwr && dlr && num)` instead. – cHao Jun 01 '18 at 13:24
  • Actually I didn't know that before, thanks! – Mysterious User Jun 01 '18 at 13:45

1 Answers1

1

"$" is a C string because it uses double quotes, so it's similar to const char*. What you want is a character:

if(x[i] == '$') {
  // ...
}

Where '$' uses single quotes and is a character, or char, which is an integer value. Here "integer" is not to be confused with the specific int type.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • @NeilButterworth Yeah, it's integery-enough that the compiler gets what you're doing. – tadman May 30 '18 at 20:24
  • @NeilButterworth I mean in contrast to an `int` or `long` integer, where "integer" and `int` are not to be confused. – tadman May 30 '18 at 20:26
  • @NeilButterworth I know what you're saying, but what I mean is when people say "integer" in the context of C++ it often means `int` not as in integer vs. real in mathematical terms. – tadman May 30 '18 at 20:28
  • @NeilButterworth Added some clarification. – tadman May 30 '18 at 20:31