new to c++ could use some help understanding it

new to c++ could use some help understanding it

Post by Inuyasha108 on Wed May 14, 2014 9:18 pm
([msg=80702]see new to c++ could use some help understanding it[/msg])

So I just started to learn c++ and I'm having some difficulty understanding it. I know there's probably around million similar threads on here and I looked for one that would help me but I couldn't find one. I'm using jumping into c++ to help me learn and I'm having some difficulties understanding exactly what I am doing. Primarily with a simple password program it asks you to write that will allow for multiple user names and passwords to be used while limiting the number of wrong entries. I understand that it's probably really simple but I just started learning about loops and I have no idea if I'm doing this in the right manner. I am not looking for someone to fix it for me or give me the answers. I just want to know where I need to look at fixing the problem .
here's the code:
Code: Select all
#include <iostream>

#include <string>

using namespace std;

int main()

{
    string user;
    string pass;
    string pass2;
    string user2;
    int x=4;
        do
        {
            if (user == "" && pass =="")
        {
        cout << "username: ";
        cin >> user, user2;
        cout << "password: ";
        cin >> pass, pass2;
        }
        else if ((user != "n" && pass != "1") || (user2 != "b" && pass2 != "2"))
        {
            cout << "wrong username and/or password. Please try again.\n";
            cout << "You have "<< x <<" attempts remaining.\n";
            cout << "username: ";
            cin >> user >> user2;
            cout << "password: ";
            cin >> pass >> pass2;
        }
        }
        while ((user != "n" && pass != "1") || (user2 != "b" && pass2 != "2"));
        if ((user == "n" && pass == "1") || (user2 == "b" && pass2 == "2"))
        {
            cout << "access granted.";
        }
        else
        {

            cout << "denied.";
        }
}
Inuyasha108
New User
New User
 
Posts: 5
Joined: Thu Mar 18, 2010 12:15 am
Blog: View Blog (0)


Re: new to c++ could use some help understanding it

Post by pretentious on Thu May 15, 2014 1:28 am
([msg=80705]see Re: new to c++ could use some help understanding it[/msg])

Here's something that compiles. I get that you're new to this so I made the code relatively advanced, just to be a dick :)
It is a lot cleaner than what you had. mostly because of the dynamic memory allocation. when you get into containers like vectors and what not. That's when things get powerful
Code: Select all
#include <iostream>
#include <map>
#include <string>
using namespace std;

int main(){
   map<string, string> creds;
   creds.insert(std::pair<string, string>("pretentious","123456"));
   // insert more credentials if needed
   int attempts = 0;
   do{
      cout<< "enter username: ";
      string username;
      cin >> username;
      cout << "enter password: ";
      string password;
      cin >> password;
      if( creds.find(username)->second == password){
         cout << "you're in"<<endl;
   return 0;   
   }
         else{
            cout << "wrong password. "<< 3-(++attempts)<< " left"<<endl;
   }
   }while(attempts <= 3);
   cout << "you're out"<<endl;
}


-- Thu May 15, 2014 5:55 pm --

i actually just tried to run your code. I'm not quite sure what you're trying to do. You have cin getting 2 seperate pairs of usernames and passwords?
I would suggest at the least, making an array. You could have a 2 dimensional array and use a loop to go through them. You only want to compare one at a time.
Maybe I'm totally off. I'm kinda doing 5 things at once right now
Goatboy wrote:Oh, that's simple. All you need to do is dedicate many years of your life to studying security.

IF you feel like exchanging ASCII arrays, let me know ;)
Can you say brainwashing It's a non stop disco
User avatar
pretentious
Addict
Addict
 
Posts: 1201
Joined: Wed Mar 03, 2010 12:48 am
Blog: View Blog (0)


Re: new to c++ could use some help understanding it

Post by Inuyasha108 on Thu May 15, 2014 2:40 am
([msg=80706]see Re: new to c++ could use some help understanding it[/msg])

no your right on, I was trying to compare two different sets of user names and passwords while limiting the number of attempts made to gain access but I didn't think that it would work if I tried two separate cin lines. I'll see if I can get it to work with with the suggestion you made. Thanks for the help.

EDIT:
So this is what I came up with when i reworked the program it's not as clean as yours, but it works. I've used all the commands I know for the most part to make it work. I'm sure there is a better way to do this but for right now I think this is advanced as I can get with my understanding of c++. Is there anyway I could clean this up so it's easier to understand at a later date?
Code: Select all
#include <iostream>

#include <string>

using namespace std;

int main()

{       string user;
        string pass;
        string user1 = "n";
        string pass1 = "1";
        string user2 = "b";
        string pass2 = "2";
    int attempts=0;
        do
        {
        cout << "username: ";
        getline(cin, user, '\n');
        cout << "password: ";
        getline(cin, pass, '\n');

        if (user == user1 && pass == pass1)
        {
            cout << "access granted.";
            return 0;
        }
        if    (user == user2 && pass == pass2)
        {
            cout << "access granted.";
            return 0;
        }
        else
        {
            cout << "wrong username and/or password. Please try again.\n";
            cout << "You have "<< (3-(++attempts)) <<" attempts remaining.\n";
        }
        }
        while (attempts < 3);
        {
            cout << "go away.";
        }
}
Inuyasha108
New User
New User
 
Posts: 5
Joined: Thu Mar 18, 2010 12:15 am
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests