1

I have the following instructions for one of my labs.

program that will prompt the user for the weight and price of the produce, the price and capacity of a plastic bag and then calculates and displays the total cost of the sale. You may assume that all customers will require bags.

this is what I have so far:

string userInput;
double weight;
double price;
double capacityOfBag;
double bagPrice;
double bags;
uint numberOfBags;
uint remainder;
double cost;

Console.WriteLine("Please input the following in order to calculate the cost of sale");

Console.Write("Weight? ");
userInput = Console.ReadLine();
weight = Convert.ToDouble(userInput);

Console.Write("Price? ");
userInput = Console.ReadLine();
price = Convert.ToDouble(userInput);

Console.Write("Capacity of bag? ");
userInput = Console.ReadLine();
capacityOfBag = Convert.ToDouble(userInput);

Console.Write("Price of bag? ");
userInput = Console.ReadLine();
bagPrice = Convert.ToDouble(userInput);

bags = weight / capacityOfBag;
numberOfBags = ?????
cost = weight * price + numberOfBags * bagPrice;

Console.WriteLine($"The customer needs {numberOfBags} bags");
Console.WriteLine($"Total cost of the sale is {cost:c}");

The calculation should be simple, however, if the weight and the capacityOfBag are double numbers, say 24.3 and 5, then bags would be 4.86. however, I should have 5 bags!

What would numberOfBags be?

We haven't learned about if statements, so please don't use it in your answer, only simple math can be applied here :D

PS. Math library can't be used either.

Thanks.

  • Welcome. Please see marked duplicate for the exact answer to your question. Before you post another question, please visit and read through the [Tour](https://stackoverflow.com/tour) and [Help Center](https://stackoverflow.com/help) center sections, especially https://stackoverflow.com/help/asking. Pay particular attention to [ask], noting of course the admonition to start your process by **[searching](https://stackoverflow.com/search) and researching** – Peter Duniho Sep 28 '19 at 05:30
  • 2
    sorry, but that answer is too advanced for me, I just started my programming 1 at college :D – atin ariamehr Sep 28 '19 at 05:32
  • There are lots of answers on the marked duplicate. Make sure you look at _all_ of them. Regardless, you wouldn't get any different answers, had this question remained opened. If these answers are in fact too advanced for you, you need to have a talk with your instructor and have them walk you through the concepts. Stack Overflow is not a place for language tutorials or teaching the fundamentals of programming or particular languages. – Peter Duniho Sep 28 '19 at 05:35
  • good to know, thanks Peter. – atin ariamehr Sep 28 '19 at 05:38
  • Did you seek for `numberOfBags = bags % (uint)bags > 0 ? (uint)bags + 1 : (uint)bags;`? It will round up all `X.Y` to `X+1` when `Y > 0`. –  Sep 28 '19 at 05:51
  • @atinariamehr Here something simpler and faster: 1. floating point variables are rounded up by `x=ceil(x)` 2. integer division `a/b` is rounded up like this: `(a+b-1)/b` (see erryjvl answer in the linked duplicate) so no conditions or casting between types is needed just addition and decrement... its not too advanced its just a lot of reading to dig the simple answer ... – Spektre Sep 29 '19 at 07:04
  • @Spektre, that was really helpful, thank you very much – atin ariamehr Oct 01 '19 at 01:37

0 Answers0