I need to receive three values in the same line, and after multiplying two of those values, my program is stopping and returning zero after the third input.
code_product_1, units_product_1, price_product_1 = gets.split(' ')
code_product_2, units_product_2, price_product_2 = gets.split(' ')
total = (units_product_1.to_i * price_product_1.to_f) (units_product_2.to_i * price_product_2.to_f)
puts "VALOR A PAGAR: R$ #{'%.2f' % total}"
problem description:
In this problem, the task is to read a code of a product 1, the number of units of product 1, the price for one unit of product 1, the code of a product 2, the number of units of product 2 and the price for one unit of product 2. After this, calculate and show the amount to be paid.
Input The input file contains two lines of data. In each line there will be 3 values: two integers and a floating value with 2 digits after the decimal point.
Output The output file must be a message like the following example where "Valor a pagar" means Value to Pay. Remember the space after ":" and after "R$" symbol. The value must be presented with 2 digits after the point.
EDIT: INPUT AND OUTPUT SAMPLES
Input Samples
- 12 - 1 - 5.30
- 16 - 2 - 5.10
Output Samples
- VALOR A PAGAR: R$ 15.50
EDIT2:
CodePudding user response:
total
is zero because some of the values collected from the user are nil
.
For the calculation of total
to be free of nil
, you'll need to collect two pieces of data from the user that each can be split
into three values.
Here's an example:
irb> code_product_1, units_product_1, price_product_1 = gets.split(' ')
1 2 3
=> ["1", "2", "3"]
irb> code_product_1
=> "1"
irb> units_product_1
=> "2"
irb> price_product_1
=> "3"
Do the same thing with your _2
inputs, and then you can complete your total calculation.
Note: this doesn't include an input or output file. That's mentioned in the question but the code shared is asking for values from the user with gets
.