❔ Making a Vending Machine for Class!

Hey ya'll, I'm new here! I've been having some trouble with this assignment and I was hoping I could get some help. Below is a snippet of my code so far. Below is a snippet of my code so far. Basically, I got the vending loop portion down, but I can't seem to figure out how to get the program to maintain the number of each item and then provide the total when the user ends the program. { Console.WriteLine(" OSU Snacks menu"); Console.WriteLine("Select 1 for Soda --> price = $" + vendingOne); Console.WriteLine("Select 2 for Cookie --> price = $" + vendingTwo); Console.WriteLine("Select 3 for Chips --> price = $" + vendingThree); Console.WriteLine(); string userInput = Console.ReadLine(); double numberSelect = Convert.ToDouble(userInput); if (numberSelect == 1) { Console.WriteLine("You chose soda. How many do you want?"); string quantitySelect = Console.ReadLine(); double qsoda = Convert.ToDouble(quantitySelect); sodaQuantity += (int)qsoda; Console.WriteLine("Press y if that is all for today"); string yesNo = Console.ReadLine(); if (yesNo == "n") continue;
if (yesNo == "y") { Console.WriteLine("Your total is $" + sodaQuantity*vendingOne); Console.WriteLine(); break; }
18 Replies
xdd
xdd2y ago
hi, i think a can help u here so, start with putting code -
xdd
xdd2y ago
wiil be more readable as i get, u need program which works till u press "Y" ?
Adrian Marquez Trevizo
Yes. How do I do what you did? Give me one sec
xdd
xdd2y ago
so like, u started it, when u pick option, it asks to pick option again, but if u press Y, it calculates all prices and maybe prints it
tacosontitan
tacosontitan2y ago
$code
MODiX
MODiX2y ago
Posting Code Snippets To post a code snippet type the following: ```cs // code here ``` Notes: - Get an example by typing $codegif in the chat. - Change the language by replacing cs with the language of your choice (for example sql or cpp). - If your code is too long, you can post it to https://paste.mod.gg/ and share the link.
xdd
xdd2y ago
so, i see bit from txt file, there is a bit different logic, how do u solve it, depends on what specifically u want to do
tacosontitan
tacosontitan2y ago
Have you learned about methods yet?
Adrian Marquez Trevizo
namespace Homework5
{
internal class Program
{
static void Main(string[] args)
{
double vendingOne = 1;
double vendingTwo = 2;
double vendingThree = 1.5;
double sodaQuantity = 0;
double cookieQuantity = 0;
double chipQuantity = 0;

double totalOne = (sodaQuantity*vendingOne);
double totalTwo = (cookieQuantity*vendingTwo);
double totalThree = (chipQuantity*vendingThree);

while (true)
{
Console.WriteLine(" OSU Snacks menu");
Console.WriteLine("Select 1 for Soda --> price = $" + vendingOne);
Console.WriteLine("Select 2 for Cookie --> price = $" + vendingTwo);
Console.WriteLine("Select 3 for Chips --> price = $" + vendingThree);
Console.WriteLine();
string userInput = Console.ReadLine();
double numberSelect = Convert.ToDouble(userInput);

if (numberSelect == 1)
{
Console.WriteLine("You chose soda. How many do you want?");
string quantitySelect = Console.ReadLine();
double qsoda = Convert.ToDouble(quantitySelect);
sodaQuantity += (int)qsoda;
Console.WriteLine("Press y if that is all for today");
string yesNo = Console.ReadLine();
if (yesNo == "n")

continue;

if (yesNo == "y")
{
Console.WriteLine("Your total is $" + sodaQuantity*vendingOne);
Console.WriteLine();
break;
}
namespace Homework5
{
internal class Program
{
static void Main(string[] args)
{
double vendingOne = 1;
double vendingTwo = 2;
double vendingThree = 1.5;
double sodaQuantity = 0;
double cookieQuantity = 0;
double chipQuantity = 0;

double totalOne = (sodaQuantity*vendingOne);
double totalTwo = (cookieQuantity*vendingTwo);
double totalThree = (chipQuantity*vendingThree);

while (true)
{
Console.WriteLine(" OSU Snacks menu");
Console.WriteLine("Select 1 for Soda --> price = $" + vendingOne);
Console.WriteLine("Select 2 for Cookie --> price = $" + vendingTwo);
Console.WriteLine("Select 3 for Chips --> price = $" + vendingThree);
Console.WriteLine();
string userInput = Console.ReadLine();
double numberSelect = Convert.ToDouble(userInput);

if (numberSelect == 1)
{
Console.WriteLine("You chose soda. How many do you want?");
string quantitySelect = Console.ReadLine();
double qsoda = Convert.ToDouble(quantitySelect);
sodaQuantity += (int)qsoda;
Console.WriteLine("Press y if that is all for today");
string yesNo = Console.ReadLine();
if (yesNo == "n")

continue;

if (yesNo == "y")
{
Console.WriteLine("Your total is $" + sodaQuantity*vendingOne);
Console.WriteLine();
break;
}
tacosontitan
tacosontitan2y ago
Also, my inference from your problem statement is that the user should be capable of selecting more than one item and calculate a total at the end, is that correct?
Adrian Marquez Trevizo
Yeah! That's what I'm having trouble with
tacosontitan
tacosontitan2y ago
Okay So Your totals shouldn't be at the top They should be after your loop is done
double quanity = 0;
double price = 1.325;
while (true) {
quantity++;
double currentTotal = quantity * price;
Console.WriteLine("Added a soda, you now have " + quantity + " for a total of $" + currentTotal + ". ");
Console.Write("Send 's' to stop. ");
string response = Console.ReadLine();
if (response == "s")
break;
}

double total = quantity * price;
Console.WriteLine("Thanks! That'll be $" + total + ".");
double quanity = 0;
double price = 1.325;
while (true) {
quantity++;
double currentTotal = quantity * price;
Console.WriteLine("Added a soda, you now have " + quantity + " for a total of $" + currentTotal + ". ");
Console.Write("Send 's' to stop. ");
string response = Console.ReadLine();
if (response == "s")
break;
}

double total = quantity * price;
Console.WriteLine("Thanks! That'll be $" + total + ".");
Calculate the current total on the fly with each iteration and save the final calculation for after the fact. Does that help?
Adrian Marquez Trevizo
I think so, I'm looking at my code and this now, thank you!
xdd
xdd2y ago
also, if u leaned switch before, use it instead of multiple if statements with same logic so, there might be some things u don't know yet, but i think ull get it, happy to explain if need something to clarify @Aquaholic
static void Main(string[] args)
{
const decimal SODA_PRICE = 1, COOKIE_PRICE = 2, CHIPS_PRICE = 1.5M;
int sodaSold = 0, cookieSold = 0, chipsSold = 0;

bool pepsi = true;
while (pepsi)
{
Console.Clear();
Console.WriteLine($"OSU Snacks menu\n" +
$"Select 1 for Soda --> price = ${SODA_PRICE}\n" +
$"Select 2 for Soda --> price = ${COOKIE_PRICE}\n" +
$"Select 3 for Soda --> price = ${CHIPS_PRICE}\n" +
$"Or press Y to exit");

string? userInput = Console.ReadLine();

switch (userInput)
{
case "1":
sodaSold++;
break;
case "2":
cookieSold++;
break;
case "3":
chipsSold++;
break;
case "Y":
pepsi = false;
break;
}
}

decimal totalSum = (sodaSold * SODA_PRICE) + (cookieSold * COOKIE_PRICE) + (chipsSold * CHIPS_PRICE);
Console.Clear();
Console.WriteLine($"Total is: ${totalSum}");
}
static void Main(string[] args)
{
const decimal SODA_PRICE = 1, COOKIE_PRICE = 2, CHIPS_PRICE = 1.5M;
int sodaSold = 0, cookieSold = 0, chipsSold = 0;

bool pepsi = true;
while (pepsi)
{
Console.Clear();
Console.WriteLine($"OSU Snacks menu\n" +
$"Select 1 for Soda --> price = ${SODA_PRICE}\n" +
$"Select 2 for Soda --> price = ${COOKIE_PRICE}\n" +
$"Select 3 for Soda --> price = ${CHIPS_PRICE}\n" +
$"Or press Y to exit");

string? userInput = Console.ReadLine();

switch (userInput)
{
case "1":
sodaSold++;
break;
case "2":
cookieSold++;
break;
case "3":
chipsSold++;
break;
case "Y":
pepsi = false;
break;
}
}

decimal totalSum = (sodaSold * SODA_PRICE) + (cookieSold * COOKIE_PRICE) + (chipsSold * CHIPS_PRICE);
Console.Clear();
Console.WriteLine($"Total is: ${totalSum}");
}
Adrian Marquez Trevizo
I'm still learning quite a bit! We've really only had 5 modules of into to work with! I really do appreciate your help 🙂
tacosontitan
tacosontitan2y ago
I'd stick with what you know and focus on the logic then as that's like the intent of your assignment 🙂 Best of luck!
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.

Did you find this page helpful?