C
C#2y ago
Elmishh

❔ ✅ can anyone tell me what my problem is?

using System;
static class RandomLetter
{
static Random _random = new Random();
public static char GetLetter()
{
// This method returns a random lowercase letter.
// ... Between 'a' and 'z' inclusive.
int num = _random.Next(0, 25); // Zero to 25
char let = (char)('a' + num);
return let;
}
}

class passwordgenerator
{
public static void Main()
{
System.Random random = new System.Random();

Console.WriteLine("enter wanted length:");
var length = Console.ReadLine();
var whentostop = 0;
var slength = length.ToString();
var swhentostop = whentostop.ToString();

var password = "";

while (true)
{
var letter = RandomLetter.GetLetter();
var number = random.Next(0, 9);
var NoM = random.Next(0, 4);

if (NoM <= 2)
{
password += number;
whentostop++;
}
if (NoM >= 3)
{
password += letter;
whentostop++;
}
if (swhentostop == slength)
{
Console.WriteLine($"password generated is {password}");
}
}
}
}
using System;
static class RandomLetter
{
static Random _random = new Random();
public static char GetLetter()
{
// This method returns a random lowercase letter.
// ... Between 'a' and 'z' inclusive.
int num = _random.Next(0, 25); // Zero to 25
char let = (char)('a' + num);
return let;
}
}

class passwordgenerator
{
public static void Main()
{
System.Random random = new System.Random();

Console.WriteLine("enter wanted length:");
var length = Console.ReadLine();
var whentostop = 0;
var slength = length.ToString();
var swhentostop = whentostop.ToString();

var password = "";

while (true)
{
var letter = RandomLetter.GetLetter();
var number = random.Next(0, 9);
var NoM = random.Next(0, 4);

if (NoM <= 2)
{
password += number;
whentostop++;
}
if (NoM >= 3)
{
password += letter;
whentostop++;
}
if (swhentostop == slength)
{
Console.WriteLine($"password generated is {password}");
}
}
}
}
is the code(i got the first part for the random letter off of the internet) and it just dos not work and i do not know why, i input a number and nothing happens no errors and i dont know what that warning means
27 Replies
Jimmacle
Jimmacle2y ago
there is a lot wrong with that code to be honest
Elmishh
ElmishhOP2y ago
probably
Jimmacle
Jimmacle2y ago
it might help if you comment it and explain what each thing is supposed to be doing
Elmishh
ElmishhOP2y ago
static class RandomLetter
{
static Random _random = new Random();
public static char GetLetter()
{
// This method returns a random lowercase letter.
// ... Between 'a' and 'z' inclusive.
int num = _random.Next(0, 25); // Zero to 25
char let = (char)('a' + num);
return let;
}
}
static class RandomLetter
{
static Random _random = new Random();
public static char GetLetter()
{
// This method returns a random lowercase letter.
// ... Between 'a' and 'z' inclusive.
int num = _random.Next(0, 25); // Zero to 25
char let = (char)('a' + num);
return let;
}
}
is supposed to give me a random letter from a-z(lowercase) when i call for
RandomLetter.GetLetter();
RandomLetter.GetLetter();
Jimmacle
Jimmacle2y ago
that code is fine
Elmishh
ElmishhOP2y ago
yeah i took it off of google
Jimmacle
Jimmacle2y ago
the most obvious issue is that there is no way for your program to stop the second is the part that seems like it should make it stop would never make it stop anyway because it checks things that don't change in the loop
Elmishh
ElmishhOP2y ago
Console.WriteLine("enter wanted length:");
var length = Console.ReadLine();
var whentostop = 0;
var slength = length.ToString();
var swhentostop = whentostop.ToString();
Console.WriteLine("enter wanted length:");
var length = Console.ReadLine();
var whentostop = 0;
var slength = length.ToString();
var swhentostop = whentostop.ToString();
is supposed to ask me how long i want the string to be length asks the user whentostop is used later to make i can break; on the exact length of length
Jimmacle
Jimmacle2y ago
why are you turning your numbers into strings?
Elmishh
ElmishhOP2y ago
well i need to turn them both into the same thing when i think of it i can turn only one of them into a string since length is already a string
Jimmacle
Jimmacle2y ago
strings have a .Length property that gives you a number saying how many characters it has
Elmishh
ElmishhOP2y ago
var passsword = "";
var passsword = "";
is so i have an empty variable that i can add onto later
Jimmacle
Jimmacle2y ago
go through this code line by line and explain what it's supposed to do
Elmishh
ElmishhOP2y ago
well i didnt know that considering ive been trying to learn C# for like 3 days i barely know anything
Jimmacle
Jimmacle2y ago
you may also want to avoid var until you're more familiar with what types different things return
string length = Console.ReadLine();
int whentostop = 0;
string slength = length.ToString();
string swhentostop = whentostop.ToString();
string length = Console.ReadLine();
int whentostop = 0;
string slength = length.ToString();
string swhentostop = whentostop.ToString();
it makes it more obvious there's a redundant line there second thing is you can convert a string to an integer with int.Parse or int.TryParse
Elmishh
ElmishhOP2y ago
like 1: outputs "enter wanted length" so the user knows they can and should input like 2: asks user for input and names it length like 3: makes a variable that is later used to stop a while (true) when it reaches the same amount as length like 4: converts length input into a string named slength like 5: oh... yeah i think a problem is trying to add 1 to a string gimme a minute
Jimmacle
Jimmacle2y ago
as for 3, you never actually use that variable to try to stop the loop generally for while loops you want to have a condition for it to stop instead of just while(true)
Elmishh
ElmishhOP2y ago
yeah i guess that would work too gimme a minute to clean up some things
Jimmacle
Jimmacle2y ago
this is also a problem - you're adding 1 to the integer variable but that won't automatically update the one where you converted it to a string
Elmishh
ElmishhOP2y ago
aha it works now
using System;
static class RandomLetter
{
static Random _random = new Random();
public static char GetLetter()
{
// This method returns a random lowercase letter.
// ... Between 'a' and 'z' inclusive.
int num = _random.Next(0, 25); // Zero to 25
char let = (char)('a' + num);
return let;
}
}

