I’m getting the following error:
/calcTax.rb:9: syntax error, unexpected $undefined
grandtotal = $#{subtotal - tax}
From this code:
print('What amount would you like to calculate tax for? $')
subtotal = gets.to_i
taxrate = 0.078
tax = subtotal * taxrate
if (tax > 0.0)
grandtotal = $#{subtotal + tax}
else if (tax < 0.0)
grandtotal = $#{subtotal - tax}
puts "Tax on $#{subtotal} is $#{tax}, so the grandtotal is $#{grandtotal}."
I’m wondering if I need to set subtotal to a value in a different way, or what I can do to repair my program?
I’m also getting an unexpected $end error on line 10.
There’s a couple of things wrong with your syntax here.
First of all, the
$#{blah}syntax is only needed (and only valid!) when inserting variables into a quoted string. When you’re just performing calculations, you can simply say something like:You’ll also need to add a
thenat the end of bothiflines, changeelse iftoelsif, and add anendafter the secondgrandtotalline. With this work all done, you should have:Which seems to work.