No errors, no problems, just no output? Random password generator.

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 2060 Views - Last Post: 13 January 2012 - 07:25 AM Rate Topic: -----

#16 anujsharma002  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 27
  • Joined: 14-December 11

Re: No errors, no problems, just no output? Random password generator.

Posted 20 December 2011 - 02:50 AM

you can use
 rand() 

or
srand() 

functions...

EX:
#include<stdlib.h>
#include<iostream.h>
#include<time.h>
void main()
{
#include<stdlib.h>
#include<iostream.h>
#include<time.h>
void main()
{
   int i, randnum;
   unsigned int seedval;
   time_t t;
   seedval=(unsigned) time (&t);      //  time (&t) function will initialize time variable with system time.
   for(i=0;i<5;++i)
cout<<rand()<<endl;
}




Just put codes of your own along with this ...

This post has been edited by anujsharma002: 20 December 2011 - 02:52 AM

Was This Post Helpful? -2
  • +
  • -

#17 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,546
  • Joined: 23-August 08

Re: No errors, no problems, just no output? Random password generator.

Posted 20 December 2011 - 06:19 AM

Well gee, if I just wanted to get a random character, then I would setup an array of all the characters I would like to possibly include, then use a random number to select an index from that array and use that character.
Was This Post Helpful? 1
  • +
  • -

#18 mlthelasher  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-December 11

Re: No errors, no problems, just no output? Random password generator.

Posted 20 December 2011 - 12:19 PM

It is alive!!
#include <iostream>
#include <time.h>
#include <stdio.h> 
#include <stdlib.h> 
using namespace std;
void main()
{
	int length=0;
	int i=0;
	char test;
	do
	{
	srand(time(NULL));
	length=rand()%'a';length=rand()%'z';
	if (length!=test)
	{
		i++;
		test=length;
		putchar(length);
	}
	}
	while (i<8);
	cout<<"\n";
}

Was This Post Helpful? 0
  • +
  • -

#19 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,546
  • Joined: 23-August 08

Re: No errors, no problems, just no output? Random password generator.

Posted 20 December 2011 - 01:20 PM

Uh, not quite.

What is the value of test the first time through your loop?

length=rand()%'a';length=rand()%'z';


Why do you set length, then immediately change it again?

THINK about what you're doing...don't just throw some code down on the page.
Was This Post Helpful? 0
  • +
  • -

#20 mlthelasher  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-December 11

Re: No errors, no problems, just no output? Random password generator.

Posted 21 December 2011 - 12:19 PM

Now how do I make this an array?

#include <iostream>
#include <time.h>
#include <stdio.h> 
#include <stdlib.h> 
using namespace std;
void main()
{
	int length;
    int i=0;
    char test;
    do
    {
    srand(time(NULL));
    length=rand()%'a';length=rand()%'z';
    if (length!=test)
    {
        i++;
        test=length;
        putchar(length);
    }
    }
    while (i<8);
    cout<<"\n";
}

Was This Post Helpful? 0
  • +
  • -

#21 mlthelasher  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-December 11

Re: No errors, no problems, just no output? Random password generator.

Posted 22 December 2011 - 09:39 AM

View PostJackOfAllTrades, on 20 December 2011 - 08:19 AM, said:

Well gee, if I just wanted to get a random character, then I would setup an array of all the characters I would like to possibly include, then use a random number to select an index from that array and use that character.

How the hell do I do that?
Was This Post Helpful? 0
  • +
  • -

#22 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 678
  • View blog
  • Posts: 2,337
  • Joined: 31-December 10

Re: No errors, no problems, just no output? Random password generator.

Posted 22 December 2011 - 09:57 AM

Make a char array and fill it with any possible characters you want to include. Then just use the rand() % size of the array. That will give you a random index into the array, now you have a random character. Also it should be "int main()" not "void main()". The reason is that main returns a value to the operating system depending on success or failure of the program. Declaring main to be void, makes it so the function doesn't return anything.

Another thing you should be careful about is using variables that you haven't initialized yet. Like the "test" variable in your program. In the if-statement you compare the "length" variable against the "test" variable. The "test" variable hasn't been initialized with any value on the first iteration of the loop, so it holds a garbage value.
Was This Post Helpful? 0
  • +
  • -

#23 mlthelasher  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-December 11

Re: No errors, no problems, just no output? Random password generator.

Posted 03 January 2012 - 07:39 AM

soo like this...? I now can not get it down to just numbers and letters and only 8 characters...

#include <iostream>
#include <iomanip>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
void main()
{
	int length;
    int i;
    do
    {
		char test[26];
		for(int a=0;a<=26;a++)
			test[a]=i+97;
		for(int b=0;b<=26;b++)
		srand(time(NULL));
		length=rand()%'a';length=rand()%'z';
			if (length!=test[b])
			{
				test[b]=length;
				putchar(length);
				i++;
			}
    }
	while (i<8);
	cout<<"\n";
}

Was This Post Helpful? 0
  • +
  • -

#24 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3033
  • View blog
  • Posts: 10,581
  • Joined: 08-August 08

Re: No errors, no problems, just no output? Random password generator.

Posted 03 January 2012 - 08:30 AM

Have you tried to run that in XCode? It fails to compile for me for reasons others have already given.

First generate the array of characters to choose from, then randomly pick from that array until you have your 8 random characters! Here, I'll start you off:

char test [] = {"abcdefghijklmnopqrstuvwxyz0123456789"};


This post has been edited by CTphpnwb: 03 January 2012 - 12:40 PM

Was This Post Helpful? 0
  • +
  • -

#25 mlthelasher  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-December 11

Re: No errors, no problems, just no output? Random password generator.

Posted 06 January 2012 - 10:49 AM

View PostCTphpnwb, on 03 January 2012 - 10:30 AM, said:

Have you tried to run that in XCode? It fails to compile for me for reasons others have already given.

First generate the array of characters to choose from, then randomly pick from that array until you have your 8 random characters! Here, I'll start you off:

char test [] = {"abcdefghijklmnopqrstuvwxyz0123456789"};


Yeah... I am only a beginner programmer, I am still in high school and is only in INTRO to computer programming. I started this because I was bored, now I can't get it to work...
Was This Post Helpful? 0
  • +
  • -

#26 WabiSabi  Icon User is offline

  • D.I.C Head

Reputation: 51
  • View blog
  • Posts: 202
  • Joined: 31-December 10

Re: No errors, no problems, just no output? Random password generator.

Posted 06 January 2012 - 12:11 PM

Taking code from one of your posts, and using the char array you've been given, this will output as much of the array as you specify (here it's 8 characters).

