954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

trying to add sales tax to price

ok so i am new to c# and i am trying to write a program that asked the user for input on what item they want to buy, the price for the item and to show the input of the price after sales tax.i am having trouble getting the price with the sales tax to show. help please

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

namespace Lab
{
    class Program
    {
        static void Main(string[] args)
        {
            string product;
            float price;
            float salesTax = 8.25F;
            float shipping;

            Console.WriteLine("Welcom to &Inc");
            Console.WriteLine("What is the product you want?");
            product = Console.ReadLine();
            Console.WriteLine("What is the price");
            price = Single.Parse(Console.ReadLine());
            Console.WriteLine(price);
            Console.WriteLine("How much is product with sales tax");
            price = price * salesTax + price;
            Console.WriteLine("This is the price with sales tax");
            Console.WriteLine(price);
        }
    }
}
ROTC89
Junior Poster in Training
62 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

Sales tax is a percentage, so you should divide it by 100 somewhere. May as well handle that in the declaration

float salesTax = 8.25F/100; // or simply hardcode 0.0825f


This won't affect your output, but you can also simplify the code on the modified price calculation. A couple of different ways, actually.

//price += price * salesTax; // 1st option
            price *= (1 + salesTax); // 2nd option
apegram
LINQ!
Team Colleague
552 posts since Jan 2010
Reputation Points: 327
Solved Threads: 135
 

o ok. thank you that really did help alot!

ROTC89
Junior Poster in Training
62 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

Good. Beyond that, if you're running it from a debug build and not from a command line and you need it to stick on the screen after the program has spit out the output, you can simply throw in a Console.Read() after the last Console.WriteLine(...) statement. The program will then close once you hit Enter (or continue on to do something else, if code were to follow the .Read() statement).

apegram
LINQ!
Team Colleague
552 posts since Jan 2010
Reputation Points: 327
Solved Threads: 135
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You