如何确保只需按一次 Enter 键并删除重复的“按 Enter 继续...”

问题描述 投票:0回答:1

在我的代码中的大多数“按回车键继续..”操作中,似乎让我按两次回车键才能继续。我不确定是什么导致它显示两次而不是一次。这是问题的输出示例:

              ===============================
                          Main Menu
              ===============================
               1. Register for vaccine
               2. View Profile details
               3. Sign Out

输入您的选择:3 正在退出... 按 Enter 继续...按 Enter 继续...

我尝试删除

cin.get()
cin.ignore();
但没有效果。即使“按 Enter 键继续...”不会显示两次,我仍然需要按 Enter 两次才能进入下一个功能。请帮我解决这个问题。这是我的整个疫苗注册系统代码:

#include<iostream>
#include<string>
#include<cstdlib>
#include<vector>
#include<limits> 
#include<iomanip>
#include<fstream>
#include<conio.h>

using namespace std;

class User {
public:
    string name, username, password, nationality, add, dob;
    int age, icNumber, ph;
    char sex;
    bool registeredForVaccine;
    
    User() : registeredForVaccine(false) {}

    void displayProfile() const {
        cout << " Name: " << name << "\n Age: " << age << "\n IC Number: " << icNumber
             << "\n Registered for Vaccine: " << (registeredForVaccine ? "YES" : "NO") << "\n";
    }
    
    void saveToFile() const {
        ofstream file("users.txt", ios::app);
        if (file.is_open()) {
            file << username << " " << password << " "
                 << age << " " << dob << " " << sex << " "
                 << icNumber << " " << add << " "
                 << nationality << " " << ph << " "
                 << registeredForVaccine << "\n";
            file.close();
        }
    }

    static bool loadFromFile(const string& username, const string& password, User& user) {
    ifstream file("users.txt");
    string line;
    while (getline(file, line)) {
        istringstream iss(line);
        if (iss >> user.username >> user.password &&
            user.username == username && user.password == password) {
            iss >> user.age >> user.dob >> user.sex
                >> user.icNumber >> user.add
                >> user.nationality >> user.ph
                >> user.registeredForVaccine;

            file.close();
            return true;  // User found in file
        }
    }
    file.close();
    return false;  // User not found in file
    }
};

class VaccineRegistrationSystem {
private:
    vector<User> users;  // Store multiple user accounts
    User currentUser;

public:
    void loginPage(const User& user = User()) {
        cout << " Enter username: ";
        cin >> currentUser.username;

        if (currentUser.username.empty()) {
            return; // Go back to the login/sign-up page
        }

        cout << " Enter password: ";

        // Disable echoing
        char ch;
        string password;
        while ((ch = _getch()) != '\r') { // Enter key pressed
            if (ch == '\b') { // Backspace key pressed
                if (!password.empty()) {
                    cout << "\b \b"; // Move back and overwrite with space
                    password.pop_back(); // Remove last character
                }
            } else {
                cout << '*'; // Print an asterisk
                password.push_back(ch); // Add character to the password
            }
        }

        currentUser.password = password;

        // Enable echoing
        cout << endl;

        // Check user information from file
        User loadedUser;
        if (User::loadFromFile(currentUser.username, currentUser.password, currentUser)) {
            cout << " Login successful!\n";
            mainMenu();
        } else {
            cout << " Invalid username or password. Please try again.\n";
            cout << " Press Enter to continue...";
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cin.get();
            loginPage(); // Show login page again if login fails
        }
    }

    void signUpPage() {
        system("cls");
        cout << " Enter Name: ";
        cin.ignore(); // Clear newline character from the buffer
        getline(cin, currentUser.name);

        cout << " Enter date of birth (xx-xx-xxxx): ";
        cin >> currentUser.dob;

        cout << " Enter IC number: ";
        cin >> currentUser.icNumber;

        cout << " Enter a new username: ";
        cin >> currentUser.username;

        cout << " Enter a new password: ";
        cin >> currentUser.password;

        currentUser.registeredForVaccine = true; // Assuming registration is successful by default
        users.push_back(currentUser);  // Add the new user to the vector
        
        // Save user information to file
        currentUser.saveToFile();
    
        cout << " Registration successful!\n";
        mainMenu();
    }

    void forgotPasswordUsernamePage() {
        system("cls");
        cout << " Enter IC number: ";
        cin >> currentUser.icNumber;
    
        bool foundUser = false;
        for (const User& user : users) {
            if (user.icNumber == currentUser.icNumber) {
                foundUser = true;
                cout << " Found username: " << user.username << "\n and password: " << user.password << "\n";
                break;
            }
        }
    
        if (!foundUser) {
            cout << " IC number is not registered. Press enter again to return to login page...\n";
        }
    }

