username collection

This is a discussion on username collection within the C++ Programming forums, part of the General Programming Boards category; I'm going to use what I've learned in File i/o to make a username collection where people can create users ...

  1. #1
    yes, I'm registered!!! algi's Avatar
    Join Date
    Nov 2004
    Location
    Ipswich
    Posts
    161

    Post username collection

    I'm going to use what I've learned in File i/o to make a username collection where people can create users and log in. But I'm stuck on something. The way I did it before allowed people to override other users, so I need some code to detect wheter that username is detected.

    A persons username is stored as username.txt and the password is inside the txt file.

    This is what I've been trying

    Code:
    #include <iostream>
    #include <windows.h>
    #include <stdlib.h>
    #include <fstream>
    using namespace std;
    
    void mainmenu();
    
    void mainmenu()
    {
         string username;
         string option;
         cout<<"1)Create new Account\n";
         cout<<"Choice: ";
         cin>>option;
         cin.ignore();
         if ( option == "1" )
         {
              cout<<"Enter your desired username: ";
              cin>>username;
              if ( !ofstream a_file () == username )
              {
                   cout<<"Enter Password";
                   Sleep(3000);
                   }
              else
              {
                  cout<<"Sorry that username is already taken";
                  Sleep(3000);
                  }
    }
    
    int main()
    {
        mainmenu();
    }
    I need something to detect whether the text file already exists

    Thanks in Advance
    I started out with nothing and I still have most of it left.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,197
    Open the txt-file for reading. If it fails to open you can almost always assume there is no file with that name. Then just close the file and open it for writing and start writing.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    just a few suggestions based on your code:

    1) why not use an integer or character for choice?
    2) why not just have mainmenu() print the menu and then return control to main?
    3) why sleep after prompting for a password?
    *I noticed you don't actually take in any password - I'm guessing this is still debug-level code
    4) why sleep after printing an error message?
    *I suggest prompting them to press enter and continuing when they do

    also, how did you have your text files set up? do you mean using filenames as the database? like this:

    Code:
    username1.txt
    password1.txt
    username3.txt
    password3.txt
    or do you have it a more accepted way like one file for usernames and another for passwords?

    if so, what were you trying to do with this line:

    Code:
    if ( !ofstream a_file () == username )
    Last edited by major_small; 01-22-2005 at 12:56 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    yes, I'm registered!!! algi's Avatar
    Join Date
    Nov 2004
    Location
    Ipswich
    Posts
    161
    Major i don't know why I used sleep, but it wouldn't be there on the real thing would
    I started out with nothing and I still have most of it left.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get current username
    By ssharish2005 in forum C Programming
    Replies: 3
    Last Post: 09-25-2007, 02:44 PM
  2. username to uid
    By chrismiceli in forum Linux Programming
    Replies: 1
    Last Post: 07-30-2007, 01:23 PM
  3. Getting the user's local username
    By Datamike in forum C Programming
    Replies: 8
    Last Post: 11-26-2005, 05:00 PM
  4. username to uid mapping
    By rotis23 in forum C Programming
    Replies: 1
    Last Post: 10-30-2002, 03:17 AM
  5. getting username from Windows NT
    By Flucas in forum Windows Programming
    Replies: 1
    Last Post: 10-29-2001, 06:39 PM

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