10 Replies - 417 Views - Last Post: 18 September 2012 - 06:50 AM Rate Topic: -----

#1 blitzgash  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-September 12

Double not being assigned in for statement.

Posted 17 September 2012 - 11:42 PM

I've completed most of this project but seem to have a problem getting visual studio to recognize that the discount is assigned.

It involves discount being in a for loop.

 double PRICE_PER_ITEM = 6.00;
        double price;
        double discount;
        int[] quanLimits = { 0, 5, 10, 15 };
        double[] limits = { 0, 0.10, 0.14, 0.20 };
        for (int x = quanLimits.Length - 1; x >= 0; --x)
        {
            if (quantityOrdered >= quanLimits[x])
                discount = limits[x];
        }
        price = quantityOrdered * PRICE_PER_ITEM;
        price = price - (price * discount);
        return price;



price = price - (price * discount);

is returning an error saying it is never assigned a value..?

Is This A Good Question/Topic? 0
  • +

Replies To: Double not being assigned in for statement.

#2 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 486
  • Joined: 13-June 12

Re: Double not being assigned in for statement.

Posted 17 September 2012 - 11:51 PM

I appreciate this isn't full code, but where is The quantityOrdered variable being assigned a value? That's the problem- the discount variable is only assigned a value if quantityOrdered is greater than or equal to a value in the quanLimits array. I it has no value, the condition will never be true.

This post has been edited by MrShoes: 17 September 2012 - 11:54 PM

Was This Post Helpful? 0
  • +
  • -

#3 blitzgash  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-September 12

Re: Double not being assigned in for statement.

Posted 17 September 2012 - 11:55 PM

