Question Unhandled exception string problem user input

sandcounter

New member
Joined
Oct 6, 2015
Messages
3
Programming Experience
Beginner
Hello all: I'm new to coding and this forum, using c# visual studio 2012. I am trying to get this code to read the user input and read it on a separate line. I get an un-handled exception, not sure why. Thank You
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Lesson_5_Project
{
    class EmpApp
    {
        static void Main()
        {
            
            char last;


            DisplayInstructions();
            last = GetName("Last:");


            DisplayName(last);            




            // Object: myEmployee
            Employee myEmployee = new Employee();


            // Instantiate the object
            myEmployee.GetPaySchedule();


            Console.ReadLine();
        }
        public static void DisplayInstructions()
        {
            Console.WriteLine("This program will calculate the employee's take-home pay.");
            /*Console.Write("Enter Last name: {0}", last);
            Console.ReadKey();*/
        }
        public static char GetName(string name1)
        {
            string inValue;
            char Name;


            Console.Write("Enter {0}", name1);
            inValue = Console.ReadLine();
            Name = char.Parse(inValue);/*An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
                                           Additional information: String must be exactly one character long.*/


            return Name;
        }
        public static void DisplayName(char last)
        {
            Console.WriteLine("Last Name: {0}", last);


        }


    }
}
 
I get an un-handled exception, not sure why
As message says char.Parse will accept only a single character string, and you are passing it a longer string. Should 'name' really be type char (one character)?
 
Thank You, I have updated the code to string, but now I can't get it to read the "employee name", I know I'm missing something somewhere, but not sure what. Here is my new code. Any help is very much appreciated.
namespace Lesson_5_Project
{
    class Employee
    {
        
        private string lname/*employeeName*/;
        private char inValue;
        private double payRate;
           
        public Employee()
                     
        {
        }
        /*public void GetEmployeeName()
        {
            EmployeeName();
            Console.WriteLine("Employee Name: {0}", lname);
                
            }
        }
            public void EmployeeName
        {
            
            get
            {
                return employeeName;
            }
            set
            {
                employeeName = inValue;
            }
}
            //employeeName = lname;
            //Console.WriteLine("Employee Name: {0}", lname);
            
        }*/
        public void GetPaySchedule()
        {
            
            EmployeePaySchedule();


                        
            if (inValue == 'S')
            {


                GetSalary();
                Console.WriteLine("Salaried Employee Income: $" + payRate);
                GetResults();


            }
            else if (inValue == 'H')
            {


                HourlyPaySchedule();
               
                Console.WriteLine();
                Console.WriteLine("Hourly Gross Income: $" + payRate);
                GetResults();


            }
            else
            {


                Console.WriteLine("Invalid entry, Please enter either H or S" +
                                  "\n-------------------------" +
                                  "\nH - Hourly               " +
                                  "\nS - Salary               " +
                                  "\n-------------------------");
                
            }
        }


                
        public void EmployeePaySchedule()
        {
            string employeeName;
            


            Console.Write("Enter Last Name: ");
            employeeName = Console.ReadLine();
            


            Console.WriteLine("Please enter one of the following: " +
                              "\n-------------------------" +
                              "\nH - Hourly               " +
                              "\nS - Salary               " +
                              "\n-------------------------");
            inValue = char.Parse(Console.ReadLine());
        }




        public void GetSalary()// Method: User enters in for Salary
        {


            Console.Write("Enter your total salary: $");
            payRate = double.Parse(Console.ReadLine());
        }




        public void HourlyPaySchedule()// Method: Method for making a decision if the employee works less than 40 or more than 40 hours.
        {


            Console.Write("Employee Hourly Wage: $");
            payRate = double.Parse(Console.ReadLine());


            Console.Write("Hours Worked: ");
            double hours = (double.Parse(Console.ReadLine()));


            double oT = (hours - 40);


            if (oT <= 0)
            {
                oT = 0;
            }
            else
            {
                oT = hours - 40;


                Console.Write("Overtime Hours= {0}", oT);
            }
            // Regular Pay
            if (hours <= 40)
            {
                payRate = (payRate * hours);
            }
            else
            {
                payRate = ((oT * 1.5 + 40) * payRate);
            }
        }
        /*public string EmployeeName
        {
            get
            {
                return employeeName;
            }
            set
            {
                employeeName = value;
            }
}*/
                  


        public void GetResults()// Method: Output for calculating the Net Income with deductions Fed Tax, Retirement, and Social Sec.
        {
            string lname;
            
            // Declare constant variables
            const double FED_TAX = .18;
            const double RETIRE_CONTRIB = .10;
            const double SOC_SEC = .06;
            double calcFedTax, calcRetire, calcSocSec, calcpayRate;


            lname = employeeName;
                
            // Calculations
            calcFedTax = (payRate * FED_TAX);
            calcRetire = (payRate * RETIRE_CONTRIB);
            calcSocSec = (payRate * SOC_SEC);
            calcpayRate = (payRate - (calcFedTax) - (calcRetire) - (calcSocSec));




            //Method: WriteLine Output


            Console.WriteLine("Employee Name: {0}", employeeName);//NOT READING EMPLOYEE NAME???
            Console.WriteLine("Federal Tax: " + calcFedTax);
            Console.WriteLine("Retirement Contribution: " + calcRetire);
            Console.WriteLine("Social Security: " + calcSocSec);
            Console.WriteLine("Net Income: " + calcpayRate);
            Console.ReadLine();
        /*}
         public override string ToString()
        {
            return "Employee Name: " + employeeName.ToString();*/


        }
        
     }
    }
 
I have formatted your code for you. Please do it for us in future. Also, please post only relevant code. We shouldn't have to wade through a load of irrelevant stuff to locate the issue when you can omit all the irrelevant stuff and point us directly to the issue.
 

Latest posts

Back
Top Bottom