Why is my output not being stored in the file C++

56 Views Asked by At

I am writing a login system, I made a registration function that takes a username and password and checks both for exceptions, and then if they are correct it stores it into a file, but it doesn't store it into the file.

void reg(){
    fin.open("records.txt",ios::in);
    fout.open("records.txt", ios::app | ios::out);
    if(fout.is_open()){
        string temp_u;
        string temp_p;
        bool loop=true;
        do{
            bool invalid=false;
            bool same_id=false;
            cout<<"Enter Username:"<<endl;
            getline(cin>>ws,temp_u);
            cout<<"Enter Password:"<<endl;
            cin>>temp_p;
                
            try{
                int i=0,number=0;
                while(temp_p[i]!='\0'){
                if(temp_p[i]>='0'&&temp_p[i]<='9'){
                    number++;
                }
                i++;
                } 
                    
                if(number<=3){
                    string mess="Password must have atleast 3 digits!:\n";
                    throw mess;
                }
            }
            catch(string mess){
                system("cls");
                cout<<mess<<endl;
                invalid=true;
            }
            try{
                string mess;
                while(fin>>user>>pass){
                    if(temp_u==user){
                        cout<<"checking"<<endl;
                        mess="ID already exists choose another one\n";
                        throw mess; 
                    }
                }
            }
            catch(string mess){
                system("cls");
                cout<<mess<<endl;
                same_id=true;
            }
            if(invalid==false && same_id==false){
                loop=false;
                fout<<temp_u<<"\t\t"<<temp_p<<"\n";
            }
                
            }while(loop==true);
        }
        
        fout.close();
        fin.close();}

So I have declared an if statement that, when the password will be acceptable and when the user id will not be similar to the old ones, the bool variable which runs the loop will be false and the user id and password will be stored in the file.

int main(){ 
    login l;
     l.reg();
    return 0;}
0

There are 0 best solutions below