如何修复用户注册码中的输入循环?

时间:2019-05-17 14:22:10

标签: c++

因此,在此任务中,我被要求制作一个代码来注册用户并将其信息存储在一个简单的txt文件中,从而使该代码始终处于活动状态,但是在输入几个序列之后,它会在电子邮件和密码输入上循环播放。

我试图通过在登录屏幕上添加其他条件来解决该问题,但失败了。

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
class user {
private:
    string username, email, password;

public:
    user(string nm, string em, string pass) {
        this->username = nm, this->email = em;
        this->password = pass;
    }
    string get_name() { return this->username; }
    string get_email() { return this->email; }
    string get_pass() { return this->password; }
    ~user() {}
};
ifstream ifs("users.txt");
ofstream ofs("users.txt", fstream::app);
string nm, em, pass; // nm=username, em=email, pass=password
vector<user> vec;
bool check_mail(string em) {
    int count = 0;
    for(int i = 0; i < vec.size(); i++) {
        if(vec[i].get_email() == em) {
            count++;
        }
    }
    if(count == 0) {
        return false;
    } else {
        return true;
    }
}
bool check_pass(string pass) {
    int count = 0;
    for(int i = 0; i < vec.size(); i++) {
        if(vec[i].get_pass() == pass) {
            count++;
        }
    }
    if(count == 0) {
        return false;
    } else {
        return true;
    }
}
void login() {
    static int i = 0;
    cout << "\nEnter email: ";
    cin >> em;
    cout << "\nEnter password: ";
    cin >> pass;
    while(i < vec.size()) {
        if(vec[i].get_email() == em && vec[i].get_pass() == pass) {
            cout << "\nWelcome " << vec[i].get_name() << "!" << endl;
            i = 0;
            break;
        } else {
            i++;
            login();
        }
    }
}
void register_pass() {
    do {
        cout << "\nEnter password: ";
        cin >> pass;
    } while(check_pass(pass));
    cout << "\nChoose desired username to finalise registration: ";
    cin >> nm;
    ofs << nm << " " << em << " " << pass << endl;
}
void register_mail() {
    do {
        cout << "\nEnter email: ";
        cin >> em;
    } while(check_mail(em));
    register_pass();
}
void main_menu() {
    int choice;
    cout << "Hello, Would you like to log in or register?\n"
         << "[1] Login\n[2] Register\n";
    cin >> choice;
    switch(choice) {
    case 1:
        login();
        break;
    case 2:
        register_mail();
        break;
    }
}
int main() {
    while(1) {
        main_menu();
        user x(nm, em, pass);
        vec.push_back(x);
    }
}

我希望代码返回到输入1或2的选择,但是它不会在几次电子邮件和密码登录后出现。

0 个答案:

没有答案