A for loop is better than your do/while with an iterator (i).
#include <iostream>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main()
{

    char test[] = {"abcdefghijklmnopqrstuvwxyz"};

    int length=0;
    int i=0;

//	do
//	{
//	srand(time(NULL));
//	length=rand()%25;
////	if (length!=test[i])
////	{
//		i++;
////		//test[i]=length;
//		putchar(test[i]);
////
////	}
//	}
//	while (i<8);

    for(int i = 0; i < 8; i++)
    {
        putchar(test[i]); //why did you print length? and I'd use cout<< instead of putchar() even though it works
    }
    cout<<"\n";
    return EXIT_SUCCESS;
}


Now all you have to do is use your random number generator to get a random number between 0 and 25 (or however many characters are in your array, minus 1, because it starts at 0), and print that array index.


If you haven't studied for loops yet, see this link about loops:
http://www.dreaminco...ing-loops-in-c/

This post has been edited by WabiSabi: 06 January 2012 - 01:39 PM

Was This Post Helpful? 0
  • +
  • -

#27 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: No errors, no problems, just no output? Random password generator.

Posted 06 January 2012 - 12:44 PM

View Postanujsharma002, on 14 December 2011 - 08:02 PM, said:

Sory, I havn't tried it.
But simply try to run it using turbo c++.
Remove namespace line and replace int main by void main , add clrcsr() at begining of main section, and replace return 0 with getch().
I am sure it will run there.
Also if u want to buid it. Simply use build option from menu, it will create a *.exe windows file that can be run on any windows computer without writting code in any compiler, it will work same as other programs do.

