New Posts  All Forums:Forum Nav:

Noob at C# - help :) ?

post #1 of 9
Thread Starter 
Hi there guys I am currently studying in C# and am stuck on creating a couple of programs I have gone through fine but am really stuck at the moment and any help is really appreciated !

(PS: I have a basic understanding of the code meanings and am still trying to figure it out as you read this. )

the question that I have been asked is to create a program using the following paragraph:

Problem Definition:

The consultation charge of a vet is obtained by multiplying the number of hours by the hourly rate
of $25.00. A program is to accept the number of hours from the keyboard, and output the
consultation charge to the screen,
(e.g. a consultation of 2 hours yields a charge of $50, i.e. 2 x 25 = 50
. a consultation of 1.5 hours yields a charge of $37.50, i.e. 1.5 x 25 = 37.5)

This is what I have made so far but I am very confused and don't know what to do.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assessment_ExerciseA
{
class Program
{
static void Main(string[] args)
{
//session 02 - Assessment ExerciseA
decimal hrs;
decimal consultation;
Console.WriteLine("80");
hrs = decimal.Parse(Console.ReadLine());
consultation = (hrs = 32)*25;
Console.WriteLine("consultation =" + hrs.ToString("N2"));
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
}
}

Thanks for your help in advanced smile.gif + rep to the best answer!
post #2 of 9
Something like this:
Code:
static double rate = 25;
        static void Main(string[] args)
        {
            Console.WriteLine("Enter hours, q to exit");

            string line;
            while ((line = Console.ReadLine()) != "q") 
            {
                try
                {
                    double hours = Convert.ToDouble(line);
                    Console.WriteLine("Charge: " + (hours * rate) + "$");
                }
                catch (FormatException fe) 
                {
                    Console.WriteLine("Invalid number"); 
                }
            }
        }
Haswell
(21 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 4770K @ 4Ghz Gigabyte Z87X-UD5H Sapphire Radeon R9 290 Vapor-X G.Skill RipjawsZ 4x4GB 2400Mhz 
Hard DriveHard DriveHard DriveHard Drive
Crucial M4 126GB Crucial MX100 4x512GB WD Caviar Green EZRX Crucial M500 120GB 
Optical DriveCoolingOSMonitor
Samsung DVD Corsair Hydro H105 Windows 7 Ultimate Acer B276HK 
MonitorMonitorKeyboardPower
Dell P2314H Samsung SA700 Logitech G710+ Seasonic P-660 XP2 
CaseMouseMouse PadAudio
Fractal Design Arc Midi R2 Razer Deathadder 2013 Mionix Sargas 900 Asus Xonar DX/XD 
Audio
Beyerdynamics Custome One Pro White 
  hide details  
Reply
Haswell
(21 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 4770K @ 4Ghz Gigabyte Z87X-UD5H Sapphire Radeon R9 290 Vapor-X G.Skill RipjawsZ 4x4GB 2400Mhz 
Hard DriveHard DriveHard DriveHard Drive
Crucial M4 126GB Crucial MX100 4x512GB WD Caviar Green EZRX Crucial M500 120GB 
Optical DriveCoolingOSMonitor
Samsung DVD Corsair Hydro H105 Windows 7 Ultimate Acer B276HK 
MonitorMonitorKeyboardPower
Dell P2314H Samsung SA700 Logitech G710+ Seasonic P-660 XP2 
CaseMouseMouse PadAudio
Fractal Design Arc Midi R2 Razer Deathadder 2013 Mionix Sargas 900 Asus Xonar DX/XD 
Audio
Beyerdynamics Custome One Pro White 
  hide details  
Reply
post #3 of 9
Thread Starter 
Thanks +rep for the help !
post #4 of 9
Quote:
Originally Posted by Jiiks View Post

Something like this:
Code:
static double rate = 25;
        static void Main(string[] args)
        {
            Console.WriteLine("Enter hours, q to exit");

            string line;
            while ((line = Console.ReadLine()) != "q") 
            {
                try
                {
                    double hours = Convert.ToDouble(line);
                    Console.WriteLine("Charge: " + (hours * rate) + "$");
                }
                catch (FormatException fe) 
                {
                    Console.WriteLine("Invalid number"); 
                }
            }
        }


Code:
//Declare the constant rate outside of your function to ensure global scope
static double rate = 25;
        //mainapp structure
        static void Main(string[] args)
        {
            //You should be using an IO stream to get the  number of hours
            Console.WriteLine("Enter hours, 0 to exit");

            //Declaring line as a string for easy input
            string line;
            while ((line = Console.ReadLine()) != "0")
            {
                try
                {
                    //converts the value to a double because we need to have factional hours
                    double hours = Convert.ToDouble(line);

                    //writing to the console our answer
                    Console.WriteLine("Charge: " + (hours * rate) + "$");
                }

                /*Since you are doing basic programming, this last part is a the
                catch statement of the TryCatch statement.  This is our IO exception
                statement that handles all the incorrect inputs into the line variable.*/
                catch (FormatException fe)
                {
                    //Writes the string directing the user
                    Console.WriteLine("Invalid number");
                }
            }
        }

        /*If your instructor doesn't not require any exception handling you can just remove the
        try catch statement by removing the calls and the brackets. But clearly handling your
        own IO error is best practice.*/
post #5 of 9
This may be a personal opinion, but it's something I feel strongly about. Try Catch is overused by many developers, it's generally considered to be a lazy practice. It's used in many cases where is it not needed or not necessary. Try Catch should never be used to control the normal flow and operation of a program. Their intended purpose is for errors that are unexpected and uncommon. The best examples of when to use them are for IO exceptions or network related exceptions. Things such as that which the developer has no control over and happen entirely unexpectedly. A simple check on the value of a string does not necessitate the use of Try Catch.

Deal with things properly instead of just relying on the Try Catch. In a larger program working this way is much easier to debug and find issues. Try Catch has a tendency to hide problems, not to mention that it's much more difficult to interpret what a previous developer might have intended.

Something like this is a much better practice. The end result is exactly the same, but when written this way there is no question about what the intended path is supposed to be:
Code:
        static void Main(string[] args)
        {
            double rate = 25;
            Console.WriteLine("Enter hours, q to exit");

            string line;
            while ((line = Console.ReadLine()) != "q")
            {
                double hours = 0;
                if (double.TryParse(line, out hours))
                {
                    Console.WriteLine("Charge: " + hours * rate + "$");
                }
                else
                {
                    Console.WriteLine("Invalid number"); 
                }
            }

            Console.WriteLine();
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }

Edited by wedge - 3/2/14 at 9:10pm
Death Star
(21 items)
 
Darksaber
(11 items)
 
 
CPUMotherboardGraphicsRAM
Athlon II x2 245 Asus M3A78 Radeon HD6570 1GB Mushkin Silverline 2GB DDR2  
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 120GB Samsung Blu-Ray Windows 7 Samsung 46" DLP 
PowerCaseOther
Silverstone Strider Essentials 400W Silverstone Milo ML03B Hauppage WinTV 1250 
  hide details  
Reply
Death Star
(21 items)
 
Darksaber
(11 items)
 
 
CPUMotherboardGraphicsRAM
Athlon II x2 245 Asus M3A78 Radeon HD6570 1GB Mushkin Silverline 2GB DDR2  
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 120GB Samsung Blu-Ray Windows 7 Samsung 46" DLP 
PowerCaseOther
Silverstone Strider Essentials 400W Silverstone Milo ML03B Hauppage WinTV 1250 
  hide details  
Reply
post #6 of 9
Quote:
Originally Posted by wedge View Post

This may be a personal opinion, but it's something I feel strongly about. Try Catch is overused by many developers, it's generally considered to be a lazy practice. It's used in many cases where is it not needed or not necessary. Try Catch should never be used to control the normal flow and operation of a program. Their intended purpose is for errors that are unexpected and uncommon. The best examples of when to use them are for IO exceptions or network related exceptions. Things such as that which the developer has no control over and happen entirely unexpectedly. A simple check on the value of a string does not necessitate the use of Try Catch.

Deal with things properly instead of just relying on the Try Catch. In a larger program working this way is much easier to debug and find issues. Try Catch has a tendency to hide problems, not to mention that it's much more difficult to interpret what a previous developer might have intended.

Something like this is a much better practice. The end result is exactly the same, but when written this way there is no question about what the intended path is supposed to be:
Code:
        static void Main(string[] args)
        {
            double rate = 25;
            Console.WriteLine("Enter hours, q to exit");

            string line;
            while ((line = Console.ReadLine()) != "q")
            {
                double hours = 0;
                if (double.TryParse(line, out hours))
                {
                    Console.WriteLine("Charge: " + hours * rate + "$");
                }
                else
                {
                    Console.WriteLine("Invalid number"); 
                }
            }

            Console.WriteLine();
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }

I agree. Exceptions being thrown for the sake of control flow is a huge performance hit. You should ideally have more try...finally than try...catch.
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
post #7 of 9
Quote:
Originally Posted by tompsonn View Post

I agree. Exceptions being thrown for the sake of control flow is a huge performance hit.

Yeah, I didn't even mention the performance penalty
Death Star
(21 items)
 
Darksaber
(11 items)
 
 
CPUMotherboardGraphicsRAM
Athlon II x2 245 Asus M3A78 Radeon HD6570 1GB Mushkin Silverline 2GB DDR2  
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 120GB Samsung Blu-Ray Windows 7 Samsung 46" DLP 
PowerCaseOther
Silverstone Strider Essentials 400W Silverstone Milo ML03B Hauppage WinTV 1250 
  hide details  
Reply
Death Star
(21 items)
 
Darksaber
(11 items)
 
 
CPUMotherboardGraphicsRAM
Athlon II x2 245 Asus M3A78 Radeon HD6570 1GB Mushkin Silverline 2GB DDR2  
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 120GB Samsung Blu-Ray Windows 7 Samsung 46" DLP 
PowerCaseOther
Silverstone Strider Essentials 400W Silverstone Milo ML03B Hauppage WinTV 1250 
  hide details  
Reply
post #8 of 9
At least he defined object for the catch (FormatException fe). =) I also agree, i see try catch way too much. It's either laziness or ignorance. Sometimes I have no clue how else to do it.
Zev's Comp
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K Sandy Bridge 3.3GHz GIGABYTE GA-Z68X-UD3H-B3 LGA 1155 Intel Z68 HDM... GeForce GTX 750 Ti G.SKILL Ripjaws X Series 8GB 
Hard DriveHard DriveHard DrivePower
1TB HDD 64GB SSD (Used for SRT) 500 GB. Antec BP550 Plus 550W Continuous Power ATX12V V... 
Case
COOLER MASTER ELITE 335 RC-335-KKN1-GP Black S... 
  hide details  
Reply
Zev's Comp
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K Sandy Bridge 3.3GHz GIGABYTE GA-Z68X-UD3H-B3 LGA 1155 Intel Z68 HDM... GeForce GTX 750 Ti G.SKILL Ripjaws X Series 8GB 
Hard DriveHard DriveHard DrivePower
1TB HDD 64GB SSD (Used for SRT) 500 GB. Antec BP550 Plus 550W Continuous Power ATX12V V... 
Case
COOLER MASTER ELITE 335 RC-335-KKN1-GP Black S... 
  hide details  
Reply
post #9 of 9
Quote:
Originally Posted by Mrzev View Post

At least he defined object for the catch (FormatException fe). =) I also agree, i see try catch way too much. It's either laziness or ignorance. Sometimes I have no clue how else to do it.

There's only one place where try...catch unfiltered is acceptable and that's around your entire program message loop (or Application.Run in C#). BUT its only for error logging and the catch block MUST contain "throw" so you rethrow it once you're done logging the error.
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming