Struggling with classes

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.
I think it would be better if you posted your failures, since then I could point out the mistakes, rather than do stuff for you.

Anyways, this could look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class counter{
    int tries_left;
    string correct_password;

    counter(int, string);
    int attempt(string);
};
class auth{
    counter c;
    string name;

    auth();
    void update();
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
counter::counter(int c, string p) : tries_left(c), correct_password(p){}
int counter::attempt(string t){
    while(tries_left>0){
        if(t == correct_password) return 1;//success
        else{
            //ask again
        }
    }
    //go away!
    return 0; //fail
}

auth::auth() : c(3, "chuck"){}
void auth::update(){
    //ask for password
    if(c.try(/*inputed_password*/)){
        //welcome and deal with naming
    }
}


This should work. You can do the rest, I think. You only need to replace my comments with appropriate lines of the code you posted (just change the variable names).
Topic archived. No new replies allowed.