Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why i cannot log in successfully?

Tags:

c++

I'm a beginner for c++ and have some problems here !

currently, i have a text file named "users.txt" and there are a few data stored inside the text file:

simon
1234
123123

john
4321
34343

weejay
8888
7777

dicson
4444
3333

kendy
5555
9998

At first, users need to enter the username and pincode to login. But, when i run the program it will directly said that i've entered invalid pincode or ID although i've entered correctly. May i know how to solve this ??

#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;
            }
        }

}
like image 481
derk Avatar asked Jun 27 '26 16:06

derk


1 Answers

cin.ignore() simply ignores the next character that is read from cin. I presume you are trying to ignore to solve this problem Why does std::getline() skip input after a formatted extraction?.

You only need to do this when switching from formatted input (reading using >>) to getline to ignore the remaining newline in the stream (otherwise std::getline returns an empty string). The correct call is: cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');, this ignores all remaining characters in the input up to and including the first newline.

When you read the username that is the first time you use cin the ignore() call simply ignores the first character of the username. Your second ignore() ignores the first character of the passcode.

A fully working example is:

#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;
}
like image 153
Alan Birtles Avatar answered Jun 29 '26 06:06

Alan Birtles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!