Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# how do I move from case to case in a switch statement?

Posted on 2009-04-05
Medium Priority
286 Views
I was writing a program that was calculating wage and here is a snippet from it. The test expression of the switch statement is hours < 40, and when a number less than 40 was entered the switch statement worked fine. But when a number larger than 40 was entered, which I thought would direct the switch statement to case 0, nothing happened. Any idea why case 0 isn't be considered the "false" result of hours < 40, as I expected?
``````printf("How many hours did you work? (enter q to quit)\n");
while(scanf("%d", &hours))
{
scanf("%f", &rate_pr_hr);
switch (hours < 40)
{
case 1:
case 0:
``````
0
Question by:prebek
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 2
• 2
• +2

LVL 84

Expert Comment

ID: 24070563
it looks like case 1: and case 0: are labels on the same (missing) code, so what go you expect to happen differently?
0

Author Comment

ID: 24070604
i didn't include the code, i just wanted to point out that case 1 worked and case 0 didn't. But here is the code:

#include <stdio.h>

int main ()
{

float rate_pr_hr;
int hours;
float pay;

printf("How many hours did you work? (enter q to quit)\n");
while(scanf("%d", &hours))
{
scanf("%f", &rate_pr_hr);
switch (hours <= 40)
{
case 1:
pay = hours * rate_pr_hr;
printf("You made \$%f last week\n\n", pay);
break;
default:
pay = 40 * rate_pr_hr + ((hours - 40) * rate_pr_hr * 1.5);
printf("You made \$%f last week\n\n", pay);
break;
}
printf("How many hours did you work?\n");
}

printf("Bye.\n\n");
return 0;
}
0

LVL 84

Expert Comment

ID: 24070630
I don't see a case 0: in that code, but when (hours <= 40) is not 1, it goes to the default: label
0

LVL 9

Expert Comment

ID: 24070648
put
0

LVL 84

Accepted Solution

ozo earned 2000 total points
ID: 24070768
case 0:  instead default: should make no difference here, since when (hours <= 40) is not 1, it will be 0
0

LVL 3

Expert Comment

ID: 24070795
what does your system kick back from

int temp = (hours <= 40);
printf("%d\n", temp);

when hours = 40 and hours > 40?
0

LVL 53

Expert Comment

ID: 24070810
Wouldn't an if statement be more appropriate here ?
``````if (hours <= 40) {
pay = hours * rate_pr_hr;
}
else {
pay = 40 * rate_pr_hr + ((hours - 40) * rate_pr_hr * 1.5);
}
printf("You made \$%f last week\n\n", pay);
``````
0

LVL 3

Expert Comment

ID: 24071394
Probably would be better programming practice Infinity08 as it doesn't require the compiler to convert the output of a boolean operation to integer as is obviously tricky, but that's not nearly as fun :P Besides, I ran this code and got the expected behavior (40 hrs at \$10 got \$400 and 50 hrs at \$10 got \$550) so it's safe to assume it's how the compiler is handling the statement.
0

Author Closing Comment

ID: 31566715
you guys are correct, i changed default to case 0 and the program worked fine. thanks.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: Iāve never triā¦
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see soā¦
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
###### Suggested Courses
Course of the Month10 days, 12 hours left to enroll