I am not entirely understanding these. Can somebody make a simple example for me?
I am not entirely understanding these. Can somebody make a simple example for me?
Thank you, but I have already done that. I'll show you what I tryed to do.
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?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!"; }
Last edited by Relikie; 11-04-2007 at 05:47 AM. Reason: forgot the "void"
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.
Work from there.Code:#include <string> class login { private: std::string username; public: login(); ~login(); void askLogin(); std::string getName(); };
Alright. This is starting to get confusing. Here is what I have.
Can someone please tell me why it gives me errors?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; }
Easy with the semicolons. You know how to define regular functions?Code:void login::askLogin() { cout<<"It worked!!"; cin.get(); }
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.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Also you shouldn't call the instance of your class by the name of the class.Code:string login::getName() { return username; } // in main std::cout<< login_inst.getName();
>> login login;
bad.
Last edited by twomers; 11-04-2007 at 08:40 AM.
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.