使用cin.get()时检测Enter键

firstly, i am a beginner and i am still learning c++, my problem is i want to detect if the user pressed enter i tried using if (cin.get() == '\n') but it skips the user's input. Also, in all cases if i pressed enter the program keeps skipping input until it reaches enter the day.

void user :: create_user()
{
    cout << "Enter the user's ID:" << endl;
    cin >> id;
    number_validation(id);
    cout << "Enter the user's Name:" << endl;
    cin.ignore();
    cin.get(name, 30);
    cout << "Enter the user's Email:" << endl;
    cin.ignore();
    cin.get(email, 50);
    cout << "Enter the user's Password:" << endl;
    cin.ignore();
    cin.get(password, 12);
    cout << "Enter the user's Account Type [student, guest, admin, staff]:" << endl;
    cin.ignore();
    cin.get(acc_type, 8);
    cout << "Enter the user's Creation Date:" << endl;
    cout << "Enter the day:" << endl;
    cin >> creation_date.day;
    number_validation(creation_date.day);
    cout << "Enter the month:" << endl;
    cin >> creation_date.month;
    number_validation(creation_date.month);
    cout << "Enter the year:" << endl;
    cin >> creation_date.year;
    number_validation(creation_date.year);
    cout << "Enter the user's Contact number (Max. Char. 3):" << endl;
    cin >> contact_no;
    number_validation(contact_no);
}
评论