Click here to Skip to main content
15,030,183 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
How do you total multiple inputs? I haven't used C# in a while and need a little help. Also, how do you get the program to not exit when the code is through.

Here is my code...if it will help:

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

namespace SalesTax
{
    class Program
    {
        static void Main(string[] args)
        {
            string product;
            float amount;
            string add = "no";
            string answer = "yes";
            float price;
            float salesTax = 10 / 100F;
            float impTax = 5 / 100F;
            float total = 0F;
            int count = 0;
            Console.WriteLine("Would you like to add a product? Y/N");
            add = Console.ReadLine();
            while (add == "Y")
            {
                Console.WriteLine("\nIs this product books, food, or medical supplies? Y/N");
                answer = Console.ReadLine();
                if (answer == "Y")
                {
                    Console.WriteLine("\nIs the product imported? Y/N");
                    if (answer == "Y")
                    {
                        answer = Console.ReadLine();
                        Console.WriteLine("\nWhat is the product you want?");
                        product = Console.ReadLine();
                        Console.WriteLine("\nHow much do of the product do you want?");
                        amount = Single.Parse(Console.ReadLine());
                        Console.WriteLine("\nWhat is the price?");
                        price = Single.Parse(Console.ReadLine());
                        Console.WriteLine(price);
                        Console.WriteLine("\nHow much is product with sales tax?");
                        price = (amount * price * impTax) + price;
                        Console.WriteLine(price);
                        count += 1;
                        Console.WriteLine("\nWould you like to add a product? Y/N");
                        add = Console.ReadLine();
                    }
                    else
                    {
                        answer = Console.ReadLine();
                        Console.WriteLine("\nWhat is the product you want?");
                        product = Console.ReadLine();
                        Console.WriteLine("\nHow much do of the product do you want?");
                        amount = Single.Parse(Console.ReadLine());
                        Console.WriteLine("\nWhat is the price?");
                        price = Single.Parse(Console.ReadLine());
                        Console.WriteLine(price);
                        Console.WriteLine("\nHow much is product with sales tax?");
                        price = (amount * price * salesTax) + price;
                        price = price;
                        Console.WriteLine(price);
                        count += 1;
                        Console.WriteLine("\nWould you like to add a product? Y/N");
                        add = Console.ReadLine();
                    }
                }
                else
                {
                    Console.WriteLine("Is the product imported? Y/N");
                    if (answer == "Y")
                    {
                        answer = Console.ReadLine();
                        Console.WriteLine("\nWhat is the product you want?");
                        product = Console.ReadLine();
                        Console.WriteLine("\nHow much do of the product do you want?");
                        amount = Single.Parse(Console.ReadLine());
                        Console.WriteLine("\nWhat is the price?");
                        price = Single.Parse(Console.ReadLine());
                        Console.WriteLine(price);
                        Console.WriteLine("\nHow much is product with sales tax?");
                        price = (amount * price * salesTax * impTax) + price;
                        Console.WriteLine(price);
                        count += 1;
                        Console.WriteLine("\nWould you like to add a product? Y/N");
                        add = Console.ReadLine();
                    }
                    else
                    {
                        answer = Console.ReadLine();
                        Console.WriteLine("\nWhat is the product you want?");
                        product = Console.ReadLine();
                        Console.WriteLine("\nHow much do of the product do you want?");
                        amount = Single.Parse(Console.ReadLine());
                        Console.WriteLine("\nWhat is the price?");
                        price = Single.Parse(Console.ReadLine());
                        Console.WriteLine(price);
                        Console.WriteLine("\nHow much is product with sales tax?");
                        price = (amount * price * salesTax) + price;
                        Console.WriteLine(price);
                        count += 1;
                        Console.WriteLine("\nWould you like to add a product? Y/N");
                        add = Console.ReadLine();
                    }
                }
            }
            Console.WriteLine("Total:");

        }
    }
}
Posted

The program will stop when add variable is not "Y", and it has to be Upper case "Y". If you type "y" then the program will stop.

You can change
while (add == "Y")

To
while (add.ToUpper() == "Y")


About total multiple inputs. You can make a new class that holds all information on all product added.

public class ProductInvoice
{
  public bool Imported;
  public string Product; // Name of product
  public float Price;
  public float PriceWithSalesTax;
  // .. etc
}


And then hold all info in a List<productinvoice>[^] list and do call calculations there.
   
v2
Comments
fjdiewornncalwe 16-May-11 18:30pm
   
OP's comment moved from answer:
What I mean is that the "customer" has entered in a number of inputs...i want to total the cost. These are the questions:

Is this product books, food, or medical supplies? (which would mean exempt from sales tax if so.)
Is the product imported? (add an extra .05 to the tax)
What is the product you want?
How much do of the product do you want?
What is the price?

I actually want there to be a summary, so let's say the customer put in 3 inputs.

1 imported face wash = 6.99 => tax would be .04 ~ 7.03
2 chocolate bars = 1.98 => tax would be 0 because it is a food and not imported
1 TV = 599.98 => tax would be 60 ~ 659.98

I would like the output to state:

1 imported face wash 7.03
2 chocolate bars = 1.98
1 TV = 659.98
Tax = 60.04
Total = 668.99

Make sense?
Are you learning to program, or actually writing something for a business?

The reason I ask is not to be insulting or judgemental, but only because knowing more about what you are doing and why could help others decide how best to help you. For example, if you are learning, people may want to give you small tips regarding pieces of what you have done so far, but not write a solution for you. If you are doing this for a business, there is a long way to go in the design before worry about the code, you'll want to start with some questions regarding application flow (methods rather than long if/else blocks, etc.).

That being said, I will just point out a couple of quick things that caught my eye.

First, if you want to recall specific information at some point, you need to store it. If you create a variable called "price", and you are setting price to some new value every so often, the only value you will get out of it is the last one it was set to. To retain information, look into using arrays for starters.

Second, at one point, you say price = price, hopefully you see the problem with that line at this point. :) It does no good, as 'price' is already equal to 'price'.

So, give some more details on what specifically you are wanting to do, why, and try and break it down into specific questions on how to do certain things, you may receive more pointed assistance.

Best of luck.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900