2 Replies - 1139 Views - Last Post: 07 November 2008 - 10:04 PM Rate Topic: -----

#1 Guest_Nostalgia20*


Reputation:

Can someone help me what is wrong with this program?

Post icon  Posted 07 November 2008 - 08:15 PM

This code is straight out of from C# Programming by Barbara Doyle
When I tried to debug it's not giving me the result that is on the textbook. I tried to figure out what is wrong with it for like an hour and i cant figure it out. can someone help me what is wrong with this? thank you. :crazy:

using System;
namespace HW3
{
	public class CarpetWithClassMethods
	{
		public static void Main()
		{
			double roomwidth;
			double roomlength;
			double pricePerSqYard;
			double noOfSquareYards;
			DisplayInstructions();
			roomlength = GetDimension("length");
			roomwidth = GetDimension("width");
			pricePerSqYard = GetPrice();
			noOfSquareYards = DetermineSquareYards(roomwidth, roomlength);
			DisplayResults(noOfSquareYards, pricePerSqYard);
		}
		public static void DisplayInstructions()
		{
			Console.WriteLine("This program will determine how much carpet to purchase. ");
			Console.WriteLine("You will be asked to enter the size of the room ");
			Console.WriteLine("and the price of the carpet, in price per square yards.");
			Console.WriteLine();
		}
		public static double GetDimension(string side)
		{
			string inputValue;
			int feet, inches;
			Console.Write("Enter the {0} in feet: ", side);
			inputValue = Console.ReadLine();
			feet = int.Parse(inputValue);
			Console.Write("Enter the {0} in inches: ", side);
			inputValue = Console.ReadLine();
			inches = int.Parse(inputValue);
			return (feet + (double)inches / 12);
		}
		public static double GetPrice()
		{
			string inputValue;
			double price;
			Console.Write("Enter the price per Square Yard: ");
			inputValue = Console.ReadLine();
			price = double.Parse(inputValue);
			return price;
		}
		public static double DetermineSquareYards(double width, double length)
		{
			const int SQ_FT_PER_SQ_YARD = 9;
			double noOfSquareYards;
			noOfSquareYards = length * width / SQ_FT_PER_SQ_YARD;
			return noOfSquareYards;
		}
		public static double DeterminePrice(double noOfSquareYards, double pricePerSquareYard)
		{
			return (pricePerSquareYard * noOfSquareYards);
		}
		public static void DisplayResults(double noOfSquareYards, double pricePerSquareYard)
		{
			Console.WriteLine();
			Console.WriteLine("Square Yarads needed: {0,N2}", noOfSquareYards);
			Console.WriteLine("Total Cost at {0;C} ", pricePerSquareYard);
			Console.WriteLine(" per Square Yard: {0:C} ", DeterminePrice(noOfSquareYards, pricePerSquareYard));
		}
	}
}



Is This A Good Question/Topic? 0

Replies To: Can someone help me what is wrong with this program?

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4364
  • View blog
  • Posts: 12,182
  • Joined: 18-April 07

Re: Can someone help me what is wrong with this program?

Posted 07 November 2008 - 10:00 PM

Well first of all you have bugs in your displayResults function...

public static void DisplayResults(double noOfSquareYards, double pricePerSquareYard)
{
            Console.WriteLine();
            // Notice we have a colon, not a comma before "N2"
            Console.WriteLine("Square Yarads needed: {0:N2}", noOfSquareYards);
            // Notice again we have colon, not a semicolon
            Console.WriteLine("Total Cost at {0:C} ", pricePerSquareYard);
            Console.WriteLine(" per Square Yard: {0:C} ", DeterminePrice(noOfSquareYards, pricePerSquareYard));
}



Once you fix this, you should be getting the correct values. One test would be 6 feet 0 inches by 6 feet 0 inches is 4 square yards so at 1.00 per square yards you should get $4.00 as an answer.

Enjoy!

"At DIC we be floor figuring code ninjas... we also do drapes, windows, and counter tops!" :snap:
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Nostalgia20*


Reputation:

Re: Can someone help me what is wrong with this program?

Posted 07 November 2008 - 10:04 PM

Thank you :^:
Was This Post Helpful? 0

Page 1 of 1