like-

void main()
{
clrscr();
:
: //your program here
:
getch();
}

Sorry, I havn't tried means , I havn't tried yours this program but I am aware with all the steps what i have written. I use them always to make any program in turbo.


If you can't provide a help then please I request you that do not misguide someone. I suggest you that go and learn first.
You smash the program totally you smash it in every sense...

> turbo c++
> Remove namespace line
> replace int main by void main
> add clrcsr()
> replace return 0 with getch()

View Postanujsharma002, on 14 December 2011 - 08:02 PM, said:

I am sure it will run there.


It seems that you were trying to take mlthelasher back to the stone age!

Never mind me...

This post has been edited by AKMafia001: 06 January 2012 - 12:45 PM

Was This Post Helpful? 1
  • +
  • -

#28 mlthelasher  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-December 11

Re: No errors, no problems, just no output? Random password generator.

Posted 12 January 2012 - 12:20 PM

I was over complicating it...
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
using namespace std;
void main()
{
	int length;
	ofstream test;
	cout<<"How long do you want your random password: ";
	cin>>length;
	srand((unsigned)time(NULL));
	test.open("PASSWORD.DAT",ios::out);
	if(test)
	{
		test<<length<<"\n";
		test<<(char)(rand()%('z'-'0'+1)+'0')<<"\n";
		test.close();
	}
	for (int i=0;i<length;i++)
	{
		cout<<(char)(rand()%('z'-'0'+1)+'0');
	}
	cout<<"\n";
}

that's it...
Was This Post Helpful? 0
  • +
  • -

#29 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3033
  • View blog
  • Posts: 10,581
  • Joined: 08-August 08

Re: No errors, no problems, just no output? Random password generator.

Posted 12 January 2012 - 06:02 PM

It looks to me like you're still over complicating it!

  • What is test doing for you? Everything in that if statement looks superfluous to me.
  • You aren't using strings, so why include string?

This seems simpler to me:
#include <iostream>
#include <time.h>
using namespace std;
#define CHAR_LENGTH 37//75
int main()
{
	int index, length;
	srand(time(NULL));
	cout << "How long do you want your random password? ";
	cin>>length;
	char choice[CHAR_LENGTH] = {"abcdefghijklmnopqrstuvwxyz0123456789"};
	//char choice[CHAR_LENGTH] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+abcdefghijklmnopqrstuvwxyz0123456789"};
	for(int b=0;b < length;b++) {
		index = rand() % CHAR_LENGTH;
		cout << choice[index];
	}
	cout<<"\n";
	return 0;
}

It's easier to control the available characters for the password too. All you need to do is change the choice array.

This post has been edited by CTphpnwb: 12 January 2012 - 06:07 PM

Was This Post Helpful? 1
  • +
  • -

#30 mlthelasher  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-December 11

Re: No errors, no problems, just no output? Random password generator.

Posted 13 January 2012 - 07:25 AM

#include <iostream>
#include <fstream>
#include <ctime>
using namespace std;
void main()
{
	int length;
	ofstream test;
	cout<<"How long do you want your random password: ";
	cin>>length;
	srand((unsigned)time(NULL));
	test.open("PASSWORD.DAT",ios::out);
	if(test)
	{
		test<<length<<"\n";
		test<<(char)(rand()%('z'-'0'+1)+'0')<<"\n";
		test.close();
	}
	for (int i=0;i<length;i++)
	{
		cout<<(char)(rand()%('z'-'0'+1)+'0');
	}
	cout<<"\n";
}

Okay I got rid of
#include <string>
now I want to make this into a win32 app., how do I do that?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2