Looping problem during compiling of C# console application.

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 2213 Views - Last Post: 23 June 2012 - 05:25 AM Rate Topic: -----

#1 codebuzz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 07-October 10

Looping problem during compiling of C# console application.

Posted 21 June 2012 - 02:02 PM

Hi guys, I am having some problem during the running of my c# console application. The output format doesn't match the expected output as shown in this image I attached with my question. Also find my codes in the code tags.

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GroceryStore
{
    class Product
    {
        
         private const double foodVATRate = 0.12, otherVATRate = 0.25;
         private string name;
         private int count;
         private double value;
         private double resultFood;
       

           public void Start()
        {
            ReadName();
            CalculateValues();
            PrintReceipt();
        }
        private void ReadInput()
        {
            ReadName();
            ReadNetUnitPrice();
            ReadIfFoodItem();
            ReadCount();


        }
        private String ReadName()
        {
            Console.Write("Enter the product name:     ");
             name = Console.ReadLine();
            return name;
        }

               
        private bool ReadIfFoodItem()
        {
            
            Console.Write("Food item (y/n):    ");
            char response = char.Parse(Console.ReadLine());
            if ((response == 'y') || (response == 'Y'))
                
                return true;
            else

                return false;
                 }

        private double ReadNetUnitPrice()
        {
            Console.WriteLine("Net unit price :");
            value = Double.Parse(Console.ReadLine());
            return value;

        }

        private int ReadCount()
        {
            Console.WriteLine("Count :");
            count = int.Parse(Console.ReadLine());
            return count;

        }

        private double CalculateValues()
          {
            //double resultFood;
         if (ReadIfFoodItem() == true) { //always use {} to be on the safe side
        resultFood = ReadNetUnitPrice() * foodVATRate * ReadCount();
         }

    else 
         {
        resultFood = ReadNetUnitPrice() * otherVATRate * ReadCount();
    }
    return resultFood;
}

        private void PrintReceipt()
        {
            Console.WriteLine("++++++++++WELCOME TO APU's SUPERMARKET+++++++++++");
            Console.WriteLine("+++");
            Console.WriteLine("+++ Name of the Product\t\t\t"+ name);
            Console.WriteLine("+++ Quantity           \t\t\t "+ count);
            Console.WriteLine("+++ Unit Price         \t\t\t" + value);
            //Console.WriteLine("+++ Food Item" + ReadIfFoodItem());
            //Console.WriteLine("+++");
           
            if (ReadIfFoodItem() == true)
            {
                Console.WriteLine("+++ Including VAT at 12% " + resultFood);
                Console.WriteLine("+++ Food Item"  );
            }
            else
            {
                Console.WriteLine("+++ Including VAT at 25% " + resultFood);
                Console.WriteLine("+++ Food Item"  );
            }
            Console.WriteLine("+++ Total Amount to Pay" + CalculateValues());
            Console.WriteLine("+++++++++++PLEASE COME AGAIN!++++++++++++++++++++++++++");
        }
    }

}
    


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GroceryStore
{
    class Program
    {
        static void Main(string[] args)
        {
            Product apu = new Product();
            apu.Start();
            Console.ReadLine();          
        }
    }
}
[attachment=29920:tt.JPG]

    





Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Looping problem during compiling of C# console application.

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9089
  • View blog
  • Posts: 34,142
  • Joined: 12-June 08

Re: Looping problem during compiling of C# console application.

Posted 21 June 2012 - 02:07 PM

Quote

The output format doesn't match the expected output as shown in this image I attached with my question.

What does this mean exactly? How does it not match up? What are the expected output?
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5479
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Looping problem during compiling of C# console application.

Posted 21 June 2012 - 02:08 PM

"Some problem" ??? Care to be just a bit more descriptive?

The math seems right:
6*1.95 = 11.7 + 2.925 = 14.625

So what exactly is the problem?
Was This Post Helpful? 0
  • +
  • -

#4 codebuzz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 07-October 10

Re: Looping problem during compiling of C# console application.

Posted 21 June 2012 - 02:21 PM

View Postmodi123_1, on 21 June 2012 - 02:07 PM, said:

Quote

The output format doesn't match the expected output as shown in this image I attached with my question.

What does this mean exactly? How does it not match up? What are the expected output?


The expected output should be if for example I used all those values for quantity and the price of the product it should produced output exactly as on the attached image. But the problem is my program keep asking for more information e.g. Food Item(y or n) before ending the problem which makes it not look good. If you try to run the code you will see what I am talking about.

