Author Topic: Help with a small program  (Read 3062 times)

Gai Daigoji

  • Gekigangar!
  • Full Member
  • ***
  • Posts: 171
    • View Profile
    • http://www.freewebs.com/neo-terra
Help with a small program
« on: November 02, 2005, 08:04:49 PM »
Ok I am dabbling with learning C# and  want to make a program were I can calculate income tax. Here's what I got so far:

using System;

class CalculateTaxalculate
{
   static void Main()
   {
      string inputSalary;
      double TaxNumber;
      double TaxSum;
      double Salary;

      Console.WriteLine("Please enter Salary");
      Salary = Console.Read();
      inputSalary = Convert.ToInt32(Salary);

      if (Salary > 23000)
      {
         TaxSum = Salary * 0.22;
         Console.WriteLine("Your tax is ");
         Console.Read(TaxSum);
      }

      if (Salary == 23000)
      {
      TaxSum = Salary * 0.22;
      Console.WriteLine("Your tax is ");
      Console.Read(TaxSum);
      }

      
      else if (Salary < 23000)
      {
         TaxSum = Salary * 0.10;
         Console.Write("Your Tax is ");
         Console.Read(TaxSum);
      }

      

      else if (Salary > 32000)
      {
         TaxSum = Salary * 0.40;
         Console.Write("Your Tax is ");
         Console.Read(TaxSum);
      }

      if (Salary < 0)
      {
         Console.WriteLine("Invalid number, please try again");
      }

      if (Salary > 100000)
      {
         Console.WriteLine("Invalid number, please try again");
      }




Can anyone tell me whats wrong with this or what I need to have? Many thanks :)
Project Leader / FREDer: Project: Gundam Seed: Wheels of Destiny -Forum

karajorma

  • He is watching YOU
  • Administrator
  • Hero Member
  • *****
  • Posts: 5268
    • View Profile
Help with a small program
« Reply #1 on: November 02, 2005, 11:46:15 PM »
Is it not compiling or is it giving you the wrong answers?

I've not worked through the logic much but remember that else always associates with the nearest if (at least in Java it does. I don't know much about C# except that it's MS's attempt to make Java so I'd imagine it is the same in that respect). I can see that screwing things up cause you've got a lot of else if's in there.

kode

  • The Swede
  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Help with a small program
« Reply #2 on: November 12, 2005, 03:19:16 PM »
if you still need help...

well anyway, I don't code C#, but I'd suggest you either nest all those if-statements together, or use a switch case with cleverer partitioning of the options. as it is now, I suppose that given a salary larger than 32000, it would calculate and print the tax for both >23000 and >32000 - this is because the if statements aren't nested together.

also, I have a question regarding this:
Code: [Select]

TaxSum = Salary * 0.10;
Console.Write(&quot;Your Tax is &quot;);
Console.Read(TaxSum);


doesn't the last line expect the user to enter data? If I understand it correctly, you just calculated the tax and saved it to the variable TaxSum, then printed out in the console the line "Your tax is: "
and then ask the user for input overwriting the same variable in which you saved the taxsum. Again, I don't code C#, but if it is the case that you want to output stuff, I think Console.Write would be the right method to use.