Little Help

This is a discussion on Little Help within the C++ Programming forums, part of the General Programming Boards category; I am trying to create a login program that is supposed to create a different txt file for every sign ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    208

    Little Help

    I am trying to create a login program that is supposed to create a different txt file for every sign up. but for sum reason when i sign up it doesn't create any txt file at all. befroe I added it in that it creates a different txt file for each different signup it worked perfectly. Can someone help me??

    Code:
    #include <fstream.h> //include everything that is nessacary
    #include <iostream.h>
    #include <string.h> 
    #include <stdlib.h>
    
    
    int login()
    { char nuttin[2];//initialize my variables
      char read[10];
      char siname[10];
      char sipass[10];
      char txt[10];
      cout<<"Please Enter Your User Name:\t";//ask for a user name
      cin>>siname;//write the name to siname
      siname=txt;
      strcat(txt,".txt");
      ifstream d_file(txt);//open username.txt
      cout<<"Please Enter Your Password:\t";//ask for a password
      cin>>sipass;//write the password to sipass
      strcat(siname, ".");//add a period at the end of siname
      strcat(siname, sipass);// combine the two to create a variable contiaining "username.password"
      d_file>>read;//write the contents of username.txt to a variable
      if (strcmp(read,siname) == 0) //compare the variables
    
    
      { cout<<"You Have Succesfully Logged In:\n";//explain the login  was successful
        system("PAUSE");//end the program
        return 0;
      }
      else 
      { cout<<"\nYour Log In Was Unsuccessful.Quit. Then Restart The Program To Try Again Or Sign Up\n";
        //explain something went wrong and give directions on what to do
        system("PAUSE");//end the program 
        return 0;
      }
          
    }  
      
    
    
    int signup()
    { 
      char name[10];      //Create My Variables 
      char password[10]; 
      char str[22];   
      char jeff[10];
    cout<<"Please Create A User Name Less Then 10 Letters:\t";//ask for a user name 
    cin>>name;//write to user name 
    cout<<"Please Create A Password Less Then 10 Letters:\t";//ask for a password 
    cin>>password;//write to password 
    name=jeff;//made it so that we have a variable that is their username that we can add on to but not destroy the username
    strcat(jeff,".txt");//adds .txt to the end of username
    ofstream a_file(jeff);//creates file with the name of username
    a_file << name << "." << password;//write the username "." Password to the file 
    a_file.close(); //close the file
    cout<<"\n Your Sign Up Was Successful. Thank You.\n"; //explain they are now signed up
    system("PAUSE");//end the program
        return 0; //return a integer
    } 
    
    
    
    int main()
    { int k;
      cout<<"\t\t******** Welcome to Login Program *********************";//some pretty ouput to tell them where they are
      cout<< "\n  1: LOGIN\n  2: SIGN UP\n  3: EXIT\n Please Make your Choice:";//output their choices and ask for theirs
      cin>>k;//input their choice and write it to k
      if (k==1)//cheak if k=login
      { login();//if it does then execute the login function
      }
      else if(k==2) //see if k= signup
      { signup();}
      else if(k==3)
      {return 0;}
    }

  2. #2
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Ive never done file i/o but how about using void functions instead of int?

    What is C++?

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    208

    not to sound rude

    If this sounds rude i didn't intend it to but what woud that do differently. Not that I am doubting it would work but I am relitivly new to programing and I never really had anyexperiance with useing void and such. And I think it would be good for me to learn what the difference between them is.

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    30

    Well,

    that depends on what your function is supposed to do. You should always have main return an int (0 if everything is successfull).
    Aside from that you have your function return whatever is needed. In your program there is no need for the functions to return a value that i can see, so they could be set to void.
    If you want an example of how to use different returns, then here's one thing that could be done to your program: make the signup a bool and have it return TRUE if it was successful and FALSE if it wasn't, then you can check what it returned in the function that called it (in this case main).

    it would look something like this:
    Code:
    bool signup()
    { 
      char name[10];      //Create My Variables 
      char password[10]; 
      char str[22];
    ... //more code
      //check if stuff went ok, if so:
      return TRUE;
    }
    This was just an example of how it works...
    "You... Remarkably made it rain. Rain of blood." - Tomoe

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    I see an error in your original code. You try to copy a char array using = this is a bad idea.

    check this out
    Code:
    char name[10],pass[10],filename[12],combo[20];
    cout << "Enter 8 character user name:";
    cin >> name;
    cout << "Enter 8 characater password:";
    cin >> pass;
    strcpy(filename,name); 
    // in string.h copys one string to another instead of doing
    //filename=name; which doesn't do what you want or think it does
    strcat(filename,".txt");
    strcpy(combo,name);
    strcat(combo,".");
    strcat(combo,pass);

  6. #6
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212

    int login()

    Code:
    int login(){
        char nuttin[2];//initialize my variables
        char read[21]; //Increase size at least siname + sipass + 1. Add one for NULL character
        char siname[10];
        char sipass[10];
        char txt[10];
        cout<<"Please Enter Your User Name:\t";//ask for a user name
        cin.getline(siname, 9);//write the name to siname
        //siname=txt; WRONG! For 2 reasons.
        strcpy(txt, siname);
        strcat(txt,".txt");
        ifstream d_file(txt);//open username.txt
        cout<<"Please Enter Your Password:\t";//ask for a password
        cin.getline(sipass,9);//write the password to sipass
        strcat(siname, ".");//add a period at the end of siname
        strcat(siname, sipass);// combine the two to create a variable contiaining "username.password"
        d_file>>read;
        if (strcmp(read,siname) == 0) { //compare the variables
            cout<<"You Have Succesfully Logged In:\n";//explain the login  was successful
            system("PAUSE");//end the program
            return 0;
        }
        else { 
             cout<<"\nYour Log In Was Unsuccessful.Quit. Then Restart The Program To Try Again Or Sign Up\n";
            //explain something went wrong and give directions on what to do
            system("PAUSE");//end the program 
            return 0;
        }
    }
    First of all they are char arrays. So use strcpy(). And second your a copying txt with nothing but junk in it to a variable that has just gotten input. I believe you should use

    strcpy(txt, siname);

    [EDIT] You should return two different values too, one for success, and one for failure.
    [/EDIT]

    kwigibo
    Last edited by kwigibo; 05-31-2002 at 07:36 PM.

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    208

    ????????????????

    I tried everything you guys suggested (by the way thanx for that I have learned many new things tody)
    but for sum reason the text file is still not being created???

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    208

    It just won't work

    I really don't know why it isn't creating the txt file???/
    I have tried a few modifications but none of them have worked

  9. #9
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    post your latest source code that you are using, that still wont create the file, any maybe another bug will be spotted.

  10. #10
    Registered User
    Join Date
    May 2002
    Posts
    208

    Latest Source Code

    Here it is know. It still isn't createing a txt file.

    Code:
    #include <fstream.h> //include everything that is nessacary
    #include <iostream.h>
    #include <string.h> 
    #include <stdlib.h>
    
    
    void login()
    { char nuttin[2];//initialize my variables
      char read[10];
      char siname[10];
      char sipass[10];
      char txt[10];
      cout<<"Please Enter Your User Name:\t";//ask for a user name
      cin>>siname;//write the name to siname
      strcpy(siname,txt); 
      strcat(txt,".txt");
      ifstream d_file(txt);//open username.txt
      cout<<"Please Enter Your Password:\t";//ask for a password
      cin>>sipass;//write the password to sipass
      strcat(siname, ".");//add a period at the end of siname
      strcat(siname, sipass);// combine the two to create a variable contiaining "username.password"
      d_file>>read;//write the contents of username.txt to a variable
      if (strcmp(read,siname) == 0) //compare the variables
    
    
      { cout<<"You Have Succesfully Logged In:\n";//explain the login  was successful
        system("PAUSE");//end the program
        
      }
      else 
      { cout<<"\nYour Log In Was Unsuccessful.Quit. Then Restart The Program To Try Again Or Sign Up\n";
        //explain something went wrong and give directions on what to do
        system("PAUSE");//end the program 
        
      }
          
    }  
      
    
    
    void signup()
    { 
      char name[10],pass[10],filename[12],combo[20];
    cout << "Enter 8 character user name:";
    cin >> name;
    cout << "Enter 8 characater password:";
    cin >> pass;
    strcpy(filename,name); 
    // in string.h copys one string to another instead of doing
    //filename=name; which doesn't do what you want or think it does
    strcat(filename,".txt");
    strcpy(combo,name);
    strcat(combo,".");
    strcat(combo,pass);
    cout<<"\n\nYou Have Successfully Signed Up.\n\n";
    system("PAUSE");
    } 
    
    
    
    int main()
    { int k;
      cout<<"\t\t******** Welcome to Login Program *********************";//some pretty ouput to tell them where they are
      cout<< "\n  1: LOGIN\n  2: SIGN UP\n  3: EXIT\n Please Make your Choice:";//output their choices and ask for theirs
      cin>>k;//input their choice and write it to k
      if (k==1)//cheak if k=login
      { login();//if it does then execute the login function
      }
      else if(k==2) //see if k= signup
      { signup();}
      else if(k==3)
      {return 0;}
    }
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  11. #11
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    look at your sign up function. It is not creating a file becuase you never make one!!! The code i gave you was just to show you how to get the name, pass and make the file name.
    you still need to use ofstream to make an output stream.

    void signup()
    {
    char name[10],pass[10],filename[12],combo[20];
    cout << "Enter 8 character user name:";
    cin >> name;
    cout << "Enter 8 characater password:";
    cin >> pass;
    strcpy(filename,name);
    // in string.h copys one string to another instead of doing
    //filename=name; which doesn't do what you want or think it does
    strcat(filename,".txt");
    strcpy(combo,name);
    strcat(combo,".");
    strcat(combo,pass);
    cout<<"\n\nYou Have Successfully Signed Up.\n\n";
    system("PAUSE");
    }

    oh and in your login function, you have this
    strcpy(siname,txt);
    but it should be
    strcpy(txt,siname); //the order is dest,src

Popular pages Recent additions subscribe to a feed

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