Login Function??

This is a discussion on Login Function?? within the C++ Programming forums, part of the General Programming Boards category; im getting an error with this piece of code, im sure theres a better way to do it but hey ...

  1. #1
    dac
    dac is offline
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147

    Login Function??

    im getting an error with this piece of code, im sure theres a better way to do it but hey its my first shot at it;

    Code:
    string Login()
    {//function
         
         int x,i,y;
         char Buffer[50], Buffer2[50], Username[50], Password[50];          
         char ch = getch();
         
        do
        {
            system("cls");
                          
            cout << "Login" << endl
                 << endl;                                   
                 
            cout << "Username       : ";
            cin.getline(Username, 50, '\n');    
            
            cout << endl;           
            cout << "Password       : ";
                
                while (ch != '\r')
                {//while                                                                      
                      i=0;                 
                      ch = getch();
                      Password[i] = ch;   //small loop so only * are displayed instead of actual password
                      cout << '*';
                      i++;
                }//while
                
         ifstream SAM_File("C:\\Documents and Settings\\Dean.Downstairs\\My Documents\\OperationalFiles\\SAMFile.dat");     
         SAM_File.getline(Buffer, 50);
         
         for(x=0; x<50; x++)
         {             
                 SAM_File.getline(Buffer[x],x);  //gets set username from .dat file
         }
         
         
         for(y=0; y<50; y++)
         {
                  SAM_File.getline(Buffer[y],y);  //gets set password from .dat file
         }
                           
         SAM_File.close();
             
         ch = ' ';         //sets to 0 so its not full
         
         } while(Buffer != Username || Buffer2 != Password);
                     
    };//function
    thanks for help in advance,
    Last edited by dac; 12-31-2006 at 02:06 PM.

  2. #2
    dac
    dac is offline
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    o ye, heres the errors;

    F:\Computing Coursework\Computing Coursework.cpp In function `std::string Login()':

    893 F:\Computing Coursework\Computing Coursework.cpp invalid conversion from `char' to `char*'

    893 F:\Computing Coursework\Computing Coursework.cpp initializing argument 1 of `std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]'

    899 F:\Computing Coursework\Computing Coursework.cpp invalid conversion from `char' to `char*'

    899 F:\Computing Coursework\Computing Coursework.cpp initializing argument 1 of `std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]'

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    getline() needs a char* and not a char.

    This is what you may want to do:

    SAM_File.getline(Buffer, 50);

    Besides, remember that you cannot compare C-style strings with the != operator. Either use std::string or use the strcmp() function.

    Edit: You also declare the function as returning a string, yet you never use strings anywhere in your code nor do you return anything at all. You also end your function with a semi-colon which is not necessary. It is only necessary to put a semi-colon after a closing brace when declaring a struct or a class and maybe an enum too (not sure about this one).
    Last edited by Desolation; 12-31-2006 at 02:07 PM.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    and password should be read into Buffer2 not Buffer
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    dac
    dac is offline
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    Quote Originally Posted by Desolation
    getline() needs a char* and not a char.

    This is what you may want to do:

    SAM_File.getline(Buffer, 50);

    Besides, remember that you cannot compare C-style strings with the != operator. Either use std::string or use the strcmp() function.

    Edit: You also declare the function as returning a string, yet you never use strings anywhere in your code nor do you return anything at all. You also end your function with a semi-colon which is not necessary. It is only necessary to put a semi-colon after a closing brace when declaring a struct or a class and maybe an enum too (not sure about this one).
    thanks, lol, ive been trying it every which way but loose, and i was editing it all the time. it was using strings before,

  6. #6
    dac
    dac is offline
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    ok its still not working, a little fine tuning needed i think;

    edit: it just breaks from the loop every time, even when the password or username is wrong.

    Code:
    void Login()
    {//function     
         int i;
         char Buffer[50], Buffer2[50], Username[50], Password[50];          
         char ch = getch();
         
        do
        {
            system("cls");
                          
            cout << "Login" << endl
                 << endl;                                   
                 
            cout << "Username       : ";
            cin.getline(Username, 50, '\n');    
            
            cout << endl;           
            cout << "Password       : ";
                
                while (ch != '\r')
                {//while                                                                      
                      i=0;                 
                      ch = getch();
                      Password[i] = ch;
                      cout << '*';
                      i++;
                }//while
                
         ifstream SAM_File("C:\\Documents and Settings\\Dean.Downstairs\\My Documents\\OperationalFiles\\SAMFile.dat");     
         SAM_File.getline(Buffer, 50);    
         
         SAM_File.getline(Buffer, 50);     
         SAM_File.getline(Buffer2, 50);  
          
         SAM_File.close();
         
         ch = ' ';
         
         } while(strcmp(Username, Buffer) == 0 || strcmp(Password, Buffer2) == 0);                 
    }//function
    Last edited by dac; 12-31-2006 at 02:40 PM.

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Use C++ (or std: strings. It makes comparison much easier! You can use the == operator. #include <string>.

  8. #8
    dac
    dac is offline
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    Quote Originally Posted by twomers
    Use C++ (or std: strings. It makes comparison much easier! You can use the == operator. #include <string>.
    im not familiar with std::, could you give me an example please.

  9. #9
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Sure.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    #include <windows.h>
    #include <conio.h>
    
    using namespace std;
    
    int main( void )
    {
        string Buffer   ="USERNAME", 
                Buffer2  ="PASSWORD", 
                Username ="", 
                Password =""; // No harm in initialising things!
    
         
        do
        {
    		cout<< "Enter your username: ";
    		getline( cin, Username ); // Defaults to termination at '\n'
    
    		cout<< "Enter your password: ";
    		getline( cin, Password );
         
         } while(Username!=Buffer && Password != Buffer2 );    
    
    	return 0;
    }
    They're just C++ style strings as opposed to character arrays. Lots of functionality and it's no harm in getting to know them!
    Last edited by twomers; 12-31-2006 at 03:08 PM.

  10. #10
    dac
    dac is offline
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    Quote Originally Posted by twomers
    Sure.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    #include <windows.h>
    #include <conio.h>
    
    using namespace std;
    
    int main( void )
    {
        string Buffer   ="USERNAME", 
                Buffer2  ="PASSWORD", 
                Username ="", 
                Password =""; // No harm in initialising things!
    
         
        do
        {
    		cout<< "Enter your username: ";
    		getline( cin, Username ); // Defaults to termination at '\n'
    
    		cout<< "Enter your password: ";
    		getline( cin, Password );
         
         } while(Username!=Buffer && Password != Buffer2 );    
    
    	return 0;
    }
    your missing the entire idea behind the function lol, its not supposed to display the password on entry, just *'s.

  11. #11
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    I was leaving that to you

  12. #12
    dac
    dac is offline
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    ok, this function is giving me great grief, its just not playing ball in any way. it just continues to loop even when the condition is 0.

    here it is;

    Code:
    void Login()
    {//function     
                 
         char ch = getch();
         string Buffer, Buffer2, Username, Password;
         
        do
        {
            system("cls");
                          
            Username = "";
            Password = "";
            
            cout << "Login" << endl
                 << endl;                                   
                 
            cout << "Username         : ";
            getline(cin, Username);     
            
            cout << endl;           
            cout << "Password         : ";
                
                do
                {//while                                                                      
                                      
                      ch = getch();
                      
                      Password += ch;
                     
                     if(ch == '\r')
                     {
                           break;
                     }
                     else if(ch != '\r')
                     {
                      cout << "*";
                     }
                      
                }while (ch != '\r');
                           
          Username = "Dcarr";
          Password = "shadow";
       
         } while(Username != Buffer || Password != Buffer2); 
                        
    }//function
    Last edited by dac; 01-01-2007 at 05:18 AM.

  13. #13
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Here's some code for ya:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    #include <windows.h>
    #include <conio.h>
    
    using namespace std;
    
    bool Login()
    {//function     
                 
    	char ch;
    	string u_buf, p_buf, Username, Password;
    //	Set the values of u_buf and p_buf here. Shouldn't really hard code them in
    
    	system("cls");
    //	there's a better alternative to this in the FAQ
    	   
    
    	cout	<< "Login" << endl
    		<< endl;                                   
    
    	cout << "Username         : ";
    	getline(cin, Username);     
    
    	cout << endl;           
    	cout << "Password         : ";
    
    	while ( ch != '\r' )
    	{ 
    		ch = getch();
    
    		if ( ch!='\r' )
    		{
    			Password += ch;
    			cout << "*";
    		}
    	}
    
    
    	return Username == u_buf && Password == p_buf;
                        
    }//function
    
    int main( void )
    {
    	if ( Login() == true ) // could change to: if ( Login ){...} as the function will return true or false
    		cout<< "Welcome!";
    	else 
    		cout<< "Try again";
    
    	return 0;
    }
    I changed a couple of things around. I made the function a bool so that you could test for equality in main(). If you want to loop it, just do a while(Login()==false); (or while( !Login() ); and it should do the same as your original code.
    Last edited by twomers; 01-01-2007 at 07:03 AM.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    Here is a variant of twomers' code example that incorporates some support for backspaces. Not guaranteed to work, I just happened to be intrigued by the idea as I rarely work with getch().
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <string>
    
    #include <conio.h>
    
    using namespace std;
    
    // Returns true if login was successful.
    bool Login()
    {
        // Set the values of u_buf and p_buf here.
        // There may be alternatives to hardcoding them in.
        string u_buf("user"), p_buf("pass");
    
        system("cls"); // There is a better alternative to this in the FAQ.
    
        cout << "Login\n" << endl;
    
        string Username;
        cout << "Username         : ";
        getline(cin, Username);
    
        string Password;
        cout << "Password         : ";
        // read input until a newline sequence is encountered
        for (;;)
        {
            char ch = getch();
    
            if (ch == '\r' || ch == '\n')
            {
                break;
            }
            else if (ch == 8)
            {
                // handle backspace by erasing what is printed
                if (!Password.empty())
                {
                    // we have to erase from the string too
                    Password.erase(Password.end() - 1);
                    cout << char(8) << " " << char(8);
                }
            }
            else
            {
                // not a newline sequence or backspace, so append it
                Password.push_back(ch);
                cout << "*";
            }
        }
    
        cout << endl;
    
        return Username == u_buf && Password == p_buf;
    }
    
    int main()
    {
        if (Login())
        {
            cout << "Welcome!";
        }
        else
        {
            cout << "Try again";
        }
        cout << endl;
        return 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    dac
    dac is offline
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    thanks twormers, :-) :-)

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21