[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2009-04-05
9
Medium Priority
?
288 Views
Last Modified: 2012-05-06
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))
	  {
		  printf("What was your hourly wage?\n");
		  scanf("%f", &rate_pr_hr);
		  switch (hours < 40)
		  {
			  case 1: 
                          case 0:

Open in new window

0
Comment
Question by:prebek
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 85

Expert Comment

by:ozo
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

by:prebek
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))
        {
              printf("What was your hourly wage?\n");
              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 85

Expert Comment

by:ozo
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:masheik
ID: 24070648
put
case 0:  instead default:
0
 
LVL 85

Accepted Solution

by:
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

by:csaint00
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

by:Infinity08
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);

Open in new window

0
 
LVL 3

Expert Comment

by:csaint00
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

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-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

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question