C
C#17mo 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
Angius17mo ago
Well, you never add to totalmoney You always set it to the new value
eazy35
eazy3517mo ago
what do you mean by that ?
Pobiega
Pobiega17mo ago
totalmoney = amounts * unitprice; is an assignment not an addition
eazy35
eazy3517mo ago
okay i see that, but now how do i implement it to add to my total money ?
MODiX
MODiX17mo 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
Pobiega17mo ago
change the = to +=
MODiX
MODiX17mo 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
eazy3517mo ago
my teacher never tought me what += was lol
Pobiega
Pobiega17mo ago
its just a shortcut for x = x + newValue
TheBoxyBear
TheBoxyBear17mo ago
Equivalent to x = x + right
Pobiega
Pobiega17mo ago
yes
Angius
Angius17mo ago
a = a + b ===> a += b
eazy35
eazy3517mo 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
Pobiega17mo ago
Post your new updated code
TheBoxyBear
TheBoxyBear17mo ago
Needs to be applied everywhere
eazy35
eazy3517mo ago
eazy35
eazy3517mo ago
also in the begging? sorry guys i am a noob lol
TheBoxyBear
TheBoxyBear17mo 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
eazy3517mo ago
so make an else if?
TheBoxyBear
TheBoxyBear17mo ago
Or just add it outside the if
Pobiega
Pobiega17mo ago
no, move the totalmoney += ... outside the if it shouldnt be conditional
TheBoxyBear
TheBoxyBear17mo ago
Only have the if for exiting the loop
eazy35
eazy3517mo ago
put it under the double unitprice?
TheBoxyBear
TheBoxyBear17mo 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
eazy3517mo ago
Thank you so much you guys fr it works now
TheBoxyBear
TheBoxyBear17mo ago
np
eazy35
eazy3517mo ago
it took me sooo long to figure this out lol
TheBoxyBear
TheBoxyBear17mo ago
Also next time $code it just makes it easier to read with syntax highlithing
MODiX
MODiX17mo 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
TheBoxyBear17mo ago
Missing the cs on the first line
eazy35
eazy3517mo ago
okay thank you @here
Accord
Accord17mo 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
More Posts
❔ Recursion Method Psuedocodehow do i recursively sort both sublists at the bottom? in pseudocode they are written left:=MergeSo✅ Code ReviewI'm a little worried I am not following SOLID and best practices to the best ability: https://www.re❔ How to build a function that return TextFragmentCollection based on PDF file url?I already have a searchable pdf, but I want to convert it into pdf with digital text...I already got❔ Entity Framework 7 - Where to call EnsureCreared methodI'm trying to create a PGSQL database with a code-first approach using EF7 in my ASP.NET Core Web AP❔ C++ HelpThe code is: ```cpp vector<string> s_split(string Str,string Seperator) { vector<string> SplitSt❔ Need help with this part of a programming assignmenthello i am brand new to c# programming and i am stuck on this part of psuedocode and was wondering i✅ Make process believe it's a startup app?Hello! So I'm building an app that starts another app. The app in question seems to behave differen❔ Board Game (Marvel Champions) Gameplay Simulator - deep clone is very inefficientI have coded a program that simulates X games of Marvel Champions (a living card game by FFG). It wo❔ Problem of collections blocking when they shouldn't (BlockingCollection/ConcurrentQueue)Hello there, I have a problem with the producer-consumer patron in an application in net 6 (console❔ How do i run a c# programHello I wrote a new program in .txt file, how do i run this? It goes: Console WriteLine ()"Hello Wor