Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
hey, i have an assignment due very soon that i'm having trouble with. i am using c#.

the assignment is to create a random password from a word that the user inputs.

my problem is that what the user inputs, all i get back is the letter "a" for how ever many characters the user puts in. i think something is wrong wtih my for loop. here it is:

for(int i = 0; i< PasswordLength; i++)
{
chars = a_inputPhrase[(int)randomBytes % allowedCharCount];

}

please help!

thanks in advance
 

scan

macrumors 6502
Oct 24, 2005
344
0
could you post the rest of your code? Chances are the problem could lie outside the loop.
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
could you post the rest of your code? Chances are the problem could lie outside the loop.

alright, here is everything i got. thanks for helping me out. it's due tomorrow

Code:
using System;
using System.Collections.Generic;
using System.Text;

namespace WoodT_program1
{
	class WoodT_program1
	{
		static void Main(string[] args)
		{
			
			//create a PasswordGenerator object
			PasswordGenerator spongeBob = new PasswordGenerator();
			
			// Get input from the user
			Console.WriteLine("Please enter a word to create a password: ");
			Console.WriteLine(spongeBob.GeneratePassword()); //call the method and display results
			
			
		}
	}
	
	//put class definition here
	public class PasswordGenerator
	{
		// class members
		private string inputPhrase;
		private int passwordLength;
		
		//class method
		public string GeneratePassword()
		{
			/* some of the following code I got from the website:
			http://www.dotnetjunkies.com/WebLog/warstar/archive/2004/08/15/22126.aspx
			I changed some of the code for my program.
			*/
			string a_inputPhrase;
			
			// variable to hold user input
			a_inputPhrase = Console.ReadLine();
			// set the length
			SetLength(a_inputPhrase.Length);
			string _allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789!@$?";
	
			//get the length of the password
			int PasswordLength = GetLength();
			byte[] randomBytes = new byte[PasswordLength];
			char[] chars = new char[PasswordLength];

			int allowedCharCount = _allowedChars.Length;
			


			for(int i = 0; i< PasswordLength; i++)
			{
				chars[i] = a_inputPhrase[(int)randomBytes[i] % allowedCharCount];
			
			}
		
			return new string(chars);
			
		}
		// mutators (setters)
		public void SetLength(int a_passwordLength)
		{
			passwordLength = a_passwordLength;
		}
		// accessors (getters)
		public int GetLength()
		{
			
			return passwordLength;
		}
		

	}
}
 

therevolution

macrumors 6502
May 12, 2003
468
0
well i'm pretty sure something is wrong with the array randomBytes. it gets set to zero somehow.
That's because you're not doing anything with it before you try to use it.


Code:
byte[] randomBytes = new byte[PasswordLength];
This line creates the empty array of size PasswordLength, but then you don't do anything else before you try to use it here:
Code:
chars[i] = a_inputPhrase[(int)randomBytes[i] % allowedCharCount];
So you shouldn't be surprised that it's still empty!
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
That's because you're not doing anything with it before you try to use it.


Code:
byte[] randomBytes = new byte[PasswordLength];
This line creates the empty array of size PasswordLength, but then you don't do anything else before you try to use it here:
Code:
chars[i] = a_inputPhrase[(int)randomBytes[i] % allowedCharCount];
So you shouldn't be surprised that it's still empty!

yeah i know. after googling a lot, i found someone who did the same program, and that helped me out. i got it to work now.

thanks. sorry, i was just stuck on this thing. i was up til 5am last night, and couldn't get it. it was driving me up the wall :eek:
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
I don't know c#, but it doesn't look like you've ever initialized the byte array randomBytes. It looks like you've only set it to be "PasswordLength" bytes long.

Todd


(edit - a little slow on the draw!!)
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
I don't know c#, but it doesn't look like you've ever initialized the byte array randomBytes. It looks like you've only set it to be "PasswordLength" bytes long.

Todd


(edit - a little slow on the draw!!)

thanks. yeah, you are right. but i got it fixed now.

Welcome to the wonderful world of programming. :D

thanks.....i've taken 2 other courses (matlab, visual basic) and this is my third language, and third intro course. makes it a little easier, but just more frustrated when you can't figure it out :eek:
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.