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);
        }
    }
}

Dani AI

Generated

Short, practical follow-ups to the existing replies by and : the core issue is converting the tax percent into a fractional rate (8.25% → 0.0825), but for correctness and real-world use it's better to (1) use the decimal type for money instead of float, (2) validate/parses user input safely, (3) decide whether to round each line item or the final total, and (4) format output as currency. The original program also declared shipping but never used it — either include it in the subtotal or remove it.

Example approach (robust, culture-aware, and using decimal):

using System;
using System.Globalization;

decimal price;
decimal taxRate = 0.0825m; // 8.25%
if (!decimal.TryParse(Console.ReadLine(), NumberStyles.Number | NumberStyles.AllowCurrencySymbol, CultureInfo.CurrentCulture, out price))
{
    Console.WriteLine("Invalid price format.");
    return;
}
decimal tax = Math.Round(price * taxRate, 2, MidpointRounding.AwayFromZero);
decimal total = price + tax; // add shipping here if applicable
Console.WriteLine(total.ToString("C", CultureInfo.CurrentCulture));

Notes and cautions: decimal avoids many of the tiny binary rounding errors that float/double can introduce with money. Rounding choices matter: receipts commonly round each tax amount to cents using MidpointRounding.AwayFromZero; other systems round the final total — that decision affects totals on multi-item invoices. For input, prefer TryParse over Parse to avoid exceptions on bad input. When running inside the debugger, use Console.ReadKey() at the end to keep the window open. These small changes make the result predictable and suitable for monetary calculations.

Recommended Answers

All 3 Replies

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

o ok. thank you that really did help alot!

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).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.