Thread: Help w/ classes and functions

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    3

    Help w/ classes and functions

    I am not entirely understanding these. Can somebody make a simple example for me?

  2. #2

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    3
    Thank you, but I have already done that. I'll show you what I tryed to do.

    Code:
    class login:
    }
    /*login();
    ~login();*/
    void askLogin();
    };
    
    login::askLogin();
    {
    cout<<"Please enter a username: ";
    cin>>username;
    cin.get();
    };
    
    int main
    {
    login login;
    login.asklogin();
    
    if (username == 001) {
    cout<<"It worked!";
    }
    Please note that I have it read strings. I just forgot how to do that. Its in my code. I deleted this part in my code, but I redid it almost exactly. Can anyone tell me whats wrong? Am I just calling something incorrectly?
    Last edited by Relikie; 11-04-2007 at 05:47 AM. Reason: forgot the "void"

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    A number of things are wrong.

    >> class login:{
    Unless you're going to put some base classes into your class you don't need a :

    >> void askLogin();
    That's going to be private by default. Put the public: specifier before it and all the other methods you want acessable.

    >> cin>>username;
    username isn't defined. Within your class you should have some kind of string called username. Probably in the private area.

    >> if (username == 001) {
    username is a string so when comparing you need to point to that fact and 001 goes to "001". Also the way you have programmed it makes it look as though username is a global variable (you can get in both within the class and main), which isn't great style. Make a method called getname where you return the username variable you made.

    Code:
    #include <string> 
    class login {
    private:
      std::string username;
    
    public:
      login();
     ~login();
    
      void askLogin();
      std::string getName();
    };
    Work from there.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    3
    Alright. This is starting to get confusing. Here is what I have.

    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class login {
    private:
      string username;
    
    public:
      login();
     ~login();
    
      void askLogin();
    //  string getName();
    };
    
    login::askLogin();
    {
    	cout<<"It worked!!";
    	cin.get();
    };
    
    int main()
    {
    	string username;
    	int passcode;
    
    	login login;
    	login.askLogin();
    
    	cout<<"Please input your username: ";
    	getline(cin, username, '\n');
    	
    	if (username == "nate") {
    		cout<<"\nInput accepted.\n";
    		cin.get();
    	}
    	else {
    		cout<<"\nAccess Denied.\n";
    		exit( 1 );
    	}
    
    	cout<<"Now, please enter your passcode: ";
    	cin>>passcode;
    	cin.get();
    	
    	if (passcode == 71227148) {
    		cout<<"\n\nWelcome! This is currently under construction";
    		cout<<"\nplease check back later to see more options!";
    		cin.get();
    	}
    	else {
    		cout<<"\nAccess Denied. Please try again.";
    		cout<<"\n\n";
    		exit( 1 );
    	}
    	cin.get();
    	return 1;
    }
    Can someone please tell me why it gives me errors?

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    void login::askLogin()
    {
    	cout<<"It worked!!";
    	cin.get();
    }
    Easy with the semicolons. You know how to define regular functions?

    I don't see the purpose of the login class yet.
    Last edited by anon; 11-04-2007 at 08:33 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Code:
    string login::getName() {
      return username;
    }
    
    // in main
    
    std::cout<< login_inst.getName();
    Also you shouldn't call the instance of your class by the name of the class.
    >> login login;
    bad.
    Last edited by twomers; 11-04-2007 at 08:40 AM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not to mention it doesn't work...
    You don't entirely seem to grasp what a class should be. A class is an object that is independant. Much like a car. The car works internally without the user doing anything but exposes certain methods so the driver can interact with the object.
    In this case, since you have a login class, the class itself should ask for username, then store it somwhere. Your main program should ask the class to gather login info, then ask the class to give the app the login info and see if it's correct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes being able to use other classes functions
    By rainmanddw in forum C++ Programming
    Replies: 6
    Last Post: 01-29-2006, 11:19 AM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM