Home > Mobile >  How do I make this work? I'm not good in C
How do I make this work? I'm not good in C

Time:01-07

#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 user input is the same as another user input but I don't know how to do it.

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

CodePudding user response:

char[] is the C way to do strings. If you're going that way, you need to strcmp to compare them.

#include <cstring>

...

if (std::strcmp(password, passConfirm) == 0) { ... }

But a much better, more C -friendly way to do this is to use std::string.

#include <string>

...

std::string password;
std::string passConfirm;

Then the == comparison would work as you expect.

CodePudding user response:

#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

  •  Tags:  
  • c
  • Related