Jump to content

Case function in C

Go to solution Solved by Moonzy,

case (argument): things to do until break;

ie.

case a<b : print out "hello"; break;

 

so in your example, you're comparing variable 'y' to see if it's an character 'A', 'B', 'C', 'D', or 'F', you're not comparing it to see if the value is larger or smaller than certain things.

 

image.png.52265f4c2d3ded2d461caec0923097c8.png

 

as for the rest of your program, no idea

Hi im having trouble getting my case function to work. I enter a number between 0-60 and it is meant to return a @your number is greater than x but less than y. It has to be done with switch statemtents but i dont know how to make this function. I am incrementing my for loop and trying to get it to work that way. Any ideas?

	#include <stdio.h>
void main(void)
{
	int y;
	double x, a, b, c, result;

	printf("Enter value for y: \n");
	scanf_s("%lf", &y);
	getchar();

	int counter;
	int min;
	int max;
	for (counter = 0; counter < 6; counter++) { // Incraments if statments up to 60
		min = counter * 10;						// Multiplys counter by 10
		max = (counter + 1) * 10;				// Allows counter up to 60
		if (y >= min && y < max)				// If statement for if its between 0<60
		{
			switch (y) {
			case 'A':
				printf("y is equal or greater than %d and smaller than %d", min, max);
				break;

			case 'B':
				printf("y is equal or greater than %d and smaller than %d", min, max);
				break;

			case 'C':
				printf("y is equal or greater than %d and smaller than %d", min, max);
				break;

			case 'D':
				printf("y is equal or greater than %d and smaller than %d", min, max);
				break;

			case 'F':
				printf("y is equal or greater than %d and smaller than %d", min, max);
				break;
			default:
				printf("Invalid number\n");
			}
		}
	}

	printf("\n Enter value for X:");	//Gets vlaue for x
	scanf_s("%lf", &x);				//Reads in value for x
	getchar();						//Waits for input
	printf("Enter value for A:");
	scanf_s("%lf", &a);
	getchar();
	printf("Enter value for B:");
	scanf_s("%lf", &b);
	getchar();
	printf("Enter value for C:");
	scanf_s("%lf", &c);
	getchar();

	result = (a * pow(x, 2)) + (b * x) + c;
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

case (argument): things to do until break;

ie.

case a<b : print out "hello"; break;

 

so in your example, you're comparing variable 'y' to see if it's an character 'A', 'B', 'C', 'D', or 'F', you're not comparing it to see if the value is larger or smaller than certain things.

 

image.png.52265f4c2d3ded2d461caec0923097c8.png

 

as for the rest of your program, no idea

-sigh- feeling like I'm being too negative lately

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×