mardi 9 août 2022

How can I create dynamic array without asking user to enter a size in C++?

I want to take 8 char password from user and then when she/he wanted to enter, I wanted it to be whatever size she/he enter. However, the code doesn't stop until it reaches 8 char, even I press 'enter', it takes it as a char and create '*'. I want its size to be whatever user write and then user press 'enter', it will take these characters as password to see if it is matched with registered password. For example;

  • User register password as 'wellcode'
  • After that, user write in login screen 'well' then enter(in my code it lasts until I reach 8 characte
  • 'Login failed! will be written

Is there a way to do that in C++?

    bool CheckCredentials(string);
       char pass[8], inPass[8];
       int main()
        {
            fstream encrypted;
            bool done = false;
            while (!done)
            {
                cout << "1. Register a password" << endl;
                cout << "2. Login your password" << endl;
                cout << "3. Exit" << endl;
                
                int menuChoice;
                cin >> menuChoice;
        
                if (menuChoice == 1)
                {
                    
                    ifstream ifile;
                    ifile.open("password.txt");
                    if (ifile){}
                    else
                    {
                        cout << "New password must contain only 8 characters: ";
                        for (int i = 0; i < 8; i++)
                        {
                            pass[i] = _getch(); _putch('*');
                        }
                        ofstream l("password.txt", ios::out);
                        if (!l.is_open()) {
                            cout << "could not open file \n";
                        }
                        for (int i = 0; i < 8; i++)
                        {
                            l << pass[i];
                        }
                        l.close();
                        cout << "\n\n";
                    }
                }
        
                else if (menuChoice == 2)
                {
        
                        cout << "Password: ";
                        for (int i = 0; i < 8; i++)
                        {
                            inPass[i] = _getch(); _putch('*');
                        }
        
        
                        if (CheckCredentials(inPass) == true)
                        {
                            cout << "\nLogin sucessful!" << "\n\n";
                        }
                        else
                        {
                            cout << "\nLogin failed!"
                        }
        
                }
                else if (menuChoice == 3)
                {
                    done = true;
                    break;
                }
            }
        
            return 0;
        }

bool CheckCredentials(string inPass)
{
    string s;

    bool status = false;

    ifstream f;
    f.open("password.txt");

    if (!f.is_open())
    {
        cout << "Unable to open file!\n";
    }
    else if (f)
    {
        while (!f.eof())
        {
            f >> s;

            if (inPass == s)
            {
                status = true;
            }
            else
            {
                status = false;
            }
        }
    }

    f.close();
    return status;
}

Aucun commentaire:

Enregistrer un commentaire