C++ A few array questions/problems

closed account (91T7Djzh)
Hey everyone,

I have a few problems with my code. I am first year university student and this is one of my assingments.
I must create a mini 'system' that has Username, password and type of account. Maximum 10 accounts/passwords. And 2 types of accounts 1 for admin and 2 for normal-user. The person using the program should be able to:
1. Login if he enters an existing username with Its right password.
2. Create new users if the user he logs in is an admin (Actually not stated in the exercise so I could skip that part and just allow anyone create new user)
3. User is being locked after 3 wrong password inputs. (I just end the program if that happens)

A few keypoints and there comes my problem.
1st. The password must be hashed using pre-determined hash which they gave me. It goes like this "a=1, b=2, c=3, d=4 and etc.." you get the idea. The "hashed" password that will be saved is the sum of all the letters used in the password, hence password "banana" = "2+1+14+1+14+1" and it will save "33". To authenticate the password the user types and the sum after it gets "hashed" should be equal to 33(Yes I realise they can use combination of other letters and still get the number 33, but that is not the point of the exercise)
We are assuming that the password will always be using lowercase letters and only letters.


I was thinking to create Arrays for Username, Password and type with a maximum of 10 total entries. But in that case how do I make the program access a specific part of the array or check if the entered username already exist within the array, same for the passwords, but the passwords come even more complicated, how do I convert the password as stated...
I searched through google and some old threads here, but didn't find anything that could actually help me.

The code I had writted I pretty much scrapped, right now it's only:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

using namespace std;

int main() {
	int a = 1;
	int b = 2;
	int c = 3;
	int d = 4;
	int e = 5;
// .... You get the idea.

	int y = 25;	
	int z = 26;

       return 0;
}


What I'd like from you guys is to throw a few pointers as to whether I should use Objects and try to figure something out with that or even if not using objects If I can make that "system" work within a single main file (Yes I do realise that will be really long, but it doesn't matter as long as I finish the assingment)

Thanks in advance! :)
There's a neat little trick involving ASCII values and characters, that allows you to map say, 'a' to 1, 'b' to 2, etc.
1
2
std::cout << 'c' - 'a' << '\n';
// output: 2 

Now we want 'c' to map to 3, not 2, so just add 1.
1
2
std::cout << 'c' - 'a' + 1 << '\n';
// output: 3 
Last edited on
closed account (91T7Djzh)
Hey integralfx,

Let me see if I understand you correctly. I should write down a 'c' - 'a' + 1 << '\n'; then +2, +3 etc... for the rest of the letters in the alphabet.
But how do I use the replaced letters to sum them up? Would the program do it automatically by assingning the number? I am not sure exactly how that works.

Thanks again!


*EDIT*

The program should do the letters going into numbers and summing them for any written password word. It doesn't have to be a set up like "banana"
Last edited on
I was thinking to create Arrays for Username, Password and type with a maximum of 10 total entries.

Using arrays would be quite complicated to work with. Are you allowed to use classes and vectors ?
closed account (91T7Djzh)
Hey Thomas1965,

Yes I am indeed allowed to use any kind of objects, classes and vectors. I am just not really sure nor experienced with them so I didn't know how to do it.
Just an idea:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const int MAX_ACCOUNTS = 10;

enum class AccountType
{
  ADMIN,
  USER
};

struct User
{
  AccountType Type;
  string Name;
  int Password; // hash value i.e. 33
};

vector<User> users;

Let me see if I understand you correctly. I should write down a 'c' - 'a' + 1 << '\n'; then +2, +3 etc... for the rest of the letters in the alphabet.

That's a good try, but it would end up the same as your original code. This is how I'd use it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int get_value( char c )
{
    // 'a' is different to 'A'
    // so 'c' - 'a' yields a different
    // value to 'c' - 'A'
    c = std::tolower( c );
    return c - 'a';
}

int main()
{
    std::string str = "banana";
    int hash = 0;
    for( int i = 0; i < str.length(); i++ ) {
        int val = get_value( str[i] );
        hash += val;
    }
    std::cout << hash << '\n';
}
Topic archived. No new replies allowed.