C
C#2y ago
eazy35

❔ 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
Angius
Angius2y ago
Well, you never add to totalmoney You always set it to the new value
eazy35
eazy35OP2y ago
what do you mean by that ?
Pobiega
Pobiega2y ago
totalmoney = amounts * unitprice; is an assignment not an addition
eazy35
eazy35OP2y ago
okay i see that, but now how do i implement it to add to my total money ?
MODiX
MODiX2y ago
Angius#1586
REPL Result: Success
int x = 0;
x = 8;
x = 754768;
x = 224;
x
int x = 0;
x = 8;
x = 754768;
x = 224;
x
Result: int
224
224
Compile: 381.034ms | Execution: 28.248ms | React with ❌ to remove this embed.
Pobiega
Pobiega2y ago
change the = to +=
MODiX
MODiX2y ago
Angius#1586
REPL Result: Success
int x = 0;
x += 8;
x += 754768;
x += 224;
x
int x = 0;
x += 8;
x += 754768;
x += 224;
x
Result: int
755000
755000
Compile: 412.778ms | Execution: 52.726ms | React with ❌ to remove this embed.
eazy35
eazy35OP2y ago
my teacher never tought me what += was lol
Pobiega
Pobiega2y ago
its just a shortcut for x = x + newValue
TheBoxyBear
TheBoxyBear2y ago
Equivalent to x = x + right
Pobiega
Pobiega2y ago
yes
Angius
Angius2y ago
a = a + b ===> a += b
eazy35
eazy35OP2y ago
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
Pobiega
Pobiega2y ago
Post your new updated code
TheBoxyBear
TheBoxyBear2y ago
Needs to be applied everywhere
eazy35
eazy35OP2y ago
eazy35
eazy35OP2y ago
also in the begging? sorry guys i am a noob lol
TheBoxyBear
TheBoxyBear2y ago
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
eazy35
eazy35OP2y ago
so make an else if?
TheBoxyBear
TheBoxyBear2y ago
Or just add it outside the if
Pobiega
Pobiega2y ago
no, move the totalmoney += ... outside the if it shouldnt be conditional
TheBoxyBear
TheBoxyBear2y ago
Only have the if for exiting the loop
eazy35
eazy35OP2y ago
put it under the double unitprice?
TheBoxyBear
TheBoxyBear2y ago
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
eazy35
eazy35OP2y ago
Thank you so much you guys fr it works now
TheBoxyBear
TheBoxyBear2y ago
np
eazy35
eazy35OP2y ago
it took me sooo long to figure this out lol
TheBoxyBear
TheBoxyBear2y ago
Also next time $code it just makes it easier to read with syntax highlithing
MODiX
MODiX2y 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/
TheBoxyBear
TheBoxyBear2y ago
Missing the cs on the first line
eazy35
eazy35OP2y ago
okay thank you @here
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server