i have problem with strings is that i read a string from file and save it into another string and then compare with the another string which user enter
....basically i am going to design a login system in which first administrator make your user name and u set a password that data is save in the form of file and (i even cheek that the data which is write in file and save as it in the string ) when i compare the strings that user enter and the string which i read from file then it is always unequal
i used following methods but ....
================================================
if (strcmp(password,save)==0) {
} else {
}
===================================
if (password==save) {
} else {
}
==================================
if(strcmp(password.c_str(),save.c_str())==0) {

} else {
// in this case it is alway there even i enter the same password
}
plz help me out thank u....

Recommended Answers

All 17 Replies

Member Avatar for iamthwee

I'm sorry but that doesn't tell us much.

Post all your code including your text file and the code you use to read in that password.

#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <conio.h>
#include <fstream>
using namespace std;
class login {
      

private:
        char op;
        string password;
        string save;
        char c;
        string out;
        string username1;
        string st;
public:
       login();
       ~login();
     void   startUp();
       void loading();
       void delay();
       void password1();
       //void compare();
       void username();
       void CreateUser();
       void MainLogin();


};
login::login() {
               
               }
login::~login() {
                }
void login::startUp() {
cout<<"\n\n\n\n\n\n";
cout<<"\t\t\t LODING"<<endl;
login::loading();
                 
                 }
void login::loading() {
cout<<endl<<endl<<endl;
                cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';
                cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';
                login::delay();
                cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';
                cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';
                login::delay();
                cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';
                cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';
                login::delay();
                cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';
                cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';
                login::delay();
                 cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';
                 cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';
                 system("cls");
                //system("color 9");
              //  login::username();
                
                
                }
void login::delay() {
               for (int c=0; c<500; c++ ) {
                   for (int d=0;d<100;d++ ) {
                       for (int e=0;e<5000;e++) {
                       }
                   }
               }
}
void login::password1() {
  do   
         {
         c = getch();
         switch(c)
            {
            case 0xE0:        
               {
               getch();
               break;
               }
            case 0:           
               {
               getch();
               break;
               }
            case '\b':
               {
               if(password.size() != 0)  
                  {
                  cout << "\b \b";
                  password.erase(password.size() - 1, 1);
                  }
               break;       
               }   
            default:
               {
               if(isalnum(c) || ispunct(c))
                  {
                  password += c;
                  save=password;
                  cout << "*";           
                  }
               break;     
               }      
            };
         }
      while(c != '\r');
//login::compare();

  }
void login::username() {
     
     system("cls");
     cout<<"\n\n\n\n\n";
     cout<<"                             LOGIN SYSTEM                       \n\n"<<endl;
     cout<<"====================================================================\n"<<endl;
     cout<<"====================================================================\n"<<endl;
     cout<<"\t\t";
     string fileName = "";
	 fstream user;
	 getchar();
     cout<<"USER NAME: ";
     getline(cin, fileName);
     user.open(fileName.c_str());
     if (!user) {
                cerr<<"sory the user name dos't exit ";
                } else {
     cout<<endl;
  
     cout<<"\t\t";
     cout<<"PASSWORD :";
     
     login::password1();
     // plz .............
/////////////////////////////////////////// ///////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////     
     
     user>>out; // takng the string from the file 
     st=out; // save the string into another string 
     cout<<endl;
     cout<<st; // cout that to conform that is it save the correct data or not it is not  a part of project but just cheak 
     
     cout<<endl;
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  //the mane problem i faced is the comparing of two string it always say that password in not mached
  if (strcmp(save.c_str(),st.c_str()) == 0) {
  //if (save==st) {
     cout<<endl;
     cout<<"===================================================================="<<endl;
     cout<<"===================================================================="<<endl;     
     // menu();    
     } else {
                cout<<"\n\nthe password is not mached "<<endl; 
        }
         }
         }
         ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
         ///////////////////////////////////////////////////////////////////////////////////////////////////////
         ////////////////////////////////////////////////////////////////////////////////////////////////////////////
