need help with c# program

Discussion in 'Mac Programming' started by twoodcc, Feb 2, 2007.

  1. macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #1
    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
     
  2. macrumors 6502

    Joined:
    Oct 24, 2005
    #2
    could you post the rest of your code? Chances are the problem could lie outside the loop.
     
  3. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #3
    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;
    		}
    		
    
    	}
    }
     
  4. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #4
    well i'm pretty sure something is wrong with the array randomBytes. it gets set to zero somehow.

    any help is appreciated
     
  5. macrumors 6502

    Joined:
    May 12, 2003
    #5
    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!
     
  6. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #6
    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:
     
  7. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #7
    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!!)
     
  8. macrumors 6502

    Joined:
    May 12, 2003
    #8
    Welcome to the wonderful world of programming. :D
     
  9. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #9
    thanks. yeah, you are right. but i got it fixed now.

    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:
     

Share This Page