❔ need help with homework (while loop)
hey guys, I am a novice programmer
I currently go to school for an MSIS degree
I am learning c#
I have a homework project that wants me to create a loop for a vending machine and i got everything to work (according to what the homework asked of me), but when it comes to calculate the total cost it only calculates the cost of the last thing i ordered.
can anyone help me with this problem? thank you in advanced.
the power point pics explain the home work.
I also posted my code. my code works for the loop but it just does not add the total money of all my orders only the last order.
32 Replies
Well, you never add to
totalmoney
You always set it to the new valuewhat do you mean by that ?
totalmoney = amounts * unitprice;
is an assignment
not an additionokay i see that, but now how do i implement it to add to my total money ?
Angius#1586
REPL Result: Success
Result: int
Compile: 381.034ms | Execution: 28.248ms | React with ❌ to remove this embed.
change the
=
to +=
Angius#1586
REPL Result: Success
Result: int
Compile: 412.778ms | Execution: 52.726ms | React with ❌ to remove this embed.
my teacher never tought me what += was lol
its just a shortcut for
x = x + newValue
Equivalent to
x = x + right
yes
a = a + b
===> a += b
ok thank you i will add the changes and run my code
bro this took me 3 hours no joke
@Poggies it still did not work
Post your new updated code
Needs to be applied everywhere
also in the begging? sorry guys i am a noob lol
You also need to add the amount if it's not all
If bagel is not y because the user wants something else, it skips the addition
so make an else if?
Or just add it outside the if
no, move the
totalmoney += ...
outside the if
it shouldnt be conditionalOnly have the if for exiting the loop
put it under the double unitprice?
That's a good place yes, before asking it there's more to order
It also needs to be under unitprice since the addition uses that variable
Thank you so much you guys fr
it works now
np
it took me sooo long to figure this out lol
Also next time $code it just makes it easier to read with syntax highlithing
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/Missing the cs on the first line
okay
thank you @here
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.