Jump to content

Error "Expression must have constant value" in C

Forenman
Go to solution Solved by igormp,

Hmm, right. Forgot that in C you can't have switch-case expressions with dynamic-valued variables. You'd need to use if/elses.

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

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

		{
			switch (y) {
			case (y > 0 & y <= 10):
				printf("y is equal or greater than 0 and smaller than 10");
				break;

			case (y >= 11 & y <=20 ):
				printf("y is equal or greater than 11 and smaller than 20");
				break;

			case (y >= 21 & y <= 30):
				printf("y is equal or greater than 21 and smaller than 30");
				break;

			case (y >= 31 & y <= 40):
				printf("y is equal or greater than 31 and smaller than 40");
				break;

			case (y >= 41 & y <= 50):
				printf("y is equal or greater than 41 and smaller than 50");
				break;
			case (y >= 51 & y <= 60):
				printf("y is equal or greater than 51 and smaller than 60");
				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;
}

The code keeps throwing an error in visual studio "Expression must have a constant value". Any ideas how to rectify this?

Link to comment
Share on other sites

Link to post
Share on other sites

In your cases, you used & (which is the binary AND operator) instead of && (logic AND)

FX6300 @ 4.2GHz | Gigabyte GA-78LMT-USB3 R2 | Hyper 212x | 3x 8GB + 1x 4GB @ 1600MHz | Gigabyte 2060 Super | Corsair CX650M | LG 43UK6520PSA
ASUS X550LN | i5 4210u | 12GB
Lenovo N23 Yoga

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, igormp said:

In your cases, you used & (which is the binary AND operator) instead of && (logic AND)

I was literally just about to say the same 👍

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, igormp said:

In your cases, you used & (which is the binary AND operator) instead of && (logic AND)

I change that their and im still getting the same error

 

Link to comment
Share on other sites

Link to post
Share on other sites

11 minutes ago, Forenman said:

I change that their and im still getting the same error

 

It'd be nice if you gave us the full error, including which line it's referring to.

FX6300 @ 4.2GHz | Gigabyte GA-78LMT-USB3 R2 | Hyper 212x | 3x 8GB + 1x 4GB @ 1600MHz | Gigabyte 2060 Super | Corsair CX650M | LG 43UK6520PSA
ASUS X550LN | i5 4210u | 12GB
Lenovo N23 Yoga

Link to comment
Share on other sites

Link to post
Share on other sites

33 minutes ago, igormp said:

It'd be nice if you gave us the full error, including which line it's referring to.

 

Capture.PNG

Link to comment
Share on other sites

Link to post
Share on other sites

Hmm, right. Forgot that in C you can't have switch-case expressions with dynamic-valued variables. You'd need to use if/elses.

FX6300 @ 4.2GHz | Gigabyte GA-78LMT-USB3 R2 | Hyper 212x | 3x 8GB + 1x 4GB @ 1600MHz | Gigabyte 2060 Super | Corsair CX650M | LG 43UK6520PSA
ASUS X550LN | i5 4210u | 12GB
Lenovo N23 Yoga

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

×