none
Issue with my program.... counting coins RRS feed

  • Question

  • I wrote a simple program to count US coins and tell me how much change I have.

    http://smallbasic.com/program/?RCP391


    TextWindow.WriteLine("Hello. I will count your money.")
    TextWindow.WriteLine("How many dollar coins do you have?")
    dollarcoins = TextWindow.Read()
    TextWindow.WriteLine("How many half dollars do you have?")
    halfdollars = TextWindow.Read()
    TextWindow.WriteLine("How many quarters do you have?")
    quarters = TextWindow.Read()
    TextWindow.WriteLine("How many dimes do you have?")
    dimes = TextWindow.Read()
    TextWindow.WriteLine("How many nickels do you have?")
    nickels = TextWindow.Read()
    TextWindow.WriteLine("How many pennies do you have?")
    pennies = TextWindow.Read()
    cents = (dollarcoins * 100) + (halfdollars * 50) + (qaurters * 25) + (dimes * 10) + (nickels * 5) + pennies
    TextWindow.WriteLine("You have " + cents + "¢ in coins according to the amounts you entered.")
    I've was using sample numbers to try this out. Here they are:
    dollar coins -- 1
    half dollars -- 2
    quarters -- 3
    dimes -- 28
    nickels -- 25
    pennies -- 95
    This is a total of 775¢ (I haven't figure out how to make this appear in dollars$)
    I have checked this total several times.

    Everytime I run this program with these numbers, it gives me a total of 700¢

    I cannot find my problem, but if any of you could check this and try to find my mistake, that would be great!

    Thank you!!!
    • Edited by Lithu.ania Sunday, May 24, 2009 4:31 PM making code more visible
    Sunday, May 24, 2009 4:30 PM

Answers

  • I has found the bug.

     You have this line

    quarters = TextWindow.Read()

    but in the line where you calculate the total, you have mispelled quarters:

    cents = (dollarcoins * 100) + (halfdollars * 50) + (qaurters * 25) + (dimes * 10) + (nickels * 5) + pennies


    The value of the variable qaurters is 0, so there's your missing 75 cents.
    Sunday, May 24, 2009 6:10 PM

All replies

  • I has found the bug.

     You have this line

    quarters = TextWindow.Read()

    but in the line where you calculate the total, you have mispelled quarters:

    cents = (dollarcoins * 100) + (halfdollars * 50) + (qaurters * 25) + (dimes * 10) + (nickels * 5) + pennies


    The value of the variable qaurters is 0, so there's your missing 75 cents.
    Sunday, May 24, 2009 6:10 PM
  • Maybe SmallBasic v0.5 should have a 'few-used variable' checker that show variables that are used very few time, or that are never setted in the program using MyVar = ...

    Print(MyVar)
    DoSomethingElse(MyVar)

    would show the warning : "The MyVar variable is never set in your program. Consider removing this variable by using "" or another variable."


    Fremy - Developer in VB.NET, C# and JScript ... - Feel free to try my extension
    Sunday, May 24, 2009 6:57 PM
  • One way to format your money is using Math.Floor and Math.Remainder

    Math.Floor(cents / 100)

    Math.Remainder(cents, 100)

    TextWindow.WriteLine("You have " + Math.Floor(cents / 100) + " dollars and " + Math.Remainder(cents, 100) + "¢ in coins according to the amounts you entered.")

    Your example would print-

    You have 7 dollars and 75¢ in coins according to the amounts you entered.
    Sunday, May 24, 2009 7:01 PM
  • ahh, thank you. it completely slipped my mind to check for typos
    Sunday, May 24, 2009 8:52 PM