void login::CreateUser() {
    string fileName = "";
	ofstream user;
	cout << "Please Enter the Name of the User   ";
	getline(cin, fileName);
   user.open(fileName.c_str());
   if (!user) {
              cerr<<"\n\nsorry the user can't be created because the secondry storage dont allow this ";
              cerr<<"\a\athe system 0x4747232 error "<<endl;
              } else {
           A:         
                      cout<<"\n\nthe new User is created please enter the password that u want to set"<<endl;
                      cout<<"\n\n\t PASSWORD :";
                    do   {
         c = getch();
         switch(c)
            {
            case 0xE0:        
               {
               getch();
               break;
               }
            case 0:           
               {
               getch();
               break;
               }
            case '\b':
               {
               if(password.size() != 0)  
                  {
                  cout << "\b \b";
                  password.erase(password.size() - 1, 1);
                  }
               break;       
               }   
            default:
               {
               if(isalnum(c) || ispunct(c))
                  {
                  password += c;
                  save=password;
                  cout << "*";           
                  }
               break;     
               }
               }
   } while(c != '\r');
   cout<<endl<<endl;
      cout<<"the password u enter Is this     "; 
      cout<<save;
      cout<<"\n\n Do u realy want to save this password press 'y' else 'n' with enter ";
      cin>>op;
      if (op=='y') {
                   user<<save;
                   cout<<"the new user is being create please wait  \n\n\n";
                   login();
                   system("cls");
                   cout<<"the new user is created ";
                   
                   }else{
                         cout<<"the blue boot is taking u to set password again"<<endl;
                         system("pause");
                         
                         goto A;
                         }
}
user.close();
}
//void login::mainlogin() {
     
     
     
  //   }
     

int main() {
    //system("color 9");
login L;
L.startUp();
L.CreateUser();
L.username();
cout<<endl;
system("pause");
return 0;
}

// the main problem is that it can't compare the data from the file with the password string
even i save the data from file into the another string it display it correct but there is problem in the comparing
plz try to solve it plz............

#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <conio.h>
#include <fstream>
using namespace std;
class login {
      

private:
        char op;
        string password;
        string save;
        char c;
        string out;
        string username1;
        string st;
public:
       login();
       ~login();
     void   startUp();
       void loading();
       void delay();
       void password1();
       //void compare();
       void username();
       void CreateUser();
       void MainLogin();


};
login::login() {
               
               }
login::~login() {
                }
void login::startUp() {
cout<<"\n\n\n\n\n\n";
cout<<"\t\t\t LODING"<<endl;
login::loading();
                 
                 }
void login::loading() {
cout<<endl<<endl<<endl;
                cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';
                cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';
                login::delay();
                cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';
                cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';
                login::delay();
                cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';
                cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';
                login::delay();
                cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';
                cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';
                login::delay();
                 cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';
                 cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';cout<<'\xdb';
                 system("cls");
                //system("color 9");
              //  login::username();
                
                
                }
void login::delay() {
               for (int c=0; c<500; c++ ) {
                   for (int d=0;d<100;d++ ) {
                       for (int e=0;e<5000;e++) {
                       }
                   }
               }
}
void login::password1() {
  do   
         {
         c = getch();
         switch(c)
            {
            case 0xE0:        
               {
               getch();
               break;
               }
            case 0:           
               {
               getch();
               break;
               }
            case '\b':
               {
               if(password.size() != 0)  
                  {
                  cout << "\b \b";
                  password.erase(password.size() - 1, 1);
                  }
               break;       
               }   
            default:
               {
               if(isalnum(c) || ispunct(c))
                  {
                  password += c;
                  save=password;
                  cout << "*";           
                  }
               break;     
               }      
            };
         }
      while(c != '\r');
//login::compare();

  }
void login::username() {
     
     system("cls");
     cout<<"\n\n\n\n\n";
     cout<<"                             LOGIN SYSTEM                       \n\n"<<endl;
     cout<<"====================================================================\n"<<endl;
     cout<<"====================================================================\n"<<endl;
     cout<<"\t\t";
     string fileName = "";
	 fstream user;
	 getchar();
     cout<<"USER NAME: ";
     getline(cin, fileName);
     user.open(fileName.c_str());
     if (!user) {
                cerr<<"sory the user name dos't exit ";
                } else {
     cout<<endl;
  
     cout<<"\t\t";
     cout<<"PASSWORD :";
     
     login::password1();
     // plz .............
/////////////////////////////////////////// ///////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////     
     
     user>>out; // takng the string from the file 
     st=out; // save the string into another string 
     cout<<endl;
     cout<<st; // cout that to conform that is it save the correct data or not it is not  a part of project but just cheak 
     
     cout<<endl;
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  //the mane problem i faced is the comparing of two string it always say that password in not mached
  if (strcmp(save.c_str(),st.c_str()) == 0) {
  //if (save==st) {
     cout<<endl;
     cout<<"===================================================================="<<endl;
     cout<<"===================================================================="<<endl;     
     // menu();    
     } else {
                cout<<"\n\nthe password is not mached "<<endl; 
        }
         }
         }
         ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
         ///////////////////////////////////////////////////////////////////////////////////////////////////////
         ////////////////////////////////////////////////////////////////////////////////////////////////////////////
