Looping this

A

={ Advocated }=

Hi there, im in need to use a loop, im not sure exactly how to do it :S

########################
/*
* File: Payroll.c
* Program to calculate the wage of a user
*/
#include <stdio.h>

int main(int argc, char* argv[])
{
double hoursWorked = 0.00;
double hourlyRate = 0.00;
double pay = 0.00;
double totalAmount = 0.00;
int fieldCount = 0;

printf("Please enter hours worked: ");
fflush(stdout);
fieldCount = scanf("%lf", &hoursWorked);

if (fieldCount != 1)
{
printf("Please enter a number value.\n");
return 0;
}

while( hoursWorked < 0.0 )
{
return 0;
printf("The hours worked are %.2f\n", hoursWorked);
}

printf("Please enter hourly rate: ");
fflush(stdout);
scanf("%lf", &hourlyRate);
printf("The hourly rate is %.2f\n", hourlyRate);

printf("The total amount is %.2f\n", hoursWorked * hourlyRate);

return 0;
}
###########################


I know that theres no need to use a while loop, but i wanted to try
something

Ive got it so that if they dont enter a value, i.e 5, 3 and enter say
hello, then it says "please enter a number value"
but in that case, i want it to then return to "please enter hours worked"

ive tried a few things, none seemed to work

I tried extending the if statement:
if (fieldCount != 1)
{
printf("Please enter a number value.\n");
return 0;
}
else
{
printf("Please enter hours worked: ");
}

but then that just messed up the code :S

Help or advise would be apreciated
 
R

Richard Heathfield

={ Advocated }= said:
Hi there, im in need to use a loop, im not sure exactly how to do it :S

########################
/*
* File: Payroll.c
* Program to calculate the wage of a user
*/
#include <stdio.h>

int main(int argc, char* argv[])
{
double hoursWorked = 0.00;
double hourlyRate = 0.00;
double pay = 0.00;
double totalAmount = 0.00;
int fieldCount = 0;

This code looks strangely familiar.
printf("Please enter hours worked: ");
fflush(stdout);
fieldCount = scanf("%lf", &hoursWorked);

if (fieldCount != 1)
{
printf("Please enter a number value.\n");
return 0;

That quits from main(), which isn't what you want.


You want something like:

while(fieldCount != 1)
{
printf("Please enter hours worked: ");
fflush(stdout);
fieldCount = scanf("%lf", &hoursWorked);
if(fieldCount != 1)
{
printf("Error in input.\n");
}
}

This will keep looping until they give you a decent number.
}

while( hoursWorked < 0.0 )
{
return 0;
printf("The hours worked are %.2f\n", hoursWorked);

If you've returned from main(), you've ended the program (modulo a nit or
two which needn't concern us here). I explained this to you before, in
Undernet.

The printf, coming as it does after the end of the program, will simply not
happen.
 
S

Sheldon Simms

Hi there, im in need to use a loop, im not sure exactly how to do it

I know that theres no need to use a while loop, but i wanted to try
something

Ive got it so that if they dont enter a value, i.e 5, 3 and enter say
hello, then it says "please enter a number value"
but in that case, i want it to then return to "please enter hours worked"

Let's look at the code:
printf("Please enter hours worked: ");
fflush(stdout);
fieldCount = scanf("%lf", &hoursWorked);

if (fieldCount != 1)
{
printf("Please enter a number value.\n");
return 0; ^^^^^^^^
}

Look what you are doing. If the user enters something other than
a number, the program exits! The keyword 'return' means "leave
this function now". Since the function in question is main(),
the program exits. So the first thing to do is get rid of the
return statement:

/* Point A */
printf("Please enter hours worked: ");
fflush(stdout);
fieldCount = scanf("%lf", &hoursWorked);

if (fieldCount != 1)
{
printf("Please enter a number value.\n");
/* Point B */
}

Now just consider this small piece of code. What you want to
do is go from Point B to Point A, right? We can move in the
right direction by adding a while loop around this whole
piece of code. For now, we won't worry about ending the loop:

int loop_some_more = 1;

while (loop_some_more)
{
/* Point A */
printf("Please enter hours worked: ");
fflush(stdout);
fieldCount = scanf("%lf", &hoursWorked);

if (fieldCount != 1)
{
printf("Please enter a number value.\n");
/* Point B */
}
}

Try compiling your program with these changes. You'll see
that no matter what you type in, you'll be prompted to
enter hours worked again. If you type a number, you'll
just get another prompt. If you type a non-number, you'll
be asked to enter a number value and then you'll get another
prompt.

This infinite loop can be broken by assigning zero to the
loop_some_more variable somewhere inside the loop. You just
need to figure out where the right place to do it is. (You
might need to change the code inside the loop a little
bit.) Try that yourself and post your code again if you can't
figure it out.

-Sheldon
 
B

Barry Schwarz

Hi there, im in need to use a loop, im not sure exactly how to do it :S

Read the comments at the bottom, then come back and look at the code.
########################
/*
* File: Payroll.c
* Program to calculate the wage of a user
*/
#include <stdio.h>

int main(int argc, char* argv[])
{
double hoursWorked = 0.00;
double hourlyRate = 0.00;
double pay = 0.00;
double totalAmount = 0.00;
int fieldCount = 0;

You never use the initial values.

Obviously, you want to start your loop here so that, if it repeats,
this prompt will be reissued.
do {
printf("Please enter hours worked: ");
fflush(stdout);
fieldCount = scanf("%lf", &hoursWorked);

Since you are interested in validating the input, scanf is not your
best choice. Consider some possible inputs
1 - enter
2 - 1
3 - 1.1
4 - 1.1.1
5 - 1a
6 - 1 a
7 - a

scanf will let you catch 1 and 7. It will appear to work for 2-6 but
in cases 4-6 it will leave garbage in the input queue that may screw
up your next input.

A better approach is to read the input into a string with fgets() and
then use strtod() to both convert the data from character to double
and notify you of potential errors.

However, for this code, let's just stick to getting the loop to work.
if (fieldCount != 1)
{

At this point, scanf said there was no double. Case 1 or 7 could be
the cause.
printf("Please enter a number value.\n");

I would suggest text like "Please retry with a numeric value.\n"
return 0;

This completely exits your function and prevents you from doing
anything more. Delete it.

This is where your test for negative input belongs
else if (hoursWorked <= 0.) { /* we know fieldCount is 1 */
printf("Please retry with a positive value.\n");

At this point, we have finished with the value and fieldCount
indicates success or not. What you don't know is if scanf left any
junk in the queue. The safe thing to do is remove any if it did, with
something like
while (getchar() != '\n')
;

We are now done with all the input for hours worked and it is time to
close the loop
} while (fieldCount != 1 || hoursWorked <= 0.0);

This will repeat the loop if scanf did not find a number or if the
number was not positive.
while( hoursWorked < 0.0 )

This validation code belongs inside the input loop.
{
return 0;

Nothing else in this block can execute after this statement.
printf("The hours worked are %.2f\n", hoursWorked);

You want to print this out if the value is positive, not negative.

You create another loop here that looks exactly the same as the first.
printf("Please enter hourly rate: ");
fflush(stdout);
scanf("%lf", &hourlyRate);

After the hourly rate loop, you should print out all three lines here.
printf("The hourly rate is %.2f\n", hourlyRate);

printf("The total amount is %.2f\n", hoursWorked * hourlyRate);

return 0;
}
###########################


I know that theres no need to use a while loop, but i wanted to try
something

Ive got it so that if they dont enter a value, i.e 5, 3 and enter say
hello, then it says "please enter a number value"
but in that case, i want it to then return to "please enter hours worked"

ive tried a few things, none seemed to work

I tried extending the if statement:
if (fieldCount != 1)
{
printf("Please enter a number value.\n");
return 0;
}
else
{
printf("Please enter hours worked: ");
}

but then that just messed up the code :S

Help or advise would be apreciated
Design before you code. Once you have a design that flows the way you
want it, then you can code it.

You want a loop that you will always execute at least once but may
repeat if you don't like what happens on the current iteration. Of
all the looping constructs in C, do{}while seems to be the best
suited. Something like
do
prompt for input
obtain input
if input not valid
issue rebuke
while (input not valid)


<<Remove the del for email>>
 
R

Richard Heathfield

Barry said:
You never use the initial values.

That isn't necessarily a Bad Thing.

I make a point of assigning a value to every object in my code, irrespective
of whether that value is used, simply to give the program no excuse (in
that regard, at least) for indeterministic behaviour. I know that some
people in this newsgroup think that to do so is silly, but the practice has
saved me considerable debugging time in the past, so I'm not about to
change it now.

(No, the OP didn't get this idea off me, at least not as far as I know.)
Since you are interested in validating the input, scanf is not your
best choice.

<grin> Beautifully put.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top