password program help!!

hello folks,
i was trying to make a password checker program,the program should do this:
1)get the password from the user.
2)checks if the password is more then 10 characters,if it's not,it should asks the user for another try.
3)next,it should get the first character of the password,and saves that character in an integer,if the number is between 60 and 90,continues,else,asks for another try.
4)prints the result.
this is what i got so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
 #include <iostream.h>
 #include <conio.h>
 
 int main()
 {
    char password[100], first_character;
    int first_character_number;
    cout << "enter your password:";
    cin >> password;
    password[0]=first_character;
    //i don't know the way to save first_character into an integer
    return 0;
}
Try to use atoi() but you'll need to include stdlib.h
would you give an example?
1
2
3
4
char str x[7]="60pass";
int i;
i=atoi(x);
cout<<i;

It'll give 60 as the output

@LendraDwi Your code wouldnt even compile the way it is, and is also incorrect - OP wants the first letter of the password i.e. password[0] converted to a integer.

just type cast it by placing an (int), there is no need for atoi...

first_character_number = (int)password[0];

So in your code it would look like...

1
2
3
4
5
6
7
8
9
10
11
12
13

#include <iostream>

int main()
{
	char password[100];
	int first_character_number;
	std::cout << "enter your password:";
	std::cin >> password;
	first_character_number = (int)password[0];
	return 0;
}
Last edited on
okay,this problem is solved but what about step "2"?
-checks if the password is more then 10 characters,if it's not,it should asks the user for another try.
????

You could use since its an array strlen - http://www.cplusplus.com/reference/cstring/strlen/?kw=strlen

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#include <iostream>

int main()
{

	char password[100];  

	do 
	{
		std::cout << "Password must be at least 10 characters in length." << std::endl;
		std::cout << "Enter your password:";
		std::cin >> password;

	} while (strlen(password) < 10);

	// we get here when password meet minimum 10 length.

	return 0;
}


Any reason why you are not using a string? - just curious. If you were using strings it would look like below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#include <iostream>
#include <string>

int main()
{

	std::string password;

	do
	{
		std::cout << "Password must be at least 10 characters in length." << std::endl;
		std::cout << "Enter your password:";
		std::cin >> password;

	} while (password.length() < 10);

	// we get here when password meet minimum 10 length.

	return 0;
}


So, before you ask about the next stage (because I have a feeling you will lol) I have provided the example below. I am assuming from your O/P that the only reason for storing the first letter was to check against it being between 60 and 90 - if this is the case then you don't need to store it in a variable to check it.

As I am not sure of your skills I will provide links below to help with my example, so apologies if you already know this information.

Do..While loops: http://www.cplusplus.com/doc/tutorial/control/
Strings: http://www.cplusplus.com/reference/string/string/

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
32



#include <iostream>
#include <string>

int main()
{

	std::string password;

	do
	{
		do
		{
			std::cout << std::endl;
			std::cout << "Password must meet the minimum requirements which are:" << std::endl;
			std::cout << "* Miniumum 10 characters in length." << std::endl;
			std::cout << "* Must start with A - Z, <, =, > or ?" << std::endl << std::endl;
			std::cout << "Enter your password:";
			std::cin >> password;

		} while (password.length() < 10);  // length check

	} while ((int)password[0] < 60 || (int)password[0] > 90);  // < to Z check

	// we get here when password meet minimum 10 length
	// and first letter is between < and Z 60 - 90 

	return 0;
}
i just searched in google to get the length of a string(char array).
there's another way to do this with a "for" loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream.h>

int main()
{
      char password[100];
      int num=0;
      cout << "password:";
      cin >> s;
      while (s[num]!='\0')
                num++;
      do
	{
		std::cout << "Password must be at least 10 characters in length." << std::endl;
		std::cout << "Enter your password:";
		std::cin >> password;

	} while(num<10);
        return 0;
}

but i think strlen() is easier,thanks for your help!

No need to use loops to check the length when there is functions provided to do the work for you :)

Your welcome :)


EDIT: Just for information purposes your last posts code will not work as the length is checked outside of the do..while loop for the first try only, and regardless if you enter a password greater than 10 length it would still loop.
Last edited on
should i use another loop?(i mean "for, while...").

Its not down to the type of loop you have used.

I will explain better; lets say I enter a password of 9 characters on line 8, you then check its length on line 9 and 10 and place its length in the num variable - num should now equal 9.

I then enter a do..while loop on line 11 regardless if I was size 10 or not so your program will ask again for a password, and never leave the loop because num will always equal 9 (i.e. your not checking its length again).

Look at my last example, I use one cin inside 2 do..while loops. Its all about the flow of your code :)











Topic archived. No new replies allowed.