    void mainMenu() {
    int choice;
    do {
        system("cls");
        cout << setw(50) << " ===============================\n";
        cout << setw(40) << " Main Menu\n";
        cout << setw(50) << " ===============================\n";
        cout << setw(43) << " 1. Register for vaccine\n";
        cout << setw(43) << " 2. View Profile details\n";
        cout << setw(31) << " 3. Sign Out\n";
        cout << setw(5) << "\n Enter your choice: ";

        cin >> choice;
        

        switch (choice) {
            case 1:
                registerForVaccine();
                break;
            case 2:
                viewProfileDetails();
                break;
            case 3:
                cout << " Signing out...\n";
                break;
            default:
                cout << " Invalid choice. Please try again.\n";
        }
        // Pause for the user to see the output
        cout << " Press Enter to continue...";
    } while (choice != 3);  // Continue the loop until the user chooses to sign out
}

    void registerForVaccine() {
    system("cls");
    cout << " =============================================================\n";
    cout << "                      Register for vaccine\n";
    cout << " =============================================================\n";
    cout << " Enter full name                 : ";
    
    // Clear newline character from the buffer
    cin.ignore();
    
    // Read the full name
    getline(cin, currentUser.name);

    cout << " Enter age                       : ";
    cin >> currentUser.age;

    // Clear the newline character from the buffer after reading the age
    cin.ignore();
    
    cout << " Enter date of birth (xx-xx-xxxx): ";
    cin >> currentUser.dob;
    
    cout << " Enter sex (F/M)                 : ";
    cin >> currentUser.sex;

    cout << " Enter IC Number                 : ";
    cin >> currentUser.icNumber;
    
    cout << " Enter address                   : ";
    cin >> currentUser.add;
 
    cout << " Enter Nationality               : ";
    cin >> currentUser.nationality;

    cout << " Enter phone number              : ";
    cin >> currentUser.ph;
    
    // Check eligibility based on age
    if (currentUser.age >= 18) {
        currentUser.registeredForVaccine = true;
        // Save user information to file after registration
        currentUser.saveToFile();
        // Display registration successful message
        cout << "\n Registration successful!\n";
    } else {
        currentUser.registeredForVaccine = false;
        // Display ineligibility message
        cout << "\n Registration unsuccessful. Eligibility for vaccine is 18 years and above.\n";
    }
        
    // Pause for the user to see the output
    cout << "\n Press Enter to continue...";
    cin.ignore();
    cin.get();
}


void viewProfileDetails() {
        system("cls");
        cout << " ===============================\n";
        cout << "    View Profile details\n";
        cout << " ===============================\n";

        if (currentUser.registeredForVaccine) {
            cout << " Full Name     : " << currentUser.name << "\n";
            cout << " Age           : " << currentUser.age << "\n";
            cout << " Date of birth : " << currentUser.dob << "\n";
            cout << " Sex           : " << currentUser.sex << "\n";
            cout << " IC Number     : " << currentUser.icNumber << "\n";
            cout << " Address       : " << currentUser.add << "\n";
            cout << " Nationality   : " << currentUser.nationality << "\n";
            cout << " Phone Number  : " << currentUser.ph << "\n";
        } else {
            cout << " User has not registered for the vaccine. Please register first.\n";
        }

    // Pause for the user to see the output
    cout << "\n Press Enter to continue...";
    cin.ignore();
    cin.get();
    }
};
    
int main() {
    VaccineRegistrationSystem registrationSystem;

    do {
        system("cls"); // Use "cls" for Windows
        cout << setw(50) << " ===============================\n";
        cout << setw(41) << " Login/Sign up\n";
        cout << setw(50) << " ===============================\n";
        cout << setw(29) << " 1. Login\n";
        cout << setw(31) << " 2. Sign Up\n";
        cout << setw(48) << " 3. Forgot password/username\n";
        cout << setw(28) << " 4. Exit\n";
        cout << setw(39) << "\n Enter your choice: ";

        int choice;
        cin >> choice;

        switch (choice) {
            case 1:
                registrationSystem.loginPage();
                break;
            case 2:
                registrationSystem.signUpPage();
                break;
            case 3:
                cout << " Exiting program...\n";
                break;
            case 4:
                cout << " Exiting program.\n";
                return 0; // Exit the program
            default:
                cout << " Invalid choice. Please try again.\n";
        }

        // Pause for the user to see the output
        cout << " Press Enter to continue...";
        cin.ignore();
        cin.get();

    } while (true);

    return 0;
}

c++ system registration
1个回答
0
投票

在你的代码中

cin.ignore();
cin.get();

调用

ignore
会读取一个字符。然后调用
get
读取另一个字符。

所以你需要输入两个字符(任意字符,不仅仅是换行符)程序才能继续。

© www.soinside.com 2019 - 2024. All rights reserved.