public static double CalculatePrice(int quantityOrdered)
    {
        double PRICE_PER_ITEM = 6.00;
        double price;
        double discount;
        int[] quanLimits = { 0, 5, 10, 15 };
        double[] limits = { 0, 0.10, 0.14, 0.20 };
        for (int x = quanLimits.Length - 1; x >= 0; --x)
        {
            if (quantityOrdered >= quanLimits[x])
                discount = limits[x];
        }
        price = quantityOrdered * PRICE_PER_ITEM;
        price = price - (price * discount);
        return price;



I'm sorry it's a method.

Oh crap, I didn't get your full post. Is there a delete post option?
Was This Post Helpful? 0
  • +
  • -

#4 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 486
  • Joined: 13-June 12

Re: Double not being assigned in for statement.

Posted 18 September 2012 - 12:21 AM

No delete, but you get an Edit option once you've made a certain number of posts/earned enough rep.

OK, so quantityOrdered is being passed as a parameter to the method. Have you set breakpoints and stepped through the code? What happens if quantityOrdered is less than zero? Can that ever happen? Also, is there a reason you're predecrementing x? Change that --x to x-- and see what the difference is...
Was This Post Helpful? 0
  • +
  • -

#5 blitzgash  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-September 12

Re: Double not being assigned in for statement.

Posted 18 September 2012 - 12:27 AM

The entire code is this:

using System;
public class Price
{
    public static void Main()
   {
      int quantity;
      double price;
      quantity = GetQuantity();
      price = CalculatePrice(quantity);
      Console.WriteLine("Final price for {0} items is {1}.",
        quantity, price.ToString("c"));
   }
    public static int GetQuantity()
    {
        int quantity;
        Console.Write("Enter number of items >> ");
        quantity = Convert.ToInt32(Console.ReadLine());
        return quantity;
    }
    public static double CalculatePrice(int quantityOrdered)
    {
        double PRICE_PER_ITEM = 6.00;
        double price;
        double discount;
        int[] quanLimits = { 0, 5, 10, 15 };
        double[] limits = { 0, 0.10, 0.14, 0.20 };
        for (int x = quanLimits.Length; x >= 0; --x)
        {
            if (quantityOrdered >= quanLimits[x])
                discount = limits[x];
        }
        price = quantityOrdered * PRICE_PER_ITEM;
        price = price - (price * discount);
        return price;
    
    }
}


if quantity ordered were 10, then discount = quanLimits[2], which is 0.14, right?

But price = price - (price * discount); is returning an error while compiling, saying it is never assigned. do for statements not allow outside information?

This post has been edited by JackOfAllTrades: 18 September 2012 - 03:13 AM
Reason for edit:: Fixed code tags.

Was This Post Helpful? 0
  • +
  • -

#6 Michael26  Icon User is offline

  • Radar love
  • member icon

Reputation: 332
  • View blog
  • Posts: 1,419
  • Joined: 08-April 09

Re: Double not being assigned in for statement.

Posted 18 September 2012 - 12:39 AM

try to initialize the discount value to 0.
Was This Post Helpful? 0
  • +
  • -

#7 blitzgash  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-September 12

Re: Double not being assigned in for statement.

Posted 18 September 2012 - 12:46 AM

That does let me build the program, however it just keeps discount at 0 throughout the entire code. It will not assign it to limits[x].. Hmm.. when quantityOrdered is set to 10 the output is 60. (meaning it didn't apply the .14 discount.)

I'm about to throw in the towel on this. I'm tired...
Was This Post Helpful? 0
  • +
  • -

#8 Michael26  Icon User is offline

  • Radar love
  • member icon

Reputation: 332
  • View blog
  • Posts: 1,419
  • Joined: 08-April 09

Re: Double not being assigned in for statement.

Posted 18 September 2012 - 12:56 AM

Also don't forget, arrays are zero-based, in for loop the length return 4 for this
int[] quanLimits = { 0, 5, 10, 15 };

but it is actually 0,1,2,3 so you'll get and IndexOutOfRange exception

tried several values, it is skipping the if loop always

This post has been edited by Michael26: 18 September 2012 - 01:08 AM

Was This Post Helpful? 0
  • +
  • -

#9 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 486
  • Joined: 13-June 12

Re: Double not being assigned in for statement.

Posted 18 September 2012 - 01:32 AM

Michael26 OP is aware of the indexing, as he had reduced 1 from the array length in his for statement - for some reason has now taken that out.

blitzgash - if you declare a variable outside a for loop then set its value inside the for loop, it will retain the value. That's not the issue.

Here's the problem: You're going backwards. You check if the quantityOrdered id greater than 15, then if true, set the discount to 0.20. Then you check if it's greater than 10, and if true, set discount to 0.14. Eventually, you check if >0 and set it to 0. That means any positive value will get a discount of 0.

Go up instead. First, check if >0 and set discount... increase the discount until you find a false condition.

public class Price
    {
        public static void Main()
        {
            int quantity;
            double price;
            quantity = GetQuantity();
            price = CalculatePrice(quantity);
            Console.WriteLine("Final price for {0} items is {1}.",
            quantity, price.ToString("c"));

            Console.ReadKey();
        }
        public static int GetQuantity()
        {
            int quantity;
            Console.Write("Enter number of items >> ");
            quantity = Convert.ToInt32(Console.ReadLine());
            return quantity;
        }
        public static double CalculatePrice(int quantityOrdered)
        {
            double PRICE_PER_ITEM = 6.00;
            double price;
            double discount = 0;
            int[] quanLimits = { 0, 5, 10, 15 };
            double[] limits = { 0, 0.10, 0.14, 0.20 };
            for (int x = 0; x < quanLimits.Length; x++)
            {
                if (quantityOrdered >= quanLimits[x])
                    discount = limits[x];
            }
            price = quantityOrdered * PRICE_PER_ITEM;
            price = price - (price * discount);
            return price;

        }
    }

This post has been edited by MrShoes: 18 September 2012 - 01:40 AM

Was This Post Helpful? 2
  • +
  • -

#10 blitzgash  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-September 12

Re: Double not being assigned in for statement.

Posted 18 September 2012 - 01:52 AM

Didn't think about the for statement needing to go up or else it would end at 0. MrShoes has saved the day. :) thanks for the insight.
Was This Post Helpful? 0
  • +
  • -

#11 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Double not being assigned in for statement.

Posted 18 September 2012 - 06:50 AM

I'd suggest that you go through this tutorial on debugging. You would have figured this out if you would have set a breakpoint and walked through the code.

http://www.dreaminco...6780-debugging/
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1