3

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

}
derk
  • 43
  • 3
  • 1
    Have you checked that `name` and `pincode` contains the data you want? Have you checked what `username` and `password` is? You could do these very easily by just playing `std::cout`s where applicable to print the data, or by using a debugger. – Tas Sep 18 '19 at 05:25
  • 3
    I suggest you start with forgetting about `goto`. It would be a nice first step. Next, you have `struct users` defined, maype consider using it somehow. Then, `cin.ignore()` doesn't quite do what you think it does. Try not using it. – n. 'pronouns' m. Sep 18 '19 at 05:31
  • 3
    See also [How to debug small programs](https://www.google.com/amp/s/ericlippert.com/2014/03/05/how-to-debug-small-programs/amp/) – n. 'pronouns' m. Sep 18 '19 at 05:36
  • You also need to check that your reads from `infile` are successful and that `infile` opened successfully in the first place – Alan Birtles Sep 18 '19 at 05:59
  • Why do you use `cin.sync_with_stdio(0); cin.tie(0);`? – Thomas Sablik Sep 18 '19 at 06:56

1 Answers1

3

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;
}
Alan Birtles
  • 22,711
  • 4
  • 22
  • 44