C#
C#

help

Root Question Message

SAFE
SAFE11/30/2022
✅ while loop help(im new ish so its probs obvious)

'''cs
namespace HippityHop
{
internal class Program
{
static void Main(string[] args)
{


Console.WriteLine("** HIPPITY HOP **");
Console.Write("Please enter an integer between 1 and 100:");

// WRITE YOUR CODE BELOW THIS COMMENT
string input = Console.ReadLine();
int num = Convert.ToInt32(input);


while (num <= 100 || num >= 1)
{
Console.Write("Please enter an integer between 1 and 100:");
num++;
return;

}
for ( num = 1; num == num; num++ )
{
if(num % 3 ==0)
{
Console.WriteLine("Hip");
}
if (num % 5 == 0)
{
Console.WriteLine("Hop");
}
if (num % 5 == 0 && num % 3 == 0)
{
Console.WriteLine("Hop");
}
}
}
}
}
'''
SAFE
SAFE11/30/2022
basically i do my input and then after it does show the "please enter integer", but it wont let me type or show anything else and it does the press any key to close command prompt
SAFE
SAFE11/30/2022
i tried few things (thats why theres return and num++) but im sure i use return somehow idk lol
hiyosilver
hiyosilver11/30/2022
You have an || or in there.
hiyosilver
hiyosilver11/30/2022
Your condition is ALWAYS true
Ezlanding
Ezlanding11/30/2022
Use backtick (`), not apostrophe (') for markdown $code highlighting
SAFE
SAFE11/30/2022
so i use &&
hiyosilver
hiyosilver11/30/2022
Wait a sec, I think I might have misread
hiyosilver
hiyosilver11/30/2022
Please hold 😄
SAFE
SAFE11/30/2022
i think i might be something else in my code
SAFE
SAFE11/30/2022
i feel like the OR should work
Ero
Ero11/30/2022
The condition isn't always true
Ero
Ero11/30/2022
It just does the exact opposite
Ero
Ero11/30/2022
Of what you want it to do
SAFE
SAFE11/30/2022
i have to make it so it keeps prompting while input isnt between 1 and 100
SAFE
SAFE11/30/2022
thats what that is
Ero
Ero11/30/2022
That's not what that is
hiyosilver
hiyosilver11/30/2022
I think its both @542772576905199626 🙂 any number is either smaller than 100 or greater than 1
Ero
Ero11/30/2022
Ah, right
Ero
Ero11/30/2022
The code inside the while is also nonsensical
hiyosilver
hiyosilver11/30/2022
the for loop is weird and looks like its also endless
Ero
Ero11/30/2022
Absolutely
hiyosilver
hiyosilver11/30/2022
num == num is a strange condition to use @261229774788100096
Ero
Ero11/30/2022
You're overwriting the user's input
SAFE
SAFE11/30/2022
oh yes i was working on that too
hiyosilver
hiyosilver11/30/2022
So the problem as I see it
SAFE
SAFE11/30/2022
Once the user enters a valid integer, use a for loop to loop through the values between 1 and the
number that the user entered
hiyosilver
hiyosilver11/30/2022
You take the input, you check if its EITHER smalelr or equal to 100
SAFE
SAFE11/30/2022
thats what that is; or am trying to do
hiyosilver
hiyosilver11/30/2022
OR larger or equal to 1
hiyosilver
hiyosilver11/30/2022
which will always be true for any number
SAFE
SAFE11/30/2022
oooh
hiyosilver
hiyosilver11/30/2022
you increment it by 1, print somethign and then end the program
SAFE
SAFE11/30/2022
trueee
SAFE
SAFE11/30/2022
okok
SAFE
SAFE11/30/2022
im trying to make it not end
Ero
Ero11/30/2022
So don't use return
hiyosilver
hiyosilver11/30/2022
if you want to keep asking for a valid input, a while loop is a perfectly reasonable choice
Ero
Ero11/30/2022
Not just that, it's your best choice
hiyosilver
hiyosilver11/30/2022
you probably just want to repeate the ReadLine instead of the num++
hiyosilver
hiyosilver11/30/2022
And drop the return, because that just returns from your Main, thus ending program execution 😉
SAFE
SAFE11/30/2022
ooh okok
SAFE
SAFE11/30/2022
that helped
SAFE
SAFE11/30/2022
didnt end
hiyosilver
hiyosilver11/30/2022
You could (perhaps should) use int.TryParse instead
hiyosilver
hiyosilver11/30/2022
it returns a boolean depending on if the conversion was succesful, rather than just crashing when it doesnt work
hiyosilver
hiyosilver11/30/2022
@261229774788100096 Is it doing what you want or just not quitting early? 🙂
SAFE
SAFE11/30/2022
had to go do something just earlier thats why i answer now; it isnt quitting anymore but i havent quite figured out how to get what i want working
SAFE
SAFE11/30/2022
i think i just need to change what i need in the brackets in while ( ) and for ( )
SAFE
SAFE11/30/2022
like this lol
hiyosilver
hiyosilver11/30/2022
You could get valid input like this for example
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy