-3

i have to compare two char array if they are equal of not. my first array is perfect but second one is printing garbage. i know why is it printing garbage, because of not finding the \0, which is at the end of the array and it goes on printing the whole array.

but i dont know how to deal with the garbage when i am comparing both of the arrays

class Credentials
{
    string name;
    string userName;
    string phoneNumber;
    char password[12];
    string email;
public:
    Credentials();
    Credentials(string, string);
    friend istream& operator >> (istream& in, Credentials&);
    bool operator == (Credentials&);
    ~Credentials();
};


istream& operator>>(istream& in, Credentials& object)
{
    start:
    //system("cls");
    ConsoleControl* C = new ConsoleControl;
    int* initialX = new int(35);
    int* initialY = new int(4);
    int* border = new int(50);
    char confirmPassword[12];

    C->setCursorPosition(*initialX, *initialY);
    C->makeBorder(*border);
    cout << "Name: ";
    getline(cin, object.name);

    C->setCursorPosition(*initialX, *initialY * 2);
    C->makeBorder(*border);
    cout << "User Name: ";
    cin >> object.userName;

    C->setCursorPosition(*initialX, *initialY * 3);
    C->makeBorder(*border);
    cout << "Phone Number: ";
    cin >> object.phoneNumber;

    C->setCursorPosition(*initialX, *initialY * 4);
    C->makeBorder(*border);
    cout << "Email: ";
    cin >> object.email;

    C->setCursorPosition(*initialX, *initialY * 5);
    C->makeBorder(*border);
    cout << "Password: ";
    int i;
    for (i = 0; i < 12; i++) 
    {
        char ch = _getch();
        if(ch == 13) break;
        object.password[i] = ch;
        _putch('*');
    }   
    cout << object.password; //it doesnt show garbage

    C->setCursorPosition(*initialX, *initialY * 6);
    C->makeBorder(*border);
    cout << "ReEnter Password: ";
    for (i = 0; i < 12; i++)
    {
        char ch = _getch();
        if (ch == 13) {
            break;
            char end = '\0';
            confirmPassword[i] = end;
        }
        confirmPassword[i] = ch;
        _putch('*');
    }
    cout << confirmPassword; //here it shows garbage value


    if (strcmp(object.password, confirmPassword))
    {
        char color[] = "color 4F";
        C->setColor(color);
        system("cls");
        C->setCursorPosition(45, 15);
        cout << "\aPasswords do not match";

    }
    else
    {
        system("cls");
        cout << "MATCHED";
    }


    
    return in;
}

object is an object of a class

1 Answers1

-2

You can't just compare 2 arrays by an equal operator How to Compare 2 Character Arrays

If you really want to compare them in such way then use std::string for storage.

konradk
  • 45
  • 1
  • 3