PDA

View Full Version : need help with c# program




twoodcc
Feb 2, 2007, 11:06 AM
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[i] = a_inputPhrase[(int)randomBytes[i] % allowedCharCount];

}

please help!

thanks in advance



scan
Feb 2, 2007, 02:49 PM
could you post the rest of your code? Chances are the problem could lie outside the loop.

twoodcc
Feb 2, 2007, 06:04 PM
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

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;
}


}
}

twoodcc
Feb 2, 2007, 06:10 PM
well i'm pretty sure something is wrong with the array randomBytes. it gets set to zero somehow.

any help is appreciated

therevolution
Feb 2, 2007, 06:27 PM
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.



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:

chars[i] = a_inputPhrase[(int)randomBytes[i] % allowedCharCount];

So you shouldn't be surprised that it's still empty!

twoodcc
Feb 2, 2007, 06:32 PM
That's because you're not doing anything with it before you try to use it.



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:

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
Feb 2, 2007, 06:36 PM
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!!)

therevolution
Feb 2, 2007, 06:55 PM
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:

Welcome to the wonderful world of programming. :D

twoodcc
Feb 2, 2007, 07:40 PM
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: