Issue with Receipt Generator Application
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Issue with Receipt Generator Application

Hybrid View

  1. #1
    Join Date
    Jan 2013
    Posts
    1

    Issue with Receipt Generator Application

    Hey everyone,

    I'm having issues priming the with the GetPrices function within my receipt generator program. Essentially, I am only able to enter in a single numerical value for the price. I release it's because I have a logic error within my do-while loop, but I'm having trouble converting that logic into a priming loop form.

    Ideally, I would like to input a series of numbers, each followed by a bool prompt after each entry asking "Do you want to input another value?" When the entry is finished, the application will generate a receipt that lists subtotal, tax, and the grand total.

    Essentially, the logic is as follows:

    1. Display price prompt
    2. Get price information
    3. Display continue prompt
    4. Get continue answer
    5. Loop to #1 while continue answer is "Y" or "y"

    My issue is that I can't figure out how to implement steps 4 and 5 within my program. I would greatly appreciate your help, as I am a complete newbie when it comes to C#.

    Thanks in advance!

    Code:
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
    
    namespace ProgramName
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<double> prices; 
                double subtotal;
                double tax;
                double taxValue = 0.09; //the rate of tax to be paid; in this case, 9% 
    
                double total;
    
                prices = GetPrices(); //capitalize all words in functions; variables start with a lowercase
                subtotal = CalcSubtotal(prices);
                tax = CalcTax(subtotal, taxValue);
                total = Total(subtotal, tax);
                PrintReceipt(prices, subtotal, tax, total);
            }
    
            static List<double> GetPrices()
            {
                List<double> prices = new List<double>();  //creates an array for us, so all we have to do is add and subtract
    
    
                //To Do: Get Prices
                string buffer;
                double price;
                Console.WriteLine("Please enter the price of the item.");
                do
                {
                    buffer = Console.ReadLine();
    
                    if (double.TryParse(buffer, out price))
                    {
                        prices.Add(price);
                    }
                    else
                    {
                        Console.WriteLine("Incorrect data was input. Please enter the price as a numerical value.");
                        buffer = Console.ReadLine();
                    }
    
                   Console.WriteLine("Do you wish to add more items? Please type either Y or N and press the enter key.");
                {
                if (Console.ReadLine("Y" || "y"));  //my latest additions here
                {
                 prices.Add(price);
                }
    
                else if (Console.ReadLine("N" || "n"));
                {
                 Console.WriteLine("Processing your receipt.");
                }
    
    
                }
                while (buffer == "Y" || buffer == "y");
    
                return prices;
            }
    
            static double CalcSubtotal(List<double> prices)
    
            {
                double subtotal = 0.00;
                foreach (double temp in prices)
    
                {
                    subtotal += temp;
                }
                return subtotal;
                //function that calculates the subtotal by taking 
            }
    
    
             static double Total (double subtotal, double tax)
             {
             double total = 0.00;
    
             total=subtotal + tax;
             return total;
             }
    
            static double CalcTax(double subtotal, double taxValue)
    
            {
                double tax;
                tax = subtotal * taxValue;
                return tax;
    
            }
    
             static void PrintReceipt(List<double> prices, double subtotal, double total, double tax)
             {
                 Console.WriteLine("Welcome to our store!");
                 {
                     foreach (double temp in prices)
    
                     {
                         Console.WriteLine(temp);
                     }
    
                 }
                 Console.WriteLine("The subtotal is..." + subtotal);
                 Console.WriteLine("The amount of tax to be paid is..."+ tax);
                 Console.WriteLine("The grand total is..." + total);
                 Console.WriteLine("Thank you for shopping with us!");
             }
    
    
    
        }
    }

  2. #2
    Join Date
    Jul 2012
    Posts
    90

    Re: Issue with Receipt Generator Application

    Generally, the way I've seen this work is as follows:

    1. Display price prompt
    2. Get price information
    3. If price information is end of data code continue else goto 1
    4. Process and print recript

    You just have to decide what will be the end of data code to pass when you are finished entering price data.
    Last edited by CGKevin; January 24th, 2013 at 03:04 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center