View PosttlhIn`toq, on 21 June 2012 - 02:08 PM, said:

"Some problem" ??? Care to be just a bit more descriptive?

The math seems right:
6*1.95 = 11.7 + 2.925 = 14.625

So what exactly is the problem?


The image is the example of how the output should be after I run the program but when I run using those values on the image file for example it doesn't give me the right output. The maths is not the problem but the input/output of the parameters and the formatted result.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5479
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Looping problem during compiling of C# console application.

Posted 21 June 2012 - 02:25 PM

We don't run all 500,000 member's broken code.


Quote

it doesn't give me the right output.

Saying "Its broke" or "Its wrong" doesn't tell us anything.

Attach a screen shot of the problem/bad output. Saying "If you run it you'll see the problem." just tells us it isn't worth your time to form your question well.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9089
  • View blog
  • Posts: 34,142
  • Joined: 12-June 08

Re: Looping problem during compiling of C# console application.

Posted 21 June 2012 - 02:27 PM

If that's the case where it's simply not showing up right then it's high time for Debugging! Place a break point at the top of your code and step through everything! Perhaps you are not setting a variable, clearing it out inadvertently, or (more than likely) your code is not executing as you assume.
Was This Post Helpful? 0
  • +
  • -

#7 codebuzz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 07-October 10

Re: Looping problem during compiling of C# console application.

Posted 21 June 2012 - 02:33 PM

View PosttlhIn`toq, on 21 June 2012 - 02:25 PM, said:

We don't run all 500,000 member's broken code.


Quote

it doesn't give me the right output.

Saying "Its broke" or "Its wrong" doesn't tell us anything.

Attach a screen shot of the problem/bad output. Saying "If you run it you'll see the problem." just tells us it isn't worth your time to form your question well.


This is the screenshot of the program. You will see the difference with the first image I uploaded.Attached Image
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5479
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Looping problem during compiling of C# console application.

Posted 21 June 2012 - 02:39 PM

You need to walk through your code and just look at it.


084 private void PrintReceipt()
094 if (ReadIfFoodItem() == true)
execution therefore goes to the ReadIfFoodItem() method

041 private bool ReadIfFoodItem()
044 Console.Write("Food item (y/n): ");
045 char response = char.Parse(Console.ReadLine());

In the middle of your your PrintReceipt() method you start asking the user for input.

Your code is doing exactly what you are telling it to do.
Was This Post Helpful? 0
  • +
  • -

#9 codebuzz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 07-October 10

Re: Looping problem during compiling of C# console application.

Posted 21 June 2012 - 02:49 PM

View PosttlhIn`toq, on 21 June 2012 - 02:39 PM, said:

You need to walk through your code and just look at it.


084 private void PrintReceipt()
094 if (ReadIfFoodItem() == true)
execution therefore goes to the ReadIfFoodItem() method

041 private bool ReadIfFoodItem()
044 Console.Write("Food item (y/n): ");
045 char response = char.Parse(Console.ReadLine());

In the middle of your your PrintReceipt() method you start asking the user for input.

Your code is doing exactly what you are telling it to do.


I have already commented the user input but the problem is the output keeps asking me "Food Item (y/n)" which I don't want. Please can you explain further what I should do.
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9089
  • View blog
  • Posts: 34,142
  • Joined: 12-June 08

Re: Looping problem during compiling of C# console application.

Posted 21 June 2012 - 03:00 PM

Have you read post #6
Was This Post Helpful? 0
  • +
  • -

#11 codebuzz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 07-October 10

Re: Looping problem during compiling of C# console application.

Posted 21 June 2012 - 03:06 PM

View Postmodi123_1, on 21 June 2012 - 03:00 PM, said:

Have you read post #6


Yes I have read it and I try to insert the breakpoint but it just shown one red ball and highlighted my code and no action taken. I am new to C#, can you please explain better?
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9089
  • View blog
  • Posts: 34,142
  • Joined: 12-June 08

Re: Looping problem during compiling of C# console application.

Posted 21 June 2012 - 03:08 PM

Did you read through the tutorial? You know hit the point where it says 'run the code in debug mode' and when your code comes to the red ball it should stop.
Was This Post Helpful? 0
  • +
  • -

#13 codebuzz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 07-October 10

Re: Looping problem during compiling of C# console application.

Posted 21 June 2012 - 03:26 PM

View Postmodi123_1, on 21 June 2012 - 03:08 PM, said:

Did you read through the tutorial? You know hit the point where it says 'run the code in debug mode' and when your code comes to the red ball it should stop.


Yes I try to use the breakpoint but it just taking me to each line of code and run it again. I have not yet see the problem why it keeps asking for Food Items or not.
Was This Post Helpful? 0
  • +
  • -

#14 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9089
  • View blog
  • Posts: 34,142
  • Joined: 12-June 08

Re: Looping problem during compiling of C# console application.

Posted 21 June 2012 - 04:02 PM

Okay so that tells you you are having an input issue. look at your code around there and determine what you need to add to NOT have it do that again.
Was This Post Helpful? 0
  • +
  • -

#15 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5479
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Looping problem during compiling of C# console application.

Posted 21 June 2012 - 04:35 PM

Q: I do x and y happens which I didn't expect but I don't know how to figure out why. How do I debug and find my problem?
A:
Spoiler

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2