using System;

public class BonusProduction
{
   public static void Main()
   {
        
  int FName,
            LNAME;
   double LastYearProduction;
   double ThisYearProduction;

Console.WriteLine("Enter your first name");
FName = Convert.ToInt32(Console.ReadLine());
Console.ReadLine("Enter your last name");
LNAME = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Last Year's Production"); 
LastYearProduction = Convert.ToInt32(Console.ReadLine());
if(LastYearProduction < 0)
Console.WriteLine("Invalid");
{
Console.WriteLine("Enter This Year's Production"); 
ThisYearProduction = Convert.ToInt32(Console.ReadLine());
if(ThisYearProduction < 0)
Console.WriteLine("Invalid");
   }}

public static void CalculateBonus()
  {         

int FName,
            LNAME;
   double LastYearProduction;
   double ThisYearProduction;


bool bonus;

bonus = ThisYearProduction - LastYearProduction;
if(bonus <= 1000)
Console.WriteLine("Your bonus is $25");
else if(bonus >= 1000 && <= 3000)
Console.WriteLine("Your bonus is $50");
else if(bonus > 3000 && <= 6000)
Console.WriteLine("Your bonus is $100");
else if(bonus > 6000)
Console.WriteLine("your bonus is $200");
{
}
else
Console.WriteLine("Invalid");

      }
   }

I need this program to ask the user to enter their first and last name, last years production and this years production. based on this input you decide if they get a bonus. if any amount less than zero is entered it should display invalid number. i have to create a method called CalculateBonus. This method will return a double back by calling the method. This method takes two parameters: LastYearProduction and ThisYearProduction(both are double) the method will calculate bonus as follows: 1000 or fewer bonus is 25, 1000 to 3000 bonus is 50, 3001 to 6000 bonus is 100, 6001 and up bonus is 200. Main method should get name, and both productions. check production add in and if less than 0 then it will display invalid and shut down, otherwise it will check to see if ThisYearProduction is greater than LastYearProduction. If it is greater; call the Calculate bonus method and assign the result a suitable variable. Then print the employee name and bonus in a suitable message. Otherwise display no bonus.

I been at this program for 3 days. I cannot get it to work and I've tried many things. I'm new to C by the way. Please help me fix my code, so I can get some rest.

Recommended Answers

All 3 Replies

First, I've reformatted your code with the proper indentation, this will make it easier to see why you have some errors:

using System;

public class BonusProduction {
    public static void Main() {

        int FName, LNAME;
        double LastYearProduction;
        double ThisYearProduction;

        Console.WriteLine("Enter your first name");
        FName = Convert.ToInt32(Console.ReadLine());
        Console.ReadLine("Enter your last name");
        LNAME = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Enter Last Year's Production");
        LastYearProduction = Convert.ToInt32(Console.ReadLine());
        if (LastYearProduction < 0)
            Console.WriteLine("Invalid");
        {
            Console.WriteLine("Enter This Year's Production");
            ThisYearProduction = Convert.ToInt32(Console.ReadLine());
            if (ThisYearProduction < 0)
                Console.WriteLine("Invalid");
        }
    }

    public static void CalculateBonus()
      {
       
      int FName,
      LNAME;
      double LastYearProduction;
      double ThisYearProduction;
       
       
      bool bonus;
       
      bonus = ThisYearProduction - LastYearProduction;
      if(bonus <= 1000)
          Console.WriteLine("Your bonus is $25");
      else if(bonus >= 1000 && <= 3000)
          Console.WriteLine("Your bonus is $50");
      else if(bonus > 3000 && <= 6000)
          Console.WriteLine("Your bonus is $100");
      else if(bonus > 6000)
          Console.WriteLine("your bonus is $200");
      {
      }
      else
      Console.WriteLine("Invalid");

    }
}

I'm going to cover why your code doesn't work and some programming style issues.

Line 6: Many style guides frown on declaring more than one variable per line. The reason is that it makes it hard to find some of the variables. Put all declarations on their own line.

Lines 6-8: It is common practice to name variables with what is known as Camel Case (Wiki uses lower camel case) and methods/properties with Pascal Case (same link, also known as upper camel case). The first letter of variables is always lower case, methods/properties upper case. This helps the readability of your code as it's easy to distinguish between the two just be looking at the first letter.

Line 11: You are getting someones first name (at least that is what you asked for) and then you are trying to convert it to an integer.

Line 12: You are using Console.ReadLine() to try to display text.

Line 13: Same as Line 11.

Line 15: You are converting the input to an integer and then assigning it to a double. While this will work, be aware that if the user types in, for example, "103.4" it won't work. You can use Convert.Double() or Double.Parse() if you really meant to get the values after the decimal point.

Line 16: You should always strive to use { and } around code that belongs to a control statement (if, while, do, case, switch, etc.) unless it's all one one line (For example if (x > 3) x = 3; . Google has a good document about style and I always recommend that programmers read Code Complete.

Lines 18-23: You've declared this as a block of code, but it's a 'floating' block (meaning it's not required to be a block as it isn't part of any control statement). Based on the requirements you listed, this block should have a conditional before it. Are you missing an 'else'?

Line 20: Same as Line 15.

Line 24: You haven't met the requirements as you specified as you don't have a call to CalculateBonus nor do you display the bonus here.

Line 26: This does not meet the requirements you specified. It needs to return a double and accept two input parameters that are both doubles.

Lines 29-32: Same as Lines 6-8.

Line 35: You declare the bonus as a boolean (true/false) value. Are they going to get a true/false as their bonus?

Line 37: You are performing calculation on two variables that have not had values assigned to them (ThisYearProduction, LastYearProduction). Refer back to Line 26 for where the values should come from. You are also trying to assign a number to a boolean (bonus) variable.

Lines 38-45: Again the { and } usage.

Lines 46-47: You declare an empty block. While C# won't care, why are you doing this?

Line 48: This else has no corresponding if. This would be clearer if you used { and ] to enclose blocks after control statements. The compiler will not compile this line.

Lines 39, 41, 43, 45: The requirements ask you to return a double, not display the bonus (at this point).

Line 51: You end the method without returning a value. While this is acceptable for the method you defined (you did declare it void), it doesn't met the requirements.


Last, but not least, this is C#, not C (or even C++). While they are all similar, they have very important differences :)

commented: First class answer :) +7
using System;

public class BonusProduction 
{
    public static void Main() 
{

        string fName;
        string lNAME;
        double lastYearProduction;
        double thisYearProduction;

        Console.WriteLine("Enter your first name");
        fName = (Console.ReadLine());
        Console.WriteLine("Enter your last name");
        lNAME = (Console.ReadLine());
        Console.WriteLine("Enter Last Year's Production");
        lastYearProduction = Convert.ToDouble(Console.ReadLine());
        {if} (lastYearProduction < 0)
            Console.WriteLine("Invalid");
        { else }
            Console.WriteLine("Enter This Year's Production");
            thisYearProduction = Convert.ToDouble(Console.ReadLine());
         { else } 
                if (thisYearProduction < 0)
                Console.WriteLine("Invalid");
        }
    }

    public static CalculateBonus()
      {
       
      int fName;
      int lNAME;
      return lastYearProduction;
      return thisYearProduction;
       
       
      double bonus;
       
      bonus = thisYearProduction - lastYearProduction;
      { if }(bonus <= 1000)
          Console.WriteLine("Your bonus is $25");C
      { else if }(bonus >= 1000 && <= 3000)
         return bonus = CalculateBonus;
      { else if }(bonus > 3000 && <= 6000)
          return bonus = CalculateBonus;
      { else if }(bonus > 6000)
          return bonus = CalculateBonus;
      
{ 
else 
}
      Console.WriteLine("Invalid");

    }
}

I do not understand returning and calling please help by showing me how.

I'm afraid you've misunderstood what Momerath was saying about using braces around if()/else statements, with results that are effectively meaningless. Rather than this:

{if} (lastYearProduction < 0)
            Console.WriteLine("Invalid");
        { else }
            Console.WriteLine("Enter This Year's Production");
            thisYearProduction = Convert.ToDouble(Console.ReadLine());

Momerath meant for you to do something more like this:

if (lastYearProduction < 0) {
            Console.WriteLine("Invalid");
        }
        else {
            Console.WriteLine("Enter This Year's Production");
            thisYearProduction = Convert.ToDouble(Console.ReadLine());
        }

To put it another way: an if() expression consists of three basic parts, the word 'if' followed by a conditional statement in parentheses; followed by either a single line of code or a block of code inside of curly braces; then an optional 'else' followed by the same sort of body as an if, but representing the code that should run if the condition is false. A BNF description of the syntax would be

<if-statement> ::= 'if' '(' <conditional> ')' '{' <body> '}' 'else' '{' <body> '}'

I don't know if this helps any, but it should clarify Momerath's earlier posting.

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.