none
C# programming trouble (not visual C#)

    Question

  • I'm trying to write a program that prompts the user for the following information:

    • Name
    • Social security number
    • Hourly pay rate
    • Number of hours worked

    And displays the following:

    • All the input data
    • Gross pay, defined as hourly pay rate times hours worked
    • Federal withholding tax, defined as fifteen percent of the gross pay
    • State withholding tax, defined as five percent of the gross pay
    • Net pay, defined as gross pay minus taxes

    This is where I'm at and I'm getting errors and unsure how to finish the rest of the code:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace Payroll

         class Program

         {

              private static string name;

                     private static string ssn;

                     private static string hourly rate;

              private static string hours worked; static void Main(string[] args)

                    {

                   Console.Write("Name: ");

                   name = Console.Readline();

                  Console.Write("SSN: ");

                  ssn = Console.Readline();              Console.Write("Hourly Rate: ");

                  hourly rate = Console.Readline();              Console.Write("Hours Worked: ");

                             hours worked = Console.Readline();

                      }

    Tuesday, July 03, 2012 12:30 AM

Answers

  • You cannot multiply strings. You need to parse the string the user entered, check that it's indeed a number, and get its value. Only when you have converted the strings to numeric values can you do arithmetic operations on them.

    For example:

    int nbEggs;
    do
    {
        Console.WriteLine("How many eggs do you want ?");
        string userInput = Console.ReadLine();
    } while(!int.TryParse(userInput, out nbEggs));
    Console.WriteLine("That will be {0} euros", nbEggs * 0.2);

    Tuesday, July 03, 2012 1:14 PM
  • C# is a case sensitive 

    so use right variable name like grossPay instead of gross pay.

    ok my dear


    sonesh

    Tuesday, July 03, 2012 5:31 AM
  • You cannot multiply strings. You need to parse the string the user entered, check that it's indeed a number, and get its value. Only when you have converted the strings to numeric values can you do arithmetic operations on them.

    For example:

    int nbEggs;
    do
    {
        Console.WriteLine("How many eggs do you want ?");
        string userInput = Console.ReadLine();
    } while(!int.TryParse(userInput, out nbEggs));
    Console.WriteLine("That will be {0} euros", nbEggs * 0.2);


    As Louis.fr stated, you cannot multiply strings, they need to be cast to a numeric type, although i'm not so sure Integer would be the proper type there Louis, we may want the decimals... (Not to bash on your snippet there because the strategy behind it has good intention, just one minor thing in there.)

    No, I'm not looking for someone to just hand me the correct code. I managed to get a little further, but the part I don't understand is coding the taxes  and totals in.

    I'm here:

    Console.WriteLine("\nPayroll Summary for:")

    Console.WriteLine(name);

    Console.WriteLine("\n" + "hourly rate" + "hours worked");

    Console.Writeline("Gross Pay: ");

    But then I get errors when I add:

    gross pay = hourly rate * hours worked;


    And again JasonD_12:

    gross pay = hourly rate * hours worked;

    You're going to get hours here for more than just trying to multiply strings. Re-read my last reply to you, I see all 3 variables in here with spaces in between them, for a variable name/identifier, you CANNOT have a space in there :)

    You need to get out of that misconception fast if you want to progress as a programmer. (I would further urge you to see what's in your errorlist, as it should give you a good indication on how it's viewing your code with spaces in variable identifiers. Learning how to debug your own code and fix it is a valuable skill in itself. What if you're programming without network access? No MSDN forums to help you out. And I don't mean to scare you by being a realist here haha *joke*)

    Good luck :)


    If a post helps you in any way or solves your particular issue, please remember to use the Propose As Answer option or Vote As Helpful
    Visit the Forum: TechLifeForum





    Wednesday, July 04, 2012 4:42 AM