void login::CreateUser() {
    string fileName = "";
	ofstream user;
	cout << "Please Enter the Name of the User   ";
	getline(cin, fileName);
   user.open(fileName.c_str());
   if (!user) {
              cerr<<"\n\nsorry the user can't be created because the secondry storage dont allow this ";
              cerr<<"\a\athe system 0x4747232 error "<<endl;
              } else {
           A:         
                      cout<<"\n\nthe new User is created please enter the password that u want to set"<<endl;
                      cout<<"\n\n\t PASSWORD :";
                    do   {
         c = getch();
         switch(c)
            {
            case 0xE0:        
               {
               getch();
               break;
               }
            case 0:           
               {
               getch();
               break;
               }
            case '\b':
               {
               if(password.size() != 0)  
                  {
                  cout << "\b \b";
                  password.erase(password.size() - 1, 1);
                  }
               break;       
               }   
            default:
               {
               if(isalnum(c) || ispunct(c))
                  {
                  password += c;
                  save=password;
                  cout << "*";           
                  }
               break;     
               }
               }
   } while(c != '\r');
   cout<<endl<<endl;
      cout<<"the password u enter Is this     "; 
      cout<<save;
      cout<<"\n\n Do u realy want to save this password press 'y' else 'n' with enter ";
      cin>>op;
      if (op=='y') {
                   user<<save;
                   cout<<"the new user is being create please wait  \n\n\n";
                   login();
                   system("cls");
                   cout<<"the new user is created ";
                   
                   }else{
                         cout<<"the blue boot is taking u to set password again"<<endl;
                         system("pause");
                         
                         goto A;
                         }
}
user.close();
}
//void login::mainlogin() {
     
     
     
  //   }
     

int main() {
    //system("color 9");
login L;
L.startUp();
L.CreateUser();
L.username();
cout<<endl;
system("pause");
return 0;
}

plz slove the problem of the comparing of two strings

commented: jeez, man, dont repost your entire code. once is enough -2

Could you please post using code tags?

Information about code-tags is available all over Daniweb :
1) in the Rules you were asked to read when you registered
2) in the text at the top of this forum
3) in the announcement at the top of this forum titled Please use BB Code and Inlinecode tags
4) in the sticky post above titled: "Read Me: Read This Before Posting"
5) any place CODE tags were used
6) Even on the background of the box where you actually typed your message in

the main problem in this code is that it can't compare two strings

the main problem in this code is that it can't compare two strings

Well, shall I give you some small examples on how to compare C/C++ strings?
Here you are:

  • Comparing two C++ strings:
    string a = "hello";
    string b = "world";
    
    if( a != b )
    {
      cout << "\'" << a << "\' is not equal to \'" << b << "\'" << endl;
    }
  • Comparing two C strings:
    char s1[] = "hello";
    char s2[] = "hello";
    
    if( !strcmp(s1, s2) )
    {
      cout << "Both strings are equal." << endl;
    }
  • Comparing a C string to a C++ string
    char a[] = "hello";
    string b = "hello";
    
    if( !strcmp( a, b.c_str() ) )
    {
      cout << "We are both equal." << endl;
    }
    
    // or
    
    if( string(a) == b )
    {
      cout << "We are both equal." << endl;
    }

Hope this helps!

dear lux4life
i know that but the problem is that when i used

if ((str.c_str(),save.c_str()==0)) {
cout<<"password is correct" ;
} else {
cout<<"password is wrong";
}

but my ans is always "password is wrong"
even i cout<<the password from file that is same that i enter but same result
""can u copy the code and run in dev c++ then u understand what i mean""

is there is any problem with the (end of line char) or (terminating char)
during the comparing of two string
one string i read from file and one which user enter inoder to conform the password

but my ans is always "password is wrong"

Print both strings and eyeball them:

cout << ">" << passFromFile << "<\n";
cout << ">" << passFromUser << "<\n";

There's probably whitespace or something that makes the difference.

but i cant understand how it work and what it do?

but i cant understand how it work and what it do?

What don't you understand?
Tom Gunn's code? It's just a check which prints the password read from the file and the password read from the user, so we can see what might be going wrong.

If you don't understand that, how did you come up then with the code you posted in this thread?

oooh thanks tom Guu "it realy wrok thank u very much"
thanks again:)

oooh thanks tom Guu "it realy wrok thank u very much"
thanks again:)

If it works now, what is stopping you then from marking this thread as solved?

dear lux4life:
the main problem is that i use password function two times and the data is save in the string two time like
i save password lux4life
it save twice like lux4lifelux4life
there4 my result is always unequal when i used guun method to analysis the string then i come to know that.
and thank u again 2 all of u who serifed there important time 4 me .....
thanks again to all of u...

i just make another string two save password that really work....

Actually it is: Tux4life :P (not with an 'l' or 'L')

sorry dear ,

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.