class passwordgenerator
{
public static void Main()
{
System.Random random = new System.Random();

Console.WriteLine("enter wanted length:");
var length = Console.ReadLine();
var whentostop = 0;
var ilength = int.Parse(length);

var password = "";

while (true)
{
var letter = RandomLetter.GetLetter();
var number = random.Next(0, 9);
var NoL = random.Next(1, 4);

if (NoL <= 2)
{
password += number;
whentostop++;
}
if (NoL >= 3)
{
password += letter;
whentostop++;
}
if (whentostop == ilength)
{
Console.WriteLine($"password generated is {password}");
break;
}
}
}
}
using System;
static class RandomLetter
{
static Random _random = new Random();
public static char GetLetter()
{
// This method returns a random lowercase letter.
// ... Between 'a' and 'z' inclusive.
int num = _random.Next(0, 25); // Zero to 25
char let = (char)('a' + num);
return let;
}
}

class passwordgenerator
{
public static void Main()
{
System.Random random = new System.Random();

Console.WriteLine("enter wanted length:");
var length = Console.ReadLine();
var whentostop = 0;
var ilength = int.Parse(length);

var password = "";

while (true)
{
var letter = RandomLetter.GetLetter();
var number = random.Next(0, 9);
var NoL = random.Next(1, 4);

if (NoL <= 2)
{
password += number;
whentostop++;
}
if (NoL >= 3)
{
password += letter;
whentostop++;
}
if (whentostop == ilength)
{
Console.WriteLine($"password generated is {password}");
break;
}
}
}
}
Jimmacle
Jimmacle2y ago
one other thing you should consider changing is avoiding the while (true) because it makes it less obvious what you're trying to do you could do something like while (whentoStop < ilength) then put the writeline after the loop instead of inside it
Elmishh
ElmishhOP2y ago
i want to get 1 line thats why it prints once and then breaks
Jimmacle
Jimmacle2y ago
the change i suggested would have the exact same behavior just structured in a more readable way
Elmishh
ElmishhOP2y ago
i can yeah
Jimmacle
Jimmacle2y ago
you don't have to, but it's what i would do
Elmishh
ElmishhOP2y ago
ok ill keep that in mind next time i think of something at least i learnt how to get user input
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server