Password Generator.

A simple program of mine.

Page 1 of 1

7 Replies - 1730 Views - Last Post: 09 September 2009 - 01:35 PM Rate Topic: -----

#1 EddA.  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-September 09

Password Generator.

Posted 07 September 2009 - 12:16 PM

Hello all, I am new here and this is my first post. I am in the process of creating a simple password generator and have got a problem in the "main" function of the program. You see I wanted the program to allow the user to enter the letters "C" or "Q" to continue or quit; the problem is the fact that whenever I go and test this function both inputs causes the program to quit. Here is the code.

/****************************************
This is the main source file for this 
program, "Password Generator 2".
****************************************/

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <math.h>
using namespace std;

void passwordis();   
float rand_0toN1(float n); 

char *password[46] = {"a", "b", "c", "d", "e", "f", "g", "h", "i",	   
							 "j", "k", "l", "m", "n", "o","p", "q", "r",		
					  "s", "t", "u", "v", "w", "x", "y", "z", "1",	   
					  "2", "3", "4", "5", "6", "7", "8", "9", "0",
					  "!", "@", "#", "$", "%", "^", "&", "*", "(",
					  ")"};

int main() {

int user_input;  
int i;			
int C;   

srand(time(NULL)); 

cout << "Hello this is a password generator program designed to give the user a desired " << endl;
cout << "password. ";
cout << endl;
cout << endl;
cout <<				  "   ---------------TO CONTINUE PLEASE PRESS C. TO EXIT PRESS Q.---------------";
cout << endl;
cout << endl;
cin >> user_input;	
C = (1);	
	if (user_input == 'C')   // The Problem is in this if else statement. 
		return C;
	 else (user_input == 'Q');
		   cout << "Thank you for using this program! ";
		   return 0;
			  
while (1) {		 
	 cout <<  "Please enter a number from 1 to 50. This will be the length of our password. ";
	 cin >> user_input;
		 if (user_input >= 0 | user_input <= 51	)
			 cout << "Please enter a number that ranges from 1 to 50. ";
			  break;
			  continue;
			 for(i = 1; i <= user_input; i++)
				passwordis();
}
return 0;
}

void passwordis() {
	
	float passw; 
	passw = rand_0toN1(46);
	cout << "Your new password is " << password << endl;
}

float rand_0toN1(float n) {
	return rand() / n;
}



I know this is a simple program but I am new to programming and still in the process of learning. Any help will be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Password Generator.

#2 trixt.er  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 46
  • View blog
  • Posts: 419
  • Joined: 28-September 08

Re: Password Generator.

Posted 07 September 2009 - 12:31 PM

You need to slow down and organize your code.
/****************************************
This is the main source file for this
program, "Password Generator 2".
****************************************/

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <math.h>
using namespace std;

void passwordis();
float rand_0toN1(float n);

char *password[46] = {"a", "b", "c", "d", "e", "f", "g", "h", "i",
							 "j", "k", "l", "m", "n", "o","p", "q", "r",
					  "s", "t", "u", "v", "w", "x", "y", "z", "1",
					  "2", "3", "4", "5", "6", "7", "8", "9", "0",
					  "!", "@", "#", "$", "%", "^", "&", "*", "(",
					  ")"};

int main() {

char user_input;
int i;
int C;

srand(time(NULL));

   cout << "Hello this is a password generator program designed to give the user a desired " << endl << "password. "
		<< endl << endl
		<<				  "   ---------------TO CONTINUE PLEASE PRESS C. TO EXIT PRESS Q.---------------"
		<< endl << endl;
   cin  >> user_input;
   C = (1);
   if (toupper(user_input) == 'Q')
   {
		   cout << "Thank you for using this program! ";
		   return 0;
   }

   while (1)
   {
	 cout <<  "Please enter a number from 1 to 50. This will be the length of our password. ";
	 cin >> user_input;
	 if (user_input <= 0 | user_input > 51	)
	 {
		 cout << "Please enter a number that ranges from 1 to 50. ";
		 continue;
	 }
	 // for(i = 1; i <= user_input; i++)
	 passwordis();
	 cin.ignore(200, '\n'); // Clear the buffer and new line..
   }
return 0;
}

void passwordis() {

	float passw;
	passw = rand_0toN1(46);
	cout << "Your new password is -> " << passw << " " << password << endl;
}

float rand_0toN1(float n)
{
	return rand() / n;
}



For example your for loop was outputting a tone of new passwords. In your passwordis function your char array
address is being outptut. And you are not even using the passw float variable.
the cin.ignore(200, '\n') will clear out the buffer and remove the newline that is corrupting the input phase.
Alright this should help.
Was This Post Helpful? 0
  • +
  • -

#3 stev3  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 04-June 09

Re: Password Generator.

Posted 07 September 2009 - 01:08 PM

I dont want to spoil your project by giving you the code... but here is an idea of how to get your password printed out neatly.

You could use a loop (i < pass_length)

			k = rand() % 46;
			cout << password[k];



View Posttrixt.er, on 7 Sep, 2009 - 11:31 AM, said:

You need to slow down and organize your code.

   C = (1); //not the best way of doing things (unless you "C = (0) at the end of the while loop)

   if (toupper(user_input) == 'Q')
		   cout << "Thank you for using this program!";
   else if (toupper(user_input) == 'C')
   {
	 cout <<  "Please enter a number from 1 to 50. This will be the length of our password. ";
	 cin >> user_input;
		 // Loop i mentioned here
   }
return 0;
}


This post has been edited by stev3: 07 September 2009 - 01:18 PM

Was This Post Helpful? 0
  • +
  • -

#4 AGRAC393  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 248
  • Joined: 13-May 09

Re: Password Generator.

Posted 07 September 2009 - 02:16 PM

So, you wrote all that yourself... and you don't know how to fix that issue???
Was This Post Helpful? 0
  • +
  • -

#5 keithgarry  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 63
  • Joined: 26-August 09

Re: Password Generator.

Posted 07 September 2009 - 02:22 PM

Hey there, just got done solving your problem.


 

/****************************************
This is the main source file for this
program, "Password Generator 2".
****************************************/

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <math.h>
using namespace std;

char passwordis();  //Changed returned void to returned char;
char rand_0toN1( int n ); // Changed passed value to int; Changed returned value to char;



Floats Vs Ints
Only use floats when you need to.
Floats are numbers that have a decimal value.
Floats take up more memory than ints. <--- WRONG: They take the same amount of memory.

Returned char
The returned char will go into its OWN users password array.

const char * const password[46] = {
						"a", "b", "c", "d", "e", "f", "g", "h", "i",	  
						"j", "k", "l", "m", "n", "o","p", "q", "r",		
						"s", "t", "u", "v", "w", "x", "y", "z", "1",	  
						"2", "3", "4", "5", "6", "7", "8", "9", "0",
						"!", "@", "#", "$", "%", "^", "&", "*", "(",
						")"
					};
char our_password[50];



Const
const means constant, or forever the same
I used const char * to let the program know that the pointer will never change

char *smelly= "ewww";
e is a constant, w is a constant, but smelly is not.
e is forever represented with the number 101 in decimal format.
w is forever represented with the number 119 in decimal format.
what happens if you write cout << (int)'e';
what do you think is going on behind the scenes?
what happens if you cout << (int)"e"; instead?



The rest is up to you to figure out! Goodluck man.

This post has been edited by keithgarry: 07 September 2009 - 03:12 PM

Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: Password Generator.

Posted 07 September 2009 - 02:45 PM

Quote

Floats take up more memory than ints.

Not on modern computers. Int is generally a 32 bit value as is a float. It is not even always true that floating point operations take more time than integer operations. I agree with your advice I just wanted to point out the detail.
Was This Post Helpful? 0
  • +
  • -

#7 keithgarry  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 63
  • Joined: 26-August 09

Re: Password Generator.

Posted 07 September 2009 - 03:11 PM

View PostNickDMax, on 7 Sep, 2009 - 01:45 PM, said:

Quote

Floats take up more memory than ints.

Not on modern computers. Int is generally a 32 bit value as is a float. It is not even always true that floating point operations take more time than integer operations. I agree with your advice I just wanted to point out the detail.



Thanks. I must have been thinking double for some reason.
cout << sizeof(int);
cout << sizeof(float);


Was This Post Helpful? 0
  • +
  • -

#8 EddA.  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-September 09

Re: Password Generator.

Posted 09 September 2009 - 01:35 PM

Thanks for all the replies. I will put all the suggestions in use and finish this program. Also the reason I use "float" in this program is because whenever I used "int" the program crashed but I think it had something to do with the buffers.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1