为什么我无法成功登录?

时间:2019-09-18 05:21:44

标签: c++

我是c ++的初学者,在这里遇到了一些问题!

当前,我有一个名为“ users.txt”的文本文件,并且在文本文件中存储了一些数据:

simon
1234
123123

john
4321
34343

weejay
8888
7777

dicson
4444
3333

kendy
5555
9998

首先,用户需要输入用户名和密码才能登录。但是,当我运行程序时,它会直接说我输入了无效的密码或ID,尽管我输入正确。我可以知道如何解决这个问题吗?

#include<iostream>
#include<fstream>
#include<stdlib.h>
using namespace std ;
struct users {
    string name ;
    string pincode ;
    int amount ;

};
int main ()
{
    cin.sync_with_stdio(0);
    cin.tie(0);

     string name[10] ;
     string pincode [10];
     int amount [10];

     users obj [10] ;
    ifstream infile("users.txt");
    for (int i=0; i <10 ; i++)
    {
        infile >> name [i];
        infile >> pincode [i];
        infile >> amount [i] ;
    }

    w:
        string username ;
        string password;

        cout <<"enter username : " << endl ;
        cin.ignore();
        getline(cin, username);
        cout << "enter password : " << endl ;
        cin.ignore();
        getline(cin, password );

        bool islogin = false ;
        for (int i=0; i < 10 ; i++)
        {
               if (password == pincode[i] && username == name[i] )
               {
                    cout << "successfully log in ! " << endl ;
                    islogin = true ;
                    break ;
                }else
                {
                    islogin = false ;
                }
        }
        if (!islogin)
        {
            cout << "sorry you have entered invalid pin code or ID  " << endl ;

            cout << "you want to login again " << endl ;
            cout << "1. yes" << endl ;
            cout << "2. no" << endl ;
            char option ;
            cin >> option ;
            if (option == '1')
            {
                system("cls");
                fflush(stdin);
                goto w ;

            }else
            {
                cout << "thanks for using our system " << endl;
            }
        }

}

1 个答案:

答案 0 :(得分:3)

cin.ignore()只会忽略从cin中读取的下一个字符。我认为您正在尝试ignore解决此问题Why does std::getline() skip input after a formatted extraction?

仅当从格式化的输入(使用>>进行读取)切换到getline时才需要这样做,以忽略流中剩余的换行符(否则std::getline返回空字符串)。正确的调用是:cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');,它将忽略输入中所有剩余的字符,包括第一个换行符。

当您初次使用username时读到cin时,ignore()调用只会忽略用户名的第一个字符。您的第二个ignore()会忽略密码的第一个字符。

一个完整的示例是:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <vector>

struct users {
    std::string name;
    std::string pincode;
    int amount;
};

int main()
{
    std::vector<users> obj;
    std::ifstream infile("users.txt");
    if (!infile)
    {
        std::cout << "users.txt not found\n";
        return 1;
    }
    for (int i = 0; i < 10; i++)
    {
        users user;
        infile >> user.name;
        infile >> user.pincode;
        infile >> user.amount;
        if (!infile)
        {
            break;
        }
        obj.push_back(user);
    }

    bool islogin = false;
    while (!islogin)
    {
        std::string username;
        std::string password;

        std::cout << "enter username :\n";
        std::getline(std::cin, username);
        std::cout << "enter password :\n";
        std::getline(std::cin, password);

        for (auto& user:obj)
        {
            if (password == user.pincode && username == user.name)
            {
                std::cout << "successfully log in !\n";
                islogin = true;
                break;
            }
            else
            {
                islogin = false;
            }
        }
        if (!islogin)
        {
            std::cout << "sorry you have entered invalid pin code or ID\n";

            std::cout << "you want to login again\n";
            std::cout << "1. yes\n";
            std::cout << "2. no\n";
            char option;
            std::cin >> option;
            if (option == '1')
            {
                system("cls");
                // ignore the newline after 1 so that the next std::getline succeeds
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            }
            else
            {
                std::cout << "thanks for using our system\n";
                break;
            }
        }
    }
    return 0;
}