C
C#β€’2y ago
SAFE

βœ… 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"); } } } } } '''
60 Replies
SAFE
SAFEβ€’2y ago
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 i tried few things (thats why theres return and num++) but im sure i use return somehow idk lol
hiyosilver
hiyosilverβ€’2y ago
You have an || or in there. Your condition is ALWAYS true
Ezlanding
Ezlandingβ€’2y ago
Use backtick (`), not apostrophe (') for markdown $code highlighting
MODiX
MODiXβ€’2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
SAFE
SAFEβ€’2y ago
so i use &&
hiyosilver
hiyosilverβ€’2y ago
Wait a sec, I think I might have misread Please hold πŸ˜„
SAFE
SAFEβ€’2y ago
i think i might be something else in my code i feel like the OR should work
ero
eroβ€’2y ago
The condition isn't always true It just does the exact opposite Of what you want it to do
SAFE
SAFEβ€’2y ago
i have to make it so it keeps prompting while input isnt between 1 and 100 thats what that is
ero
eroβ€’2y ago
That's not what that is
hiyosilver
hiyosilverβ€’2y ago
I think its both @Ero πŸ™‚ any number is either smaller than 100 or greater than 1
ero
eroβ€’2y ago
Ah, right The code inside the while is also nonsensical
hiyosilver
hiyosilverβ€’2y ago
the for loop is weird and looks like its also endless
ero
eroβ€’2y ago
Absolutely
hiyosilver
hiyosilverβ€’2y ago
num == num is a strange condition to use @SAFE
ero
eroβ€’2y ago
You're overwriting the user's input
SAFE
SAFEβ€’2y ago
oh yes i was working on that too
hiyosilver
hiyosilverβ€’2y ago
So the problem as I see it
SAFE
SAFEβ€’2y ago
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
hiyosilverβ€’2y ago
You take the input, you check if its EITHER smalelr or equal to 100
SAFE
SAFEβ€’2y ago
thats what that is; or am trying to do
hiyosilver
hiyosilverβ€’2y ago
OR larger or equal to 1 which will always be true for any number
SAFE
SAFEβ€’2y ago
oooh
hiyosilver
hiyosilverβ€’2y ago
you increment it by 1, print somethign and then end the program
SAFE
SAFEβ€’2y ago
trueee okok im trying to make it not end
ero
eroβ€’2y ago
So don't use return
hiyosilver
hiyosilverβ€’2y ago
if you want to keep asking for a valid input, a while loop is a perfectly reasonable choice
ero
eroβ€’2y ago
Not just that, it's your best choice
hiyosilver
hiyosilverβ€’2y ago
you probably just want to repeate the ReadLine instead of the num++ And drop the return, because that just returns from your Main, thus ending program execution πŸ˜‰
SAFE
SAFEβ€’2y ago
ooh okok that helped didnt end
hiyosilver
hiyosilverβ€’2y ago
You could (perhaps should) use int.TryParse instead it returns a boolean depending on if the conversion was succesful, rather than just crashing when it doesnt work @SAFE Is it doing what you want or just not quitting early? πŸ™‚
SAFE
SAFEβ€’2y ago
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 i think i just need to change what i need in the brackets in while ( ) and for ( ) like this lol
hiyosilver
hiyosilverβ€’2y ago
You could get valid input like this for example
int number;
while(!int.TryParse(Console.ReadLine(), out number) || number < 1 || number > 100) {
Console.WriteLine("Please choose a valid number between 1 and 100");
}
int number;
while(!int.TryParse(Console.ReadLine(), out number) || number < 1 || number > 100) {
Console.WriteLine("Please choose a valid number between 1 and 100");
}
Ok, then what did you want again, let me double check πŸ˜„ So you had the general syntax for a for-loop correct, but using the input as the iterator variable is probably not what you want. Insteead do something like
for(int i = 1; i <= input; i++) {
//Do something for each number from 1 to input;
}
for(int i = 1; i <= input; i++) {
//Do something for each number from 1 to input;
}
You don't want to be modifying the original input by incrementing, like you are doing. Because then you immediately lose track of what it was and to which number you want to loop. That's why your original condition could never have worked.
SAFE
SAFEβ€’2y ago
<= cannot be applied to int and string 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; while (!int.TryParse(Console.ReadLine(), out num) num < 1 num > 100) { Console.WriteLine("Please choose a valid number between 1 and 100"); } { Console.Write("Please enter an integer between 1 and 100:"); Console.ReadLine(); } for (int i = 1; i <= input; i++) { if (i % 3 == 0) { Console.WriteLine("Hip"); Console.ReadLine(); } if (i % 5 == 0) { Console.WriteLine("Hop"); Console.ReadLine(); } if (i % 5 == 0 && num % 3 == 0) { Console.WriteLine("Hippity Hop"); Console.ReadLine(); } } } } }
hiyosilver
hiyosilverβ€’2y ago
Well, you were a bit too literal there πŸ˜‰ You copied my example, you have no variable called input however. I just picked the name randomly to illustrate the point You want to compare the iterator variable against your parsed input, num
SAFE
SAFEβ€’2y ago
ooh i was confused cause i had already soemthing named input
hiyosilver
hiyosilverβ€’2y ago
Happens, you also should remove the stuff between the extra brackets after the while loop. You are just taking extra input unnecessarily πŸ™‚ Also, do you want execution to pause after every print in your for-loop? It should give you the right output now, so you can just let it run in one go.
SAFE
SAFEβ€’2y ago
just all at once after i type
hiyosilver
hiyosilverβ€’2y ago
All good then?
SAFE
SAFEβ€’2y ago
close im still trying things
hiyosilver
hiyosilverβ€’2y ago
Well I'll be off, if anything else comes up, I'm sure you can find a helping hand around here somewhere.
SAFE
SAFEβ€’2y ago
i think i did get it actually just now yeah this place seems great for help so i almost got it, its just that it is not showing the non-multiples. its only shows the multiples(hip, hop, hippity hop)
hiyosilver
hiyosilverβ€’2y ago
In bed but still awake πŸ˜„ You are only testing for the multiples and discarding all others. You could append a final else and just print i in that case.
SAFE
SAFEβ€’2y ago
SAFE
SAFEβ€’2y ago
it should like this for 45 but it shows 33 lines which are only hip/hop/hippity hop and also theres defo not 33 multiples lol an else makes sense
hiyosilver
hiyosilverβ€’2y ago
The screenshot is what you want or what you have?
SAFE
SAFEβ€’2y ago
what it should look for for 45
hiyosilver
hiyosilverβ€’2y ago
You should als make your last two ifs into else ifs. The way it is now, 15 for example would make all 3 true, thus 3 prints
SAFE
SAFEβ€’2y ago
this is what it is for me
hiyosilver
hiyosilverβ€’2y ago
Yes, only hips and hops because of no final else to catch non-multiples. And too many because some numbers fulfill multiple conditions.
SAFE
SAFEβ€’2y ago
yeah im confused about that part so i think i should put two else ifs instead of if two else ifs under first if
Unknown User
Unknown Userβ€’2y ago
Message Not Public
Sign In & Join Server To View
hiyosilver
hiyosilverβ€’2y ago
Actually, you should test for mod 3 AND mod 5 first of all. Chain the other two with else ifs so only one of the three happens for any given number. Add a final else clause for any numbers that dont match any of the three.
SAFE
SAFEβ€’2y ago
how would i do nervousowo trying to figure out cant think of what would be good to put in it i++ maybe ill try hmm feel like its something with i++ since each line increments by 1 till the input number got it !! console.writeline i worked so theres no hops at all for 45.. when in the example when he types 45 he gets all three @Stalli help please if u want ? : )
Unknown User
Unknown Userβ€’2y ago
Message Not Public
Sign In & Join Server To View
SAFE
SAFEβ€’2y ago
ooh ive heard of that ill look at that so my assignment is fizzbuzz in disguise basically
hiyosilver
hiyosilverβ€’2y ago
Got it working?
SAFE
SAFEβ€’2y ago
yes it’s all good i think; i submitted it earlier i got one more little assignment due for next week and then final test on the 15th
cap5lut
cap5lutβ€’2y ago
please use the /close command to mark this thread as answered πŸ˜‰
Accord
Accordβ€’2y ago
Closed!
Want results from more Discord servers?
Add your server
More Posts