hello everyone,
i am really new to C++ and trying to learn the basics of creating a class. I have several books on C++ and have read each chapter on classes. Funny thing is, when I read the book, forum posts, or online tutorials, I understand the flow of everything. I guess it is because I can see and follow the whole program start to finish. My problem is creating my own class or classes.
I created the following short program trying to keep it fairly simple and straight forward so I could create a class called
auth which would take the users input, match it against the correct input, and either welcome the user or reject them after 3 attempts. I also wanted to create a class called
counter that would count each failed attempt to login and then boot the unauthorized user.
The basics of classes are to keep the code clean, clear, and portable right? So that is what I am trying to do. Make two classes and also be able to reuse them later as I am learning more.
I am using Dev-C++ Bloodshed Compiler to create the program, and run everything from command line.
I am not posting my several failed attempts of the class. Just the program that works. Can someone please maybe explain this so I understand better.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#include <iostream>
#include <string>
using namespace std;
string a; //string varible to hold user input
string b ("chuck"); //string varialbe to hold password to validate against
unsigned short int c(3); //varible to count down against for user login atempts
string d; //string varible to hold users name
int main() { //enter main
cout << "Please enter your password:\a" <<endl; //ask for password
getline (cin, a); //assign input to string variable a
if( a==b ){ //if password correct welcome user
cout << "Password correct!" << endl;
cout << "Welcome to this short program!" << endl;
cout << "What is your name?" << endl;
getline (cin, d);
cout << "Nice to meet you, " << d << "!" << endl;
}else{ //if password incorrect ask again
while(a != b && c > 0){ //loop as long as password wrong or 3 failed attempts
cout << "Wrong username, you may try again " << c << " times." << endl;
getline (cin, a); // assign input to variable a
c--; //decrement the counter by 1
}
cout << "Go away! You dont belong here." <<endl; // tell them to leave
}
return 0;
}
|
Thanks for your time and any help offered.