Search code examples
c++stringstring-comparison

How to compare two C strings?


#include <iostream>
using namespace std;

int main() {
  char username[50];
  char password[50];
  char passConfirm[50];
  
  cout << "Create a username: ";
  cin >> username;

  cout << "Create a password: ";
  cin >> password;
  
  cout << "Confirm your password: ";
  cin >> passConfirm;

  if (password == passConfirm) {
    cout << "Password confirmed";  
  } else {
    cout << "Password denied";
  }
}

Trying to see if the user's input is the same as the user's other input but I don't know how to do it.

I tried that in order to find out if the password is the same as passConfirm but it won't work and I don't know what to do.


Solution

  • #include <iostream>
    using namespace std;
    
    int main() {
      string username;
      string password;
      string passConfirm;
      
      cout << "Create a username: ";
      cin >> username;
    
      cout << "Create a password: ";
      cin >> password;
      
      cout << "Confirm your password: ";
      cin >> passConfirm;
    
      if (password == passConfirm) {
        cout << "Password confirmed";  
      } else {
        cout << "Password denied";
      }
    }
    

    string is a vector of chars, therefore it is not necessary to create an array of chars