I have inserted several WriteLine statements to assist with debugging. The values I get are confusing. To test the program and get the error I have received, use the value 93. Other values that cause problems include 13, and 53.
I would like to better understand the nature of this error, and am not so much looking for a solution for fixing it, although that could prove useful as well.
Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Chapter3Excercise6
{
class ChangeForDollar
{
public static double changeDouble = -999; //made global to allow access from all methods
static void Main()
{
AmountMethod();
double qQ = QuarterMethod();
double dQ = DimeMethod();
double nQ = NickelMethod();
double pQ = PennyMethod();
//output data
Console.WriteLine("You will recive " + qQ + " quarter(s).");
Console.WriteLine("You will recive " + dQ + " dimes(s).");
Console.WriteLine("You will recive " + nQ + " nickels(s).");
if (pQ > 1)
{
Console.WriteLine("You will recive " + pQ + " pennies.");
}
else
{
Console.WriteLine("You will recive " + pQ + " penny.");
}
}//end of main
static void AmountMethod() //Brings in data
{
Console.WriteLine("How much did you spend today? Please enter a value that is less than a dollar");
String valueString = Console.ReadLine();
double valueDouble = double.Parse(valueString);
if (valueDouble >= 1)//allows user to input price in either $x.xx format or xx¢
{
valueDouble = (valueDouble / 100);
}
changeDouble = (1.00 - valueDouble);
Console.WriteLine("\nValue of changeDouble at end of AmountMethod() is: " + changeDouble + "\n");
}
static int QuarterMethod()
{
int quarterQ = 0;
while (changeDouble >= .25)
{
++quarterQ;
changeDouble = (changeDouble - .25);
Console.WriteLine("Value of changeDouble at end of while loop\n in QuarterMethod() is: " + changeDouble + "\n");
}
return quarterQ;
}
static int DimeMethod()
{
int dimeQ = 0;
while (changeDouble >= .10)
{
++dimeQ;
changeDouble = (changeDouble - .10);
Console.WriteLine("Value of changeDouble at end of while loop\n in DimeMethod() is: " + changeDouble + "\n");
}
return dimeQ;
}
static int NickelMethod()
{
int nickelQ = 0;
while (changeDouble >= .05)
{
++nickelQ;
changeDouble = (changeDouble - .05);
Console.WriteLine("Value of changeDouble at end of while loop\n in NickelMethod() is: " + changeDouble + "\n");
}
return nickelQ;
}
static int PennyMethod()
{
int pennyQ = 0;
Console.WriteLine("Value of changeDouble at start of PennyMethod() is: " + changeDouble + "\n");
while (changeDouble >= .01)
{
Console.WriteLine("Value of changeDouble inside while loop\n at start of loop in PennyMethod() is: " + changeDouble + "\n");
++pennyQ;
Console.WriteLine("Value of changeDouble inside while loop\n before subtracting .01 in PennyMethod() is: " + changeDouble + "\n");
changeDouble = (changeDouble - .01);
Console.WriteLine("Value of changeDouble inside while loop\n after subtracting .01 in PennyMethod() is: " + changeDouble + "\n");
}
return pennyQ;
}
}//end of class
}//end of namespace
Thank you in advance for any comments, suggestions, and/or explanations.

New Topic/Question
Reply




MultiQuote







|