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

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 mlthelasher  Icon User is offline

  • New D.I.C Head

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

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

Posted 14 December 2011 - 07:37 AM

I have a predicament, my random password generator does not come up with any errors, but it does not output anything! Here it is:

#include <iostream>
#include <time.h>
#include <stdio.h>
#include <process.h>
#include <stdlib.h>
#include <sys/types.h>
using namespace std;
int getpid();
void main()
{
	int length=0;
	srand(time(NULL));
	{
	do
	{
		int get_Record();
		int getpid();
		length++;
	}
	while(length);
	putchar(true);
	srand(true);
	}
}


I have no clue what is wrong, I have tried everything I know. I have even just messed around with it. The only output I did get, when I was messing around with it, was numbers. I tried to get an idea of where things should be, by using my random number generator, is that where I went wrong? Or am I missing something so blatantly simple?

Is This A Good Question/Topic? 0
  • +

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

#2 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 445
  • View blog
  • Posts: 849
  • Joined: 17-March 11

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

Posted 14 December 2011 - 07:51 AM

Your code is a little confusing so let me ask you some questions about it first.

int get_Record();
int getpid();



What are these statements supposed to do ? They look a little like function calls/declarations, but they are not. Basically you are declaring 2 variables, this is the same as:

int get_Record;
int getpid;



while(length)



This is the same as while(length != 0), but length keeps increasing, so will never become 0. This loop will never end.

putchar(true);



putchar outputs a character, yet you give it a boolean. What happens is the boolean true (1) gets converted to a char 1. The character that is then printed is the character at position 1 in the ascii table, but this is not a printable character. It is not clear to me what you do want to print.

srand(true);



This re-initializes your random generator with a seed of true (1), why ?

This post has been edited by Karel-Lodewijk: 14 December 2011 - 07:56 AM

Was This Post Helpful? 2
  • +
  • -

#3 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 14 December 2011 - 08:02 AM

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.
Was This Post Helpful? -3
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon

Reputation: 3048
  • View blog
  • Posts: 9,282
  • Joined: 25-December 09

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

Posted 14 December 2011 - 08:06 AM

Sorry, but please let Turbo-C die in peace.

The function main() should be defined as returning an int, not void, int main() and you should return an int from this section. Using void main is non-standard and will produce an error on many of the modern compilers.

The clrscr() and getch() functions are non-standard, operating system specific functions that should be avoided in most cases.

You need to find a compiler that was released in this century, and is standard compliant. You may want to look into Visual C++, Code::Blocks, WXDevC++.

Jim
Was This Post Helpful? 2
  • +
  • -

#5 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 14 December 2011 - 11:56 AM

I have Microsoft Visual C++ 6.0. If I understand what you are saying, I need to make
void main()
to
int main()
also I need to get rid of
int get_Record();
                                                                  int getpid();

and make
while(length)
to
while(length!=0)
also make
putchar(true); 
into
putchar(NULL);
and get rid of the second
srand(true);
Is this correct??

So this is what I have...
#include <iostream>
#include <time.h>
#include <stdio.h>
#include <process.h>
#include <stdlib.h>
#include <sys/types.h>
int getpid();
int main()
{
	int length=0;
	srand(time(NULL));
	{
	do
	{
		length++;
	}
	while(length!=0);
	putchar(NULL);
	}
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon

Reputation: 3048
  • View blog
  • Posts: 9,282
  • Joined: 25-December 09

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

Posted 14 December 2011 - 12:18 PM

Please note Visual C++ 6.0 is almost as bad as Turbo-C, you should upgrade this compiler to the latest version VC++ 2010.

You also seem to have one extra brace pair {}, you don't need the opening brace that is after the srand() call nor the closing brace after your putchar() call.

What is the purpose of your putchar(NULL); call?


Jim
Was This Post Helpful? 1
  • +
  • -

#7 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 16 December 2011 - 07:21 AM

putchar(NULL); is what allows me to output characters, without it, I would be outputing numbers. So, I did a little bit of reserch and now I am trying this:
#include <iostream>
#include <time.h>
#include <stdio.h>
#include <process.h>
#include <stdlib.h>
#include <sys/types.h>
int getpid();
int main()
{
	char length;
	srand(time(NULL));
	for(length='a'; length<='z';length++);
	putchar(length);
	return 0;
}


But now it only outputs "{". And the only reason I use Microsoft Visual C++ 6.0, is because that is what is provided at my school where I am doing this. I have tried VC++ 2010, but I can't get it to output using CMD.
Was This Post Helpful? 0
  • +
  • -

#8 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 16 December 2011 - 07:26 AM

Wait I messed up, here is the correct version and it now only outputs the alpabet.
#include <iostream>
#include <time.h>
#include <stdio.h>
#include <process.h>
#include <stdlib.h>
#include <sys/types.h>
using namespace std;
int main()
{
	int length=0;
	srand(time(NULL));
	for(length='a'; length<='z';length++)
	{
	putchar(length);
	}
	cout<<"\n";
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon

Reputation: 3048
  • View blog
  • Posts: 9,282
  • Joined: 25-December 09

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

Posted 16 December 2011 - 08:06 AM

Quote

I have tried VC++ 2010, but I can't get it to output using CMD.


What do you mean by "I can't get it to output using CMD."? You may need to add a getchar() statement at the end of your program to keep the window open, but the program should run, if there are no compile errors.

Jim
Was This Post Helpful? 0
  • +
  • -

#10 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 16 December 2011 - 08:09 AM

You can get vc ++ 2010 free of cost for personal use. Just google for download link. I have downloaded it and its wrking fine. But, problem is what turbo is being provided in the institution where i am learning this.
Don't matter I will check your program and return back to you recently.
Was This Post Helpful? 0
  • +
  • -

#11 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 16 December 2011 - 11:45 AM

Soo, how do I fix it from just putting out the alphabet, and output something random?
Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is offline

  • member icon

Reputation: 3048
  • View blog
  • Posts: 9,282
  • Joined: 25-December 09

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

Posted 16 December 2011 - 01:32 PM

Well first you would need some kind of random data to print. You may want to study the documentation for rand() and then try to generate a random number and print that number.

Jim
Was This Post Helpful? 0
  • +
  • -

#13 eypros  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 08-October 11

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

Posted 17 December 2011 - 11:07 AM

srand(time(NULL));


You are not really creating any random number this way. You just seed the random number generator. That is give your generator a number so that it produces the same sequence of numbers for the same seed. Because of this we normally use
time(NULL)

the UNIX time (that changes all the time as can be seen).

The random number is normally given by
rand()
this is the call to random number generator that produces a random number (int). So normally by taking the modulo of this number versus another number N we get N different (random) numbers.

I guess you may want to see also
rand()

Something final about randomness of this generator:

they are not fully random just pseudorandom number generators. That means they create a rather big number but are not random in the strict mathematical sense. Normally this does not affect the way they are used but in some cases there might be implication.

Some examples of these implication include:
1) creating just 0 and 1 often (depends on the compiler I think this one) just switch between 0 and 1 and this means a strict sequence like 0,1,0,1,0,1,0,1 etc and not 0,1,1,0,0,0,1,0,1,1,1,1 etc
2) If big number is used as N above some numbers are favored than others. That is the probability of appearing some numbers is often much higher than others in the sequence. I am not expanding more in the issue though.

Hope that gave you an idea about randomness in c++
Was This Post Helpful? 0
  • +
  • -

#14 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 19 December 2011 - 07:23 AM

I am NOT creating a random NUMBER generator, but I AM creating a random PASSWORD/CHARACTER/STRING generator. I know how to create a random number generator. However, I do not know how to create a random character generator.
Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg  Icon User is offline

  • member icon

Reputation: 3048
  • View blog
  • Posts: 9,282
  • Joined: 25-December 09

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

Posted 19 December 2011 - 07:39 AM

Characters are numbers. See this link: ASCII Chart.

Jim
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2