I need to make a program that asks the user for their name and PIN number, then have it check the accounts file and see if that user is in there. If it finds a match it will output the users account balance. I have the text file set up and the program seems to work but only with the first account in the text file. Any accounts below that are just ignored.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;


int main ()
{     
   bool found=false;       

   int PIN;
   int uPIN;      
   string name;  
   string uname;
   string ID;
   float balance;     
            
          
  
   ifstream infile;
   infile.open ("accounts.txt");

   
   cout<< "Please enter your name:            ";
   cin >> uname;
   cout<< "Please enter your PIN:            ";
   cin >> uPIN;


while(infile)
{
   infile >> ID >> name >> PIN >> balance;
   
   {
       if (uname == name && uPIN == PIN)
           found = true;
   }
}

   {
       if(found == false)
           cout << "DENIED" << endl;
   }
   {
       if (found == true)
           cout << "Approved" << endl;
   }




   infile.close();
   system("pause");
   return 0;
}

Use structure for reading or writing to file

struct student 
{

//require variable
}

Bump! I'd like to know more in depth for how to do this too, preferably without the use of structure functions and it will complicate things a lot more. Can't we create a loop, find userid, check pin in that loop and return true or false based on the match?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.