Click here to Skip to main content
14,601,122 members
Rate this:
Please Sign up or sign in to vote.
See more:
i am writing a program that will determine the gross pay for each of three employees and need to use a while loop to implement the inputs and calculations. I am missing something because it won't calculate.

i have my while (count<=3);

if (hours<=40)
pay=hours*rate;
else
Pay =(40*rate) + (hours -40) * (rate*1.5);


my console.WriteLine is ("Enter hourly rate: {0} " , rate);

What I have tried:

I took the rate out in the Console.WriteLine and i changed the {1}
Posted
Updated 18-Sep-19 20:00pm
Rate this:
Please Sign up or sign in to vote.

Solution 3

Two things:
Firstly, this line:
Console.WriteLine("Enter hourly rate: {0} " , rate);
doesn't input any values - it only Writes the current rate to the console for the user to read.
To get his response, you need to call Console.ReadLine and then convert his response to a number:
Console.Write("Enter hourly rate: ");
string input = Console.ReadLine();
double rate;
if (!double.TryParse(input, out rate))
   {
   Console.WriteLine($"\"{input}\" is not a valid number!\n Please try again.");
   }
else
   {
   ... your code to use the rate value goes here ...
   }


Secondly, why does your code do this:
while (count<=3);

Since the line ends with a semicolon, that terminates the while loop - and you would get a warning about that along the lines of "empty statement".
But when your code enters the loop, it will never exit - because count never changes!

Pay attention to Warnings as well as errors - as a beginner, you should never run code with warnings - because the compiler does know better than you and is almost certainly pointing out things you really, really didn't mean to do! (I run with "treat warnings as errors" enabled, and I am far from a beginner!)
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

Quote:
I am missing something because it won't calculate.

Please show your code as 1 consistent piece of code and use "code" button to keep formatting.
I think there is a problem here:
Pay =(40*rate) + (hours -40) * (rate*1.5);

If employee do less than 40 hours, missing hours are removed at 1.5 times the rate.
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100