C
C#•2d ago
Moha

Bank code

https://paste.mod.gg/jkuiernhqqgp/0 How can I use a while loop here?
BlazeBin - jkuiernhqqgp
A tool for sharing your source code with the world!
100 Replies
leowest
leowest•2d ago
not sure if you're trolling or legitimate asking a question with your code indented like that sorry can you resubmit the file so its readable at least? CTRL+K+D if you're using visual studio to format your code
Jimmacle
Jimmacle•2d ago
yeah that formatting is wild
Moha
MohaOP•2d ago
I'm on mobile, I just started coding yesterday..
leowest
leowest•2d ago
fair then if u would format the file and reupload that would be helpful for us to help you
Moha
MohaOP•2d ago
Yeah sure one sec
Moha
MohaOP•2d ago
Pastebin
using System;class Bank{ static void Main() { Console....
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
mg
mg•2d ago
yeah it looks like you've already written a couple while loops. could you clarify what you're asking?
Moha
MohaOP•2d ago
I basically want to start the questions again In the top top And the same proccess
mg
mg•2d ago
In that case, you'd want all the logic of asking the questions and getting answers to be in the body of a while loop
leowest
leowest•2d ago
much better yes! thanks
Moha
MohaOP•2d ago
Yea but how And I want the loop to be looped Until the user says enough
mg
mg•2d ago
Let's start with that. How are you determining when the user says enough?
taner.
taner.•2d ago
put the logic in a while loop
Moha
MohaOP•this hour
I'd put a console.readline string in a
if
if
and just
break
break
, isn't that how it works? Oops
taner.
taner.•this hour
you already ask the user for yes or no. like while(choice == "yes"){....}
Moha
MohaOP•this hour
Why's it like that
mg
mg•this hour
well right now you're using the choice variable, right? you read a string into that variable and check if it's one of a few possible responses
Moha
MohaOP•this hour
Yeah I put the while there but I don't know what to loop here Where to start and end
mg
mg•this hour
in this case, you always want to ask the questions at least once, so you're actually going to want a do while loop instead of a while loop
Moha
MohaOP•this hour
A while loop doesn't work? I only understand while loops
mg
mg•this hour
a do/while is very similar it just always runs at least once e.g.
while (false) { Console.WriteLine("foo"); } // does nothing
do { Console.WriteLine("foo"); } while (false) // writes "foo" to the console once
while (false) { Console.WriteLine("foo"); } // does nothing
do { Console.WriteLine("foo"); } while (false) // writes "foo" to the console once
Moha
MohaOP•this hour
What if I want more than once, also what does while false represent, what if I put true
mg
mg•this hour
what's inside the parentheses is the condition for the while loop: what determines if it'll run again i just used false as an example both will run as long as the condition is true it's just that while checks the condition before running the first time, and do/while doesn't so with do/while, the code inside always runs at least once whereas with while, it may not run at all
Moha
MohaOP•this hour
So if I put true it will run forever
mg
mg•this hour
yep
Moha
MohaOP•this hour
Ok now that I uh Where do I start the loop what marks the end The while ()?
mg
mg•this hour
while (choice != "yes" && choice != "yes." && choice != "no" && choice != "no.")
{
Console.WriteLine("Please say yes or no!");
choice = Console.ReadLine().ToLower();
}
while (choice != "yes" && choice != "yes." && choice != "no" && choice != "no.")
{
Console.WriteLine("Please say yes or no!");
choice = Console.ReadLine().ToLower();
}
this one?
Moha
MohaOP•this hour
No I want to loop the whole code over and over again until a break happens The "do" marks the beginning of the loop and "while()" marks the end?
mg
mg•this hour
correct but you'll need some code before do specifically you'll need to declare the variable that holds the user's answer
Moha
MohaOP•this hour
String choice; ?
mg
mg•this hour
yep, but use lowercase string
Moha
MohaOP•this hour
And where do I put the value
mg
mg•this hour
not a functional difference, it's just conventional C# before the loop, should choice even have a value?
Moha
MohaOP•this hour
choice is a readline
mg
mg•this hour
it's a variable you're assigning the output of Console.ReadLine().ToLower() to it
Moha
MohaOP•this hour
Yes
mg
mg•this hour
but it's just a variable. it can remain uninitialized so before the loop, should it have a value?
Moha
MohaOP•this hour
No
mg
mg•this hour
right
Moha
MohaOP•this hour
When I declare the thing assigned It should be at that time when I ask if the user wants to continue doing actions Am I correct
mg
mg•this hour
yep
Moha
MohaOP•this hour
So I would uhh Put it in line 74 whatever
mg
mg•this hour
so your code will look like
string choice;
do
{
// ask about withdrawing, depositing, etc.
// do the withdrawal, deposit, etc.
// ask if the user wants to go again, so now choice has a value
}
while (/* what goes here? */);
string choice;
do
{
// ask about withdrawing, depositing, etc.
// do the withdrawal, deposit, etc.
// ask if the user wants to go again, so now choice has a value
}
while (/* what goes here? */);
Moha
MohaOP•this hour
Goes true Also uhh
mg
mg•this hour
do you want it to keep going forever?
Moha
MohaOP•this hour
What if I want the loop to be in a if Until the user commands it to stop Line 73
mg
mg•this hour
so we definitely don't want true in the parentheses, otherwise it'll just keep going forever
Moha
MohaOP•this hour
But false makes it go only once right?
mg
mg•this hour
there are infinitely many options other than true and false
Moha
MohaOP•this hour
Or do u mean once every run
mg
mg•this hour
you've got the logic there already, if (choice == "yes" || choice == "yes.")
Moha
MohaOP•this hour
Or once every time the user wants to keep going
mg
mg•this hour
so choice == "yes" || choice == "yes." is what goes in the parentheses of the do/while
Moha
MohaOP•this hour
Ok I'm dumb
mg
mg•this hour
that's a regular feeling in this line of work
Moha
MohaOP•this hour
Ok now time to do it How did the do command work Do{} ? @mg
mg
mg•this hour
Iteration statements -for, foreach, do, and while - C# reference
C# iteration statements (for, foreach, do, and while) repeatedly execute a block of code. You use those statements to create loops or iterate through a collection.
Moha
MohaOP•this hour
Oh I put the while outside the do Why is it giving me this error?
Moha
MohaOP•this hour
No description
leowest
leowest•this hour
if u read the error it tells u what is missing there
leowest
leowest•this hour
No description
Moha
MohaOP•this hour
I filled the errors and it gave me more errors 🥲
leowest
leowest•this hour
im not sure what filled means can u provide code? if you look at the link above, a do while is written like this
do
{
// body
} while (condition);
do
{
// body
} while (condition);
you need to close it with the semicolon
Moha
MohaOP•this hour
I filled a } before a ; So it became like ;} On another error I messed up something
leowest
leowest•this hour
can you show the code?
Moha
MohaOP•this hour
I CANT FIND IT
Moha
MohaOP•this hour
Pastebin
using System;class Bank{ static void Main() { string -...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
glhays
glhays•this hour
In most cases this will be an error ;}
leowest
leowest•this hour
while (choice != "yes" && choice != "yes." && choice != "no" && choice != "no.") ;

} {
Console.WriteLine("Please say yes or no!");
choice = Console.ReadLine().ToLower();
}
while (choice != "yes" && choice != "yes." && choice != "no" && choice != "no.") ;

} {
Console.WriteLine("Please say yes or no!");
choice = Console.ReadLine().ToLower();
}
I guess the problem is around here? by adding a ; after the () in the while u essentially terminated the while then u have broken brackets }{} its important that every time u open a bracket u know there is a pairing bracket for it AKA
{
}
{
}
Moha
MohaOP•this hour
Yeah It looks so wrong
leowest
leowest•this hour
yes, its what u have in your code.
Moha
MohaOP•this hour
On second
mg
mg•this hour
I would forget about the "Please say yes or no" part for now
Moha
MohaOP•this hour
What the hell is that bracket doing
leowest
leowest•this hour
you probably accidently added it once u broke the while
mg
mg•this hour
just focus on getting the do/while working and you can re-add that part later
leowest
leowest•this hour
so you need to fix it by removing the ; and fixing the brackets
Angius
Angius•this hour
No description
Moha
MohaOP•this hour
I FIXED IT YES the brackets were not positioned well
leowest
leowest•this hour
understanding your own code is 10 steps fowards 🙂 if you're using visual studio you can also hover your mouse over the bracket and it shows u what is its other side of the bracket
Moha
MohaOP•this hour
Yep
leowest
leowest•this hour
so u know they are opening and ending at the right place
Moha
MohaOP•this hour
Im using c# shell Mobile app Ok now something went wrong No errors though dawg the app stop wroking
leowest
leowest•this hour
No description
Angius
Angius•this hour
Writing code on mobile has to be an... experience
Moha
MohaOP•this hour
I don't have a pc vruh
glhays
glhays•this hour
Remove the space a before the semi-colon while (choice != "yes" && choice != "yes." && choice != "no" && choice != "no.") ;
Moha
MohaOP•this hour
Alright
Angius
Angius•this hour
I can recommend getting a cheap Bluetooth keyboard if your phone supports it. That's how I did my statistics exam in R in college, since I didn't have a laptop :KEKW:
Moha
MohaOP•this hour
I actually used while commands before without semicolons and they worked well.. LOL
mg
mg•this hour
while loops don't need a semicolon at the end, but do/whiles do
Moha
MohaOP•this hour
Ok my dumb ahh went straight to put semicolons after whiles
Angius
Angius•this hour
Generally, every control flow statement (if, for, foreach, while, switch, (arguably using as well)) is structured like so:
keyword (stuff)
{
code
}
keyword (stuff)
{
code
}
One exception being do..while
do {
code
} while (stuff);
do {
code
} while (stuff);
Moha
MohaOP•this hour
CHAT MY CODE IS DONE IT WORKS
Angius
Angius•this hour
🎉
Moha
MohaOP•this hour
Pastebin
using System;class Bank{ static void Main() { string -...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Moha
MohaOP•this hour
RAHHHHHHHHHHHTR It's the hardest code I did so far Thanks to all who helped Appreciated fr They say don't celebrate too early chat Now -600+1000 is 400 but
Moha
MohaOP•this hour
No description
Moha
MohaOP•this hour
yes
mg
mg•this hour
-600 + 1000 is indeed 400, so your program must not be computing -600 + 1000. it must be computing something else not sure if whatever mobile IDE you're using supports breakpoints but at worst you can Console.WriteLine() to see what the value of different things are at different points in your code's execution

Did you find this page helpful?