7 Replies - 730 Views - Last Post: 08 February 2010 - 08:38 AM Rate Topic: -----

#1 Guest_newbiz*


Reputation:

Help With String Compare, I cant login with the stored username and pa

Posted 07 February 2010 - 09:06 PM

#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
void login()
{
     string nuser, npass,name,pass;
     cout << "Enter user name: ";
     cin >> nuser;
     cout << "Enter password: ";
	 cin >> npass;
     ifstream file ("password.txt");
     if (file.is_open())
     {
		 std::getline(file, name);
		 std::getline(file, pass);

        if ((strcmp(nuser, name)==0) && (strcmp(npass,pass)==0))
           cout << "Login successful!!!" << endl;
        else
            cout << "Wrong user name or password!!!" << endl;
     }
     cin.ignore();
}
int menu()
{
     int opt;
     do{
     cout << "1: Login\n2: Exit\nEnter an option: ";
     cin >> opt;
         switch(opt)
         {
                    case 1: login(); break;
                    case 2: break;
         }
         }while(opt != 2);
     return 0;
}
void newuser()
{
     string pass;
     string name;
     cout << "---Welcome new user---" << endl
          << "Enter user name: ";
     cin >> name;
     cout << "Enter new password: ";
     cin >> pass;
     ofstream file ("password.txt");
     file <<name<<":"<<pass<<":"<<setw(4) << setfill('0') << (rand() % 10000);
     file.close();
	 getchar();
     menu(); 
}
int main()
{
    ifstream file ("password.txt");
    file.open ("password.txt");
    if (file.is_open())
       menu();
    else
        newuser();
	return 0;
}

This post has been edited by NickDMax: 08 February 2010 - 01:11 AM
Reason for edit:: Added/Fixed code tags [code]...your program here...[/code]


Is This A Good Question/Topic? 0

Replies To: Help With String Compare, I cant login with the stored username and pa

#2 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,778
  • Joined: 20-September 08

Re: Help With String Compare, I cant login with the stored username and pa

Posted 07 February 2010 - 10:45 PM

If you use these start and end [ code ] 'code tags' [ / code ] , with the extra spaces inside each [ ... ] removed ... your code will retain it's indented formatting ... and will then be MUCH easier to read.

[ code ] .... your code ... [ / code ]

It is most always a good idea to start with a small shell program that compiles and runs ... giving the expected output. This is especially needful for beginners ... or those prone to making typos ...

Then, add one block of code, recompile, run ... and test the output.

That way, you limit the places you may need to look to fix your code, so that it compiles and gives the expected output.

I made several changes ... This code now compiles and seems to give reasonable output.

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

using namespace std;

int login()
{
    string nuser, npass, name, pass;
    cout << "Enter user name: ";
    getline( cin, nuser );
    cout << "Enter password: ";
    getline( cin, npass );
    ifstream fin( "password.txt" );
    if ( fin.is_open() )
    {
        getline(fin, name);
        getline(fin, pass);
        fin.close();
    }
    else
    {
        cout << "Error accessing password file ...\n" << endl;
        return 0;
    }
    
    // can simply compare C++ strings ...
    if( nuser == name && npass == pass )
    {
        cout << "Login successful!!!" << endl;
        return 1;
    }
    // else ...
    cout << "Wrong user name or password!!!" << endl;
    return 0;
}


int menu()
{
    int opt, logOk; // may? need to use logOk value
    for( ; ; )
    {
        cout << "1: Login\n2: Exit\nEnter an option: ";
        opt = cin.get();
        cin.sync(); // 'flush' cin stream ...
        switch(opt)
        {
            case '1' : if( !(logOk = login()) ) return 0; break;
            case '2' : return 0;
            default  : cout << "Not implemented yet ...\n";
        }
    }
}

void newuser()
{
    string pass, name;
    cout << "---Welcome new user---" << endl
         << "Enter user name: ";
    getline( cin, name );
    cout << "Enter new password: ";
    getline( cin, pass );
    ofstream file ("password.txt");
    file << name << endl << pass << endl;
    file.close();
}



int main()
{
    ifstream file( "password.txt" );
    if( file.is_open() ) file.close();
    else newuser();
    do
    {
    }while( menu() );
    
    cout << "\nPress 'Enter' to continue ... " << flush;
    cin.get();
}


This post has been edited by David W: 07 February 2010 - 10:53 PM

Was This Post Helpful? 0
  • +
  • -

#3 Guest_freakx*


Reputation:

Re: Help With String Compare, I cant login with the stored username and pa

Posted 08 February 2010 - 03:55 AM

hello there, thanks for replying on my post... but when it comes to i add another user it gives me problem.. how can i solve it? so multiple user can login into it.
Was This Post Helpful? 0

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Help With String Compare, I cant login with the stored username and pa

Posted 08 February 2010 - 04:25 AM

View Postfreakx, on 08 February 2010 - 02:55 AM, said:

when it comes to i add another user it gives me problem.


What problem do you have?

Put a bit of effort into describing your problem and working on your code and it is possible that someone will put some effort into helping you.

If you can't be bothered to try then don't expect others to bother to try either.
Was This Post Helpful? 0
  • +
  • -

#5 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: Help With String Compare, I cant login with the stored username and pa

Posted 08 February 2010 - 05:52 AM

The code provided by David W (a corrected version of your code) reads only the first username/password from the file.
So you can start by changing that code to read usernames/passwords from the file until it runs out of users/passwords (in that case add a user) or it finds a match (login successful).
In the newuser() code, make sure that the existing usernames/passwords don't get overwritten, and that the new username and password that were entered get appended to the existing ones.

Good luck.
Was This Post Helpful? 0
  • +
  • -

#6 Guest_newbiz*


Reputation:

Re: Help With String Compare, I cant login with the stored username and pa

Posted 08 February 2010 - 06:40 AM

for example, now the text file is stored like this.
admin
admin
but i want it to be stored in one line, like admin:admin, but the problem is i dont know how to compare the string in one line. i have tried but it wont work for me..

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include "md5.h"

using namespace std;

int login()
{
    string nuser, npass, name, pass;
    cout << "Enter user name: ";
    getline( cin, nuser );
    cout << "Enter password: ";
    getline( cin, npass );
    ifstream fin( "password.txt" );
    if ( fin.is_open() )
    {
        getline(fin, name);
        getline(fin, pass);
        fin.close();
    }
    else
    {
        cout << "Error accessing password file ...\n" << endl;
        return 0;
    }
    
    // can simply compare C++ strings ...
    if( nuser == name && npass == pass )
    {
        cout << "Login successful!!!" << endl;
        return 1;
    }
    // else ...
    cout << "Wrong user name or password!!!" << endl;
    return 0;
}


int menu()
{
    int opt, logOk; // may? need to use logOk value
    for( ; ; )
    {
        cout << "1: Login\n2: Exit\nEnter an option: ";
        opt = cin.get();
        cin.sync(); // 'flush' cin stream ...
        switch(opt)
        {
            case '1' : if( !(logOk = login()) ) return 0; break;
            case '2' : return 0;
            default  : cout << "Not implemented yet ...\n";
        }
    }
}

void newuser()
{
    string pass, name;
    cout << "---Welcome new user---" << endl
         << "Enter user name: ";
    getline( cin, name );
    cout << "Enter new password: ";
    getline( cin, pass );
    ofstream file ("password.txt");
    file << name <<":"<< pass << endl; //i change this so it can stored in one line. but i cant compare the username and pass, the question here is, how can i break it into 2 part where username and pass//
    file.close();

}



int main()
{
    ifstream file( "password.txt" );
    if( file.is_open() ) file.close();
    else newuser();
    do
    {
    }while( menu() );
    
    cout << "\nPress 'Enter' to continue ... " << flush;
    cin.get();
}


//Sorry for my bad english.

This post has been edited by JackOfAllTrades: 08 February 2010 - 08:40 AM
Reason for edit:: Added code tags.

Was This Post Helpful? 0

#7 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: Help With String Compare, I cant login with the stored username and pa

Posted 08 February 2010 - 08:07 AM

I removed the line #include "md5.h" and executed your code.

Got the following output:

Quote

1: Login
2: Exit
Enter an option: 1
Enter user name: Enter password:


So I suspected this is a cin buffer issue. So I did
int login()
{
string nuser, npass, name, pass;
cout << "Enter user name: ";
cin.ignore(1); // <--- Added this to test what I suspected
getline( cin, nuser );


And the output was:

Quote

1: Login
2: Exit
Enter an option: 1
Enter user name: John Smith
Enter password: swordfish
Wrong user name or password!!!

Press 'Enter' to continue ...

Was This Post Helpful? 0
  • +
  • -

#8 ub3rgeek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-February 10

Re: Help With String Compare, I cant login with the stored username and pa

Posted 08 February 2010 - 08:38 AM

hi.. its me newbiz, i just registered in here.. btw i still dont know how to fix it so that the program can allow multiple user to login with diffrent username and password.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1