C
C#•2mo 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•2mo 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•2mo ago
yeah that formatting is wild
Moha
MohaOP•2mo ago
I'm on mobile, I just started coding yesterday..
leowest
leowest•2mo ago
fair then if u would format the file and reupload that would be helpful for us to help you
Moha
MohaOP•2mo ago
Yeah sure one sec
Moha
MohaOP•2mo 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•2mo ago
yeah it looks like you've already written a couple while loops. could you clarify what you're asking?
Moha
MohaOP•2mo ago
I basically want to start the questions again In the top top And the same proccess
mg
mg•2mo 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•2mo ago
much better yes! thanks
Moha
MohaOP•2mo ago
Yea but how And I want the loop to be looped Until the user says enough
mg
mg•2mo ago
Let's start with that. How are you determining when the user says enough?
taner.
taner.•2mo ago
put the logic in a while loop
Moha
MohaOP•2mo ago
I'd put a console.readline string in a
if
if
and just
break
break
, isn't that how it works? Oops
taner.
taner.•2mo ago
you already ask the user for yes or no. like while(choice == "yes"){....}
Moha
MohaOP•2mo ago
Why's it like that
mg
mg•2mo ago
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•2mo ago
Yeah I put the while there but I don't know what to loop here Where to start and end
mg
mg•2mo ago
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•2mo ago
A while loop doesn't work? I only understand while loops
mg
mg•2mo ago
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•2mo ago
What if I want more than once, also what does while false represent, what if I put true
mg
mg•2mo ago
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•2mo ago
So if I put true it will run forever
mg
mg•2mo ago
yep
Moha
MohaOP•2mo ago
Ok now that I uh Where do I start the loop what marks the end The while ()?
mg
mg•2mo ago
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•2mo ago
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•2mo ago
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•2mo ago
String choice; ?
mg
mg•2mo ago
yep, but use lowercase string
Moha
MohaOP•2mo ago
And where do I put the value
mg
mg•2mo ago
not a functional difference, it's just conventional C# before the loop, should choice even have a value?
Moha
MohaOP•2mo ago
choice is a readline
mg
mg•2mo ago
it's a variable you're assigning the output of Console.ReadLine().ToLower() to it
Moha
MohaOP•2mo ago
Yes
mg
mg•2mo ago
but it's just a variable. it can remain uninitialized so before the loop, should it have a value?
Moha
MohaOP•2mo ago
No
mg
mg•2mo ago
right
Moha
MohaOP•2mo ago
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•2mo ago
yep
Moha
MohaOP•2mo ago
So I would uhh Put it in line 74 whatever
mg
mg•2mo ago
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•2mo ago
Goes true Also uhh
mg
mg•2mo ago
do you want it to keep going forever?
Moha
MohaOP•2mo ago
What if I want the loop to be in a if Until the user commands it to stop Line 73
mg
mg•2mo ago
so we definitely don't want true in the parentheses, otherwise it'll just keep going forever
Moha
MohaOP•2mo ago
But false makes it go only once right?
mg
mg•2mo ago
there are infinitely many options other than true and false
Moha
MohaOP•2mo ago
Or do u mean once every run
mg
mg•2mo ago
you've got the logic there already, if (choice == "yes" || choice == "yes.")
Moha
MohaOP•2mo ago
Or once every time the user wants to keep going
mg
mg•2mo ago
so choice == "yes" || choice == "yes." is what goes in the parentheses of the do/while
Moha
MohaOP•2mo ago
Ok I'm dumb
mg
mg•2mo ago
that's a regular feeling in this line of work
Moha
MohaOP•2mo ago
Ok now time to do it How did the do command work Do{} ? @mg
mg
mg•2mo ago
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•2mo ago
Oh I put the while outside the do Why is it giving me this error?
Moha
MohaOP•2mo ago
No description
leowest
leowest•2mo ago
if u read the error it tells u what is missing there
leowest
leowest•2mo ago
No description
Moha
MohaOP•2mo ago
I filled the errors and it gave me more errors 🥲
leowest
leowest•2mo ago
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•2mo ago
I filled a } before a ; So it became like ;} On another error I messed up something
leowest
leowest•2mo ago
can you show the code?
Moha
MohaOP•2mo ago
I CANT FIND IT
Moha
MohaOP•2mo ago
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•2mo ago
In most cases this will be an error ;}
leowest
leowest•2mo ago
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•2mo ago
Yeah It looks so wrong
leowest
leowest•2mo ago
yes, its what u have in your code.
Moha
MohaOP•2mo ago
On second
mg
mg•2mo ago
I would forget about the "Please say yes or no" part for now
Moha
MohaOP•2mo ago
What the hell is that bracket doing
leowest
leowest•2mo ago
you probably accidently added it once u broke the while
mg
mg•2mo ago
just focus on getting the do/while working and you can re-add that part later
leowest
leowest•2mo ago
so you need to fix it by removing the ; and fixing the brackets
Angius
Angius•2mo ago
No description
Moha
MohaOP•2mo ago
I FIXED IT YES the brackets were not positioned well
leowest
leowest•2mo ago
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•2mo ago
Yep
leowest
leowest•2mo ago
so u know they are opening and ending at the right place
Moha
MohaOP•2mo ago
Im using c# shell Mobile app Ok now something went wrong No errors though dawg the app stop wroking
leowest
leowest•2mo ago
No description
Angius
Angius•2mo ago
Writing code on mobile has to be an... experience
Moha
MohaOP•2mo ago
I don't have a pc vruh
glhays
glhays•2mo ago
Remove the space a before the semi-colon while (choice != "yes" && choice != "yes." && choice != "no" && choice != "no.") ;
Moha
MohaOP•2mo ago
Alright
Angius
Angius•2mo ago
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•2mo ago
I actually used while commands before without semicolons and they worked well.. LOL
mg
mg•2mo ago
while loops don't need a semicolon at the end, but do/whiles do
Moha
MohaOP•2mo ago
Ok my dumb ahh went straight to put semicolons after whiles
Angius
Angius•2mo ago
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•2mo ago
CHAT MY CODE IS DONE IT WORKS
Angius
Angius•2mo ago
🎉
Moha
MohaOP•2mo ago
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•2mo ago
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•2mo ago
No description
Moha
MohaOP•2mo ago
yes
mg
mg•2mo ago
-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?