All replies

  • Ok I figured out the errors, but still stuck on how to finish the code for the program.
    Tuesday, July 03, 2012 12:39 AM
  • I have a few questions and notes for you:

    - If it's not Visual C# what is it?
    - Variable names cannot have spaces in them:

    private static string hours worked;
    private static string hourly rate;


    -C# is case sensitive

    Console.Readline();

    SHOULD be:

    Console.ReadLine();

    Ok I figured out the errors, but still stuck on how to finish the code for the program.

    This is meant to be a help forum on issues you're having, so if you don't tell us what you're having trouble with to complete the program it's not much help if we just write down the code for you. If this was a homework project for you then i'd probably be advised not to do that period and write any direct code for you, but to help you understand how to fix your own code.

    Do not get the misconception that this is a project helper forum, this is for help with programming in C# itself for anything you don't understand, which does not include not knowing how to create a program :)

    To get you on the right path though, I would use the input assigned to those variables inside of Functions which return the calculated values you're missing out here. Gross Pay, etc... return the value of the hours the person worked multiplied by the rate and write it to the console if you need to show that output, or do something else with it. It is pretty simple, this would be Function basics, so if you're unaware on how Functions work yet, i'd highly advise taking self-initiative to review that kind of material. There's lots on MSDN documentation as well, and it should be the easiest thing to find out there. 

    If, then, you have any issues, THEN come to the MSDN forums, but I like to see where people actually try to learn on their own instead of asking for the easiest way out.

    I help people learn, but I don't help write people's projects.

    ~Ace


    If a post helps you in any way or solves your particular issue, please remember to use the Propose As Answer option or Vote As Helpful
    Visit the Forum: TechLifeForum



    Tuesday, July 03, 2012 1:27 AM
  • No, I'm not looking for someone to just hand me the correct code. I managed to get a little further, but the part I don't understand is coding the taxes  and totals in.

    I'm here:

    Console.WriteLine("\nPayroll Summary for:")

    Console.WriteLine(name);

    Console.WriteLine("\n" + "hourly rate" + "hours worked");

    Console.Writeline("Gross Pay: ");

    But then I get errors when I add:

    gross pay = hourly rate * hours worked;

    • Edited by JasonD_12 Tuesday, July 03, 2012 2:36 AM
    Tuesday, July 03, 2012 2:32 AM
  • C# is a case sensitive 

    so use right variable name like grossPay instead of gross pay.

    ok my dear


    sonesh

    Tuesday, July 03, 2012 5:31 AM
  • You cannot multiply strings. You need to parse the string the user entered, check that it's indeed a number, and get its value. Only when you have converted the strings to numeric values can you do arithmetic operations on them.

    For example:

    int nbEggs;
    do
    {
        Console.WriteLine("How many eggs do you want ?");
        string userInput = Console.ReadLine();
    } while(!int.TryParse(userInput, out nbEggs));
    Console.WriteLine("That will be {0} euros", nbEggs * 0.2);

    Tuesday, July 03, 2012 1:14 PM
  • You cannot multiply strings. You need to parse the string the user entered, check that it's indeed a number, and get its value. Only when you have converted the strings to numeric values can you do arithmetic operations on them.

    For example:

    int nbEggs;
    do
    {
        Console.WriteLine("How many eggs do you want ?");
        string userInput = Console.ReadLine();
    } while(!int.TryParse(userInput, out nbEggs));
    Console.WriteLine("That will be {0} euros", nbEggs * 0.2);


    As Louis.fr stated, you cannot multiply strings, they need to be cast to a numeric type, although i'm not so sure Integer would be the proper type there Louis, we may want the decimals... (Not to bash on your snippet there because the strategy behind it has good intention, just one minor thing in there.)

    No, I'm not looking for someone to just hand me the correct code. I managed to get a little further, but the part I don't understand is coding the taxes  and totals in.

    I'm here:

    Console.WriteLine("\nPayroll Summary for:")

    Console.WriteLine(name);

    Console.WriteLine("\n" + "hourly rate" + "hours worked");

    Console.Writeline("Gross Pay: ");

    But then I get errors when I add:

    gross pay = hourly rate * hours worked;


    And again JasonD_12:

    gross pay = hourly rate * hours worked;

    You're going to get hours here for more than just trying to multiply strings. Re-read my last reply to you, I see all 3 variables in here with spaces in between them, for a variable name/identifier, you CANNOT have a space in there :)

    You need to get out of that misconception fast if you want to progress as a programmer. (I would further urge you to see what's in your errorlist, as it should give you a good indication on how it's viewing your code with spaces in variable identifiers. Learning how to debug your own code and fix it is a valuable skill in itself. What if you're programming without network access? No MSDN forums to help you out. And I don't mean to scare you by being a realist here haha *joke*)

    Good luck :)


    If a post helps you in any way or solves your particular issue, please remember to use the Propose As Answer option or Vote As Helpful
    Visit the Forum: TechLifeForum





    Wednesday, July 04, 2012 4:42 AM