Solved

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

Posted on 2009-04-05
9
276 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 84

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 84

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
 
LVL 9

Expert Comment

by:masheik
ID: 24070648
put
case 0:  instead default:
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 84

Accepted Solution

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now