Thread: trying to write a login prompt that takes user input and verifies it vs. pre-existing

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    2

    trying to write a login prompt that takes user input and verifies it vs. pre-existing

    ok, below is the code.... basically, I'm trying to make a login promt (like what you see at the beginnign of linux or windows or any OS), I'm trying to read in the user name then compare it to one that would already exist, either that ive defined in the program, or that resides in a text file... when i try to compile this, i get error: line 9: assignment to 'char' from 'const char *' lacks a cast... anyone have any idea what I'm doing wrong/


    #include <iostream> //Basic Input/Output
    #include <string.h> //Manipulate strings

    int main()
    {
    char name[20], name1[20], pass[20], pass1[20];
    cout << "Enter username: ";
    cin.getline(name, 20, '\n');
    *name1="mike";
    if(!strcmpi(name1, name))
    {
    cout << "correct... logging in.... ";
    }

    else
    {
    cout << "Incorrect user name, please re-enter user name";
    }

    return 0;
    }

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Code:
    *name1="mike";
    You just can't do that using char pointers. I suggest using strings instead.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Easier Way To Do It Is Just Do This
    Code:
    if(!strcmpi("mike", name))
    {
    cout << "correct... logging in.... ";
    }
    Its not case sensitive either i think

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    2
    oh hey, i guess that does kinda make sense... as for the pointers thing.. who knows what I was thinkin' there... I don't even know... now i have another question... say i wanted to have multiple user names that could be used is there a way i can do this without making seperate if statements or do I have to make a seperate if statement for each user name?

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Nope Just Like A Standard if statement just do this
    Code:
    if(!strcmpi("mike", name)||!strcmpi("brian",name))
    {
    cout << "correct... logging in.... ";
    
    }
    Woop?

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    161
    You could also do something like this (makes the user list easier to update):

    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    int main()
    {
      const char* users[] = { "mike", "brian", "jim", "bob", "joe", 0 };
      bool found = false;
    
      while(!found)
      {
        char name[20];
        cout << "Enter username: ";
        cin.getline(name, 20, '\n');
    
        unsigned i = 0;
    
        while(users[i] && !found)
        {
          if(strcmp(users[i], name) == 0)
            found = true;
          else ++i;
        }
    
        if(!found)
          cout << "Incorrect user name, please re-enter user name" << endl;
      }
    
      cout << "correct... logging in..." << endl;
    
      return 0;
    }
    -tf

Popular pages Recent additions subscribe to a feed