If condition

NewGuy91

New member
Joined
Jul 6, 2019
Messages
3
Programming Experience
1-3
I need help writing the appropriate code for my assignment this week.

I have most of the assignment complete. I just need to add in the choice to make the employee a W2 or 1099, by user input I'll show the code I have written so far. I know it's not perfect and could be simplified with some loop statements I just need guidance and a mentor in this field.


C#:
using System;

namespace Week2var
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to our Software Development Company!");   //displays welcome message

            Console.WriteLine("What is the employee's name?");                  // Ask for name input, sets variable
            var developerName = (Console.ReadLine());

            Console.WriteLine("What is the developer's address?");             // Ask for address, sets variable
            var developerAddress = (Console.ReadLine());

            Console.WriteLine("What is the developer's gross pay");           // Ask for gross pay, sets variable
            var developerGrossPay = int.Parse(Console.ReadLine());

            Console.WriteLine("What is the employee's name?");                  // Ask for name input, sets variable
            var developerName2 = (Console.ReadLine());

            Console.WriteLine("What is the developer's address?");             // Ask for address, sets variable
            var developerAddress2 = (Console.ReadLine());

            Console.WriteLine("What is the developer's gross pay");           // Ask for gross pay, sets variable
            var developerGrossPay2 = int.Parse(Console.ReadLine());

            Console.WriteLine("What is the employee's name?");                  // Ask for name input, sets variable
            var developerName3 = (Console.ReadLine());

            Console.WriteLine("What is the developer's address?");             // Ask for address, sets variable
            var developerAddress3 = (Console.ReadLine());

            Console.WriteLine("What is the developer's gross pay");           // Ask for gross pay, sets variable
            var developerGrossPay3 = int.Parse(Console.ReadLine());

            var taxes = (developerGrossPay * .007);                          // Calculates taxes paid
            var taxes2 = (developerGrossPay2 * .007);
            var taxes3 = (developerGrossPay3 * .007);

            var annualPay = (developerGrossPay * 12);                         // calculates annual pay
            var annualPay2 = (developerGrossPay2 * 12);
            var annualPay3 = (developerGrossPay3 * 12);

            var annualTaxes = (taxes * 12);                                 // Calculates annual taxes
            var annualTaxes2 = (taxes2 * 12);
            var annualTaxes3 = (taxes3 * 12);

            Console.WriteLine(developerName);                // Displays developers information
            Console.WriteLine(developerAddress);
            Console.WriteLine(developerGrossPay);
            Console.WriteLine(taxes);
            Console.WriteLine(annualPay);
            Console.WriteLine(annualTaxes);

            Console.WriteLine(developerName2);                // Displays developers information
            Console.WriteLine(developerAddress2);
            Console.WriteLine(developerGrossPay2);
            Console.WriteLine(taxes2);
            Console.WriteLine(annualPay2);
            Console.WriteLine(annualTaxes2);

            Console.WriteLine(developerName3);                // Displays developers information
            Console.WriteLine(developerAddress3);
            Console.WriteLine(developerGrossPay3);
            Console.WriteLine(taxes3);
            Console.WriteLine(annualPay3);
            Console.WriteLine(annualTaxes3);
        }
    }
}
 
Last edited by a moderator:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,275
Location
Sydney, Australia
Programming Experience
10+
What do you actually need help with? You obviously know how to prompt the user for input and you obviously know how to read that input into a variable, so you can do that for this particular value without our help. It would make sense to specify the valid values in the prompt. Once you have that value, it's simply a matter of writing an if...else or maybe a switch statement based on the value of that variable. I imagine that you have been taught at least the first, so you can at least have a go at it and see what you come up with. If it doesn't work, that would be the time to ask a question, showing what you did and what happened when you did it.

That said, I'm not sure what the requirements of your assignment are but you will have issues there if the user doesn't enter valid input. For instance, if a nonnumerical value is entered for any of the gross pay fields, your app will crash on the int.Parse calls. Students are often allowed to assume valid input for early learning purposes but not later on and never in the real world. In situations like that, you should always validate the data. You would normally start a loop (do or while) and prompt the user inside the loop. You'd then validate the data and either break out of the loop if it's valid or loop and prompt again. You can use int.TryParse to validate and convert in one operation.

This choice of 'W2' or '1099' is a similar case. Ideally you would specify in the prompt that you expect one of those two values and then you'd check the input to see that it is one of those two values. If it's not, you'd loop back to the prompt again, otherwise you would break out of the loop and use the value.
 

NewGuy91

New member
Joined
Jul 6, 2019
Messages
3
Programming Experience
1-3
I tried a few approaches I just didn't save any of them. Thanks for your comment. I'll try again.

I guess what I'm asking is how to use and if condition to take the user input of w2 or 1099 and apply it to the taxing principal. I get scattered brained when deadlines come up and can't think clearly and over complicate things. I'm just looking for some guidance man.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,275
Location
Sydney, Australia
Programming Experience
10+
I guess what I'm asking is how to use and if condition to take the user input of w2 or 1099 and apply it to the taxing principal.
Say it out loud and that basically tells you what the code should look like. If the value entered by the user is "W2" then do one thing and if the value entered by the user is "1099" then do a different thing.
I get scattered brained when deadlines come up and can't think clearly and over complicate things.
Which is exactly why you shouldn't try to go straight from a vague idea in your head to working code. People seem to think that they should be able to do that and, if they can't, the alternative is to get someone else to tell them what to do. What you should be doing is planning your code before you write it. The more experienced you get, the more complex things have to be before you get to that point but that's what experienced developers do all the time. The first step is to work out the logic involved, which is nothing at all to do with programming. You should be able to get the desired results with pen and paper yourself and explain clearly to someone with no prior knowledge of the problem how to do the same. Once you've done that, you can write pseudo-code that corresponds to your logic, which has nothing specifically to do with C#. Finally, write actual code in your language of choice to implement the pseudo-code. If you haven't done all that then you haven;t really trued to solve your problem. Don't worry, you're not the only one. You need to develop those multi-step skills though, or you'll never progress beyond the simplest of programming problems.
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
205
Location
Virginia Beach, VA
Programming Experience
10+
This is part of the reason why old school programming teachers insist on teaching flowcharting and pseudo-code writing before even writing the first line of code. Granted, back then, they were used to having limit access to computers so you had to have all your ducks in a row before you even got to a keyboard. My dad used to tell me about he'd have to submit a stack of punch cards, then get a print out the following day. Debugging involved checking the results of the print outs. The smart programmer back then would leave themselves lots of trace statements so that they could confirm the logic that the code was following and the intermediate computed values matched what they were expecting to see.
 

Sheepings

Senior Programmer
Joined
Sep 5, 2018
Messages
159
Location
UK
Programming Experience
10+
You received some very good advice form John above. Consider an if statement as a simple question that gives a simple answer, providing the condition of your opening statement is met. It will execute its expression for said condition, if the condition is not met, it will execute in an else section of the statement should you provide one. What you should do before writing code, is to jot down on paper, a few boxes, and semantically draw your logic in boxes and mark what each box is, and what its intended purpose is.
If.png
Logic is simple, and it sounds like you're over-thinking this; probably because of your heightened stress. If you have an understanding of code, and it appears that you do. Jot down just like a drawing of what it is you're trying to do, and then apply your code teachings to that drawing and you'll devise a strategy which you can formulate into written code, similar to that little chart above. Something else which was touched on above is your int.parse. I'm not a fan of it. Here is what I posted on another topic regarding int.parse vs int.tryparse ::
Also, be careful when using int.parse() - If you pass in a value and blindly assume its int, and its actually something like 5.5, you will be hit with a system format exception but instead, I would first prefer int.TryParse() which returns a bool indicating if the value is of type int. Be warned that it fails silently, whereas int.parse() will actually throw an exception if it fails to parse.

If you do use int.parse, consider wrapping that code in a try catch block with an exception of type system format exception and handle the error accordingly. No such try catch blocks are needed for tryparse, however you do need to check the bool state for a value of true if is has parsed.
You might also find the link in my signature helpful for conditional statements ifelse. And try not to stress.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,275
Location
Sydney, Australia
Programming Experience
10+
Here is what I posted on another topic regarding int.parse vs int.tryparse ::

You might also find the link in my signature helpful for conditional statements ifelse. And try not to stress.
The only time I would consider using int.Parse or Convert.ToInt32 or the like for user input, unless it's a beginner learning project where you're told to assume valid input, is if I'd validated the input previously, e.g. if you handle the Validating and Validated events of a TextBox then you could assume valid input in the second event handler because you would already have validated it in the first.
 

NewGuy91

New member
Joined
Jul 6, 2019
Messages
3
Programming Experience
1-3
Thanks for all the help. I have attempted some psudocode and used visual logic to try helping me put it into perspective. I'll try not to stress but truth is I'm already quite stressed. That's what puts my mind into a frantic mode where I cannot concentrate properly. Feeling better about it today though. Hopefully I can finish it and move on with my life. Sorry my amateur questions were such a pain to you @jmcilhinney
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,275
Location
Sydney, Australia
Programming Experience
10+
Sorry my amateur questions were such a pain to you @jmcilhinney
I don't need apologies. I just want people to do better in future, for their own sake. I want everyone who posts to be the best developer they can be. We all try to take the quick/easy way at times, myself included. I know from experience that doing so as a learner will only hurt you in the long run and often the short term as well.
 
Top Bottom