exit
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include <stdafx.h>
#include <stdio.h>
void main()
{
int hours = 0, rate = 8;
double salary = 0.00;
do
{
printf("Enter number of hours worked ( -1 to end ): ");
scanf_s("%d", & hours);
printf("Hourly rate ( &00.00 ): %d\n", rate);
if ( hours < 48 )
{
salary = hours * rate;
printf("Salary is $%.2f\n", salary);
}
} while ( hours != -1);
}
|
How to end when i type '-1' to exit? Currently, it exit after prompt all. Please help
You need to test
inside the loop.
Also, void main() is evil.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
#include <stdio.h>
int main()
{
double hours = 0.0;
double rate = 8.0;
double salary = 0.0;
do
{
printf( "\nEnter number of hours worked ( -1 to end ): " );
fflush( stdout );
scanf( "%lf", &hours );
if (hours < 0.0)
break;
printf( "Enter the hourly rate: " );
fflush( stdout );
scanf( "%lf", &rate );
if (hours < 48)
{
salary = hours * rate;
printf( "Salary is %%.2f\n", salary );
}
else
printf( "What! You have to work less than 48 hours!\n" );
}
while (true); // thanks bluecoder
return 0;
}
|
Hope this helps.
Last edited on
i actually want to loop three time and only exit when i type '-1' , can pls check my below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#include <stdafx.h>
#include <stdio.h>
void main()
{
int hours = 0;
int rate = 8;
double salary = 0.00;
printf("Enter number of hours worked ( -1 to end ): ");
scanf_s("%d", &hours);
while (hours != -1)
{
if ( hours < 48)
{
printf("Hourly rate ( $00.00 ): %d\n", rate);
salary = hours * rate;
printf("Salary is $%.2f\n", salary);
}
else
{
printf("Hourly rate ( $00.00 ): %d\n", rate);
salary = 48.0 * rate + (hours - 48.0) * rate * 1.5;
printf("Salary is $%.2f\n", salary);
}
}
}
|
You aren't going to get very far if you can't pay attention.
Duoas are you missing while in do loop.
Oops! Fixed above.
Sorry wrz234
Thanks duoas i had make it done with your ref.
Topic archived. No new replies allowed.