Thread: Need some help with a C++ program. Logging in and toupper.

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    4

    Need some help with a C++ program. Logging in and toupper.

    Hi all,

    I'm currently doing a project where I'm making a programme where you can create an account and login to the program. The user name and passwords will be saved on another file called "Personal.txt"

    This is how the "Personal.txt" will look like.
    insert
    Code:
    infile >> name >> user >> password;
    e.g. John Johny John123


    The thing is I cant seem to do the logging in part. If I have alot of users the program seems to only read the first line of user name and password. How do I make the program scan the entire "Personal.txt" to allow it to login?

    This is my current part of the login.


    insert
    Code:
    {
    
    
            cout << "Please login. Enter name<space>password: ";
            cin >> user1 >> password1;
    
    
    
    
    
    
    
    
            if (user1 == user && password1 == password)
            {
                cout << "WELCOME";
            }
            else
            {
                cout<<"Failed";
            }
    
    
        }
    Also, How do I use toupper? I want to Toupper a "Y n N" and the username section of the program but from what I've seen online I do not understand what they mean.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sipth
    If I have alot of users the program seems to only read the first line of user name and password. How do I make the program scan the entire "Personal.txt" to allow it to login?
    You have a few options. One option is to keep reading from the file until you find an entry that matches. If there is no such entry, then the credentials are incorrect. A possibly better option is to define a struct/class to model a user, then create a vector of objects of that struct/class. You read all the entries from the file into the vector of objects. Then, you search the vector for the entry with the given username/password.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2014
    Posts
    4
    Quote Originally Posted by laserlight View Post
    You have a few options. One option is to keep reading from the file until you find an entry that matches. If there is no such entry, then the credentials are incorrect. A possibly better option is to define a struct/class to model a user, then create a vector of objects of that struct/class. You read all the entries from the file into the vector of objects. Then, you search the vector for the entry with the given username/password.
    Can you give me of an example of the first method of reading the file until i find an entry that matches?

    I currety changed mine to this but I'm not sure how to continue from it.

    Code:
    {
    
    
    		cout << "Please login. Enter name<space>password: ";
    		cin >> user1 >> password1;
    
    
    		while (!inputfile.eof())
    		if (user1 == user && password1 == password)
    		{
    			cout << "WELCOME";
    		}
    		else
    		{
    			cout<<"Failed";
    		}
    
    
    	}
    Last edited by sipth; 08-04-2014 at 05:57 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A simple example:
    Code:
    bool authenticated = false
    while (infile >> name >> user >> password)
    {
        if (user == user1 && password == password1)
        {
            authenticated = true;
            break;
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2014
    Posts
    4
    Quote Originally Posted by laserlight View Post
    A simple example:
    Code:
    bool authenticated = false
    while (infile >> name >> user >> password)
    {
        if (user == user1 && password == password1)
        {
            authenticated = true;
            break;
        }
    }
    I'm not sure where I'm going wrong. Just now, it only reads the first line now it only reads the last line.
    Code:
    {
    
    
            cout << "Please login. Enter name<space>password: ";
            cin >> user1 >> password1;
    
    
    
    
    
    
            while (infile >> name >> user >> password)
            if (user1 == user && password1 == password)
            {
                cout << "WELCOME";
                break;
            }
            else
            {
                cout<<"Failed";
                break;
            }
    
    
    }
    Last edited by sipth; 08-04-2014 at 07:01 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think you mean to say that it only reads the first line.

    Look at your loop. Suppose (user1 == user && password1 == password) evaluates to true. You then print a message then break from the loop. Otherwise, you print a different message then break from the loop. Therefore, we can rewrite your loop to be:
    Code:
    if (infile >> name >> user >> password)
        if (user1 == user && password1 == password)
        {
            cout << "WELCOME";
        }
        else
        {
            cout<<"Failed";
        }
    That is, it isn't a loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Aug 2014
    Posts
    4
    Quote Originally Posted by laserlight View Post
    I think you mean to say that it only reads the first line.

    Look at your loop. Suppose (user1 == user && password1 == password) evaluates to true. You then print a message then break from the loop. Otherwise, you print a different message then break from the loop. Therefore, we can rewrite your loop to be:
    Code:
    if (infile >> name >> user >> password)
        if (user1 == user && password1 == password)
    
    Isit possible if I sent you a private message with the code? I cant seem to solve the logging in part and I'm really frustrated
        {
            cout << "WELCOME";
        }
        else
        {
            cout<<"Failed";
        }
    That is, it isn't a loop.


    Isit possible If I sent you a private message inclusive of my code? It would help greatly if you could help me see what the problem is. I cant seem to solve the logging in part.

    EDIT: Seems like I cant send a private message, it's okay i guess...
    Last edited by sipth; 08-04-2014 at 07:54 AM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's better if you post it publically so everyone can see. That way, anyone can help.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logging headaches
    By homer_3 in forum C++ Programming
    Replies: 3
    Last Post: 09-11-2013, 05:25 PM
  2. Logging out
    By arjunajay in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 06-29-2005, 05:36 AM
  3. IP logging
    By cruxis in forum Networking/Device Communication
    Replies: 5
    Last Post: 10-05-2004, 04:31 AM
  4. a dial up logging program
    By ukcpaul in forum Windows Programming
    Replies: 4
    Last Post: 02-05-2002, 03:37 PM
  5. Logging IP's
    By DanMan in forum C++ Programming
    Replies: 2
    Last Post: 10-16-2001, 03:32 PM

Tags for this Thread