i think this part got problem..anyone can guide me?

ifstream BanKing;
     BanKing.open("BankData.txt");
     
                                            
     while (!BanKing.eof())
     {
                             
       BanKing>>loginName;
       BanKing>>password;
			
			
     if(loginName == loginName1 && password == password1)
     {                  
         menu();
         break;             		
	    }
				        
     else 
     {
                            
        cout<<"\tUnregistered ID or you have enter a wrong password !"<<endl;
           
           cin.clear();
           cin.ignore();
           getchar();
           return ;  
       }
			    
       BanKing.close();
                                }
       
       }

i got a function that get the input of login id and password and store in a file,

void registeration()
             {
                  
                  fstream BanKing;
                  
                  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
                  cout <<"\tEnter 'Q' to return to quit."<<endl;
                  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
                  cout<<"\tEnter desired login ID: ";
                  getline(cin, loginName);
                  cout<<endl;
                  
                  
                  if(loginName == "q" || loginName == "Q")
                  {
                               system("cls");
                               SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 13);
                               header3();
                               return ;
	              }
	
                  cout<<"\tEnter desired password(in digit): ";
                  cin>>password;
                  cout<<endl;
                  
                  if (password >= 10000)
                               {        
                                        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
                                        cout <<" \t\t\t------------------------------"<<endl;
                                        cout <<"\t\t\t [Password secure level : Strong]"<<endl;
                                        cout <<"\t\t\t ------------------------------"<<endl;
                                        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
                                        }
                  if (password < 10000)
                               {       
                                       SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
                                       cout <<"\t\t\t  ----------------------------"<<endl;
                                       cout<<" \t\t\t [Password secure level : Weak]"<<endl;
                                       cout <<"\t\t\t  ----------------------------"<<endl;
                                       SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
                                       cout<<"\tPlease insert a new 5-digits password : ";
                                       cin>> password ;
                                       cout<<"\tYour password has been changed !! " <<endl;
                                       }       
                                       
                                       BanKing.open("BankData.txt", ios::out);
	                                   if (BanKing.is_open())
	                                   
		                               BanKing<<loginName <<"\t";
		                               BanKing<<password <<"\t";
		
                                       BanKing.put('\n');
		                               BanKing.close();
	                                
	                                   
	                                   reminder();
	                                   
	                                   cout<<endl;
	                                      
                                       registerProfile();  
                                       
                                       system("cls");
                               }

then i use a function that can login by compare the login id and password inserted..
the problem is after i inserted the password and login id to login, it just jump out and close the cmd
what is the problem?

text file:
BankData.txt
hao 12345

savedData.txt
1
1
10000
0
0

Recommended Answers

All 6 Replies

I don't see anything obvious about the login...maybe you could describe the problem you're having better?

As not all of the code required to validate the login is shown, I'm presuming that all of the variable declarations are ok and that the user input for the login and password don't have problems.

I'm not sure why you have the while loop that starts on line 5 in your first code fragment. You never loop as the if statement either causes the while to break (in the true clause) or return (in the else clause).

I didn't really look at the second code fragment, but I did look at the listed contents for BankData.txt and it appeared to be ok.

The problem with your program is in it's design.

Here you throw everything inside of a huge loop... and yes, since you are testing eof() which is highly unrecommended, the loop will make at least 2 iterations:

ifstream myfile("BankData.txt");
     
     if (myfile.is_open())
     {                                            
          while (!myfile.eof())
          {                             
               myfile>>loginName;
               myfile>>password;
			
	       if(loginName == loginName1 && password == password1)
               {                  
                      menu();                  		
	       }				        
               else 
               {
                      cout<<"\tUnregistered ID or you have enter a wrong password !"<<endl;
           
                      cin.clear();
                      cin.ignore();
                      getchar();
                      return ;  
               }
			    
          myfile.close();
          }
    }

Not only that, you close() your ifstream obect, so on the 2nd iteration, your loop condition will fail.. causing your program to close.

Now, I will attempt to fix this mess:

if (!myfile.is_open())
     {            
          cout << "\a\nError! File did not open.";
          cout << "\nCheck if file exists, is in specified location, has been renamed, or deleted.";
          cout << "\nProgram will now terminate in preparation for file troubleshooting.";     
          cout << "\nPress [Enter] to exit program... ";
          cin.get();
          exit(EXIT_FAILURE);
     }                       
                                    
     myfile>>loginName;
     myfile>>password;
			
     if(loginName == loginName1 && password == password1)
     {                  
          menu();                  		
     }				        
               
     else 
     {
          cout << "\tUnregistered ID or you have enter a wrong password !" << endl;           
          cin.clear();
          cin.ignore();
          getchar();
          return;  
     }
			    
     myfile.close();

This is just one implemented solution of many possible. I know it's probably not exactly what you want. Feel free to tweak it as necessary to come up with your own solution.

still can't
i tried on a more simple cpp

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class bank
{
      string loginID, loginID2;
      long password, password2;
      
      public:
      void login();
      void registeration();
      
      };
      
void bank::registeration()
{
     cout<<"Enter loginID: ";
     getline(cin,loginID);
     
     
     cout<<"Enter password: ";
     cin>>password;
     
     fstream banking;
     banking.open("banking.txt", ios::out);
     if(banking.is_open())
     banking<<loginID<<"\t";;
     banking<<password<<"\t";;
     
     banking.put('\n');
     banking.close();
     login();
     }

void bank::login()
{
     cout<<"\tPlease enter 1 login"<<endl;
     cout<<"\tEnter 0 to exit"<<endl;
     cout<<"\tSelect: ";
	                  
     int answers;
     cin>>answers;
	                  
    
	  
     system("cls");
                                    
     if(answers == 1)
     {
        
        cout<<"\tLogin ID: ";
        getline(cin, loginID2);
        cin.ignore(100, '\n');
        cout<<endl;
        cout<<"\tPassword: ";
        cin>>password2;
        cin.ignore(100, '\n');
        }
       
       else if(answers == 0)
     {
          return ;
          }
                      
                     
             
   
                      
     fstream banking;
     banking.open("banking.txt", ios::in);
     
    if (!banking.is_open())
   
      {
   
      cout << "\a\nError! File did not open.";
   
      cout << "\nCheck if file exists, is in specified location, has been renamed, or deleted.";
   
      cout << "\nProgram will now terminate in preparation for file troubleshooting.";
   
      cout << "\nPress [Enter] to exit program... ";
   
      cin.get();
   
      exit(EXIT_FAILURE);
  
      }
  
       
  
      banking>>loginID;
  
      banking>>password;
  
       
  
      if(loginID == loginID2 && password == password2)
  
      {
  
      cout<<"ok"<<endl;
  
      }
  
       
  
      else
  
      {
  
      cout<<"\tUnregistered ID or you have enter a wrong password !"<<endl;
  
  
  
      }
  
       
  
      banking.close();
}

int main()
{
     
    bank b;
    b.registeration();
    
    cin.clear();
    cin.ignore();
    getchar();
    return 0;
    }

it run well but it seem like always loop to else cout there..

Wohoo..problem solved!!
Thanks Clinton~~ =) u help me a lots

then mark it solvd so others dont comment too

okay...thanks you too zobadof =)

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.