This code i've written keeps showing an error "constant expression expected in function main". I'm not sure if the code i've written is on track.

#include<stdio.h>
  #include<math.h>
  #define MULTIPLY 1
	int i,n;
	int operation; int Answer = 1;
int main()
{
printf("You are welcome\n");

             
	printf(" how many numbers do you want to multiply? \n");
	scanf("%f",&n);
	float x[n];

  switch (operation){
	 case 'x':

	for(i=0; i<=n; i++)
	if (i<n)
	 printf("enter your value\n");
	else
	 printf("enter the last value\n");
	  scanf("%f",x[n]);


  for(i=0; i<=n; i++)
	Answer = Answer * x[i];

	printf( "The answer is %d", Answer);
	break;

	default:printf("sorry,this input is invalid");}
	return(operation);
	}

Recommended Answers

All 4 Replies

firstly, this is C and not C++, second use code tags...i can see you declared variable n as integer but then you are reading it as float....also, case x?? what are you trying to do

>> float x[n];
The error message means that you can not declare an array using a variable like n. Must have a const integer.

If you need to use the variable then allocate the array after the value of n is known.

float* x = 0;
<snip>
x = malloc(n * sizeof(float));

I assume you know that code has several other errors, such as missing { and } around multiple-line if statements.

the code is not intended properly, that's why we assumed missing {}. you have at least that right. I successfully found all { & }. 10 points to Prabakar:)

What is the operation variable doing? You never assigned a value for it.

I guess the program in itself incomplete. Are you planing to provide all basic arithmetics through the program?

why is the main fn returning a variable operation?

Could you state the problem more clearly?

And I would say there are limits for integers or even for floats, you cannot have infinite multipliers!!

you got a lot of errors, but i see what you're trying to do. so at least you're on track. :) here's a few of the more obvious problems:

(1) all variables should be declared before using them. I suggest that you declare the array "x" at the start of your "main" routine like so : int x[100]; ... this means of course that you are limited to a max of 100 values. change the number if you want. you should probably make a corresponding check that the user doesnt try to exceed this maximum value.

Dragon suggested "malloc"... definitely an option, but it is a more advanced concept and can get you in trouble later down the road if you dont fully understand it's power. Also, if this is homework, your grader will become very suspicious of you using advanced memory allocation functions when you dont have a grasp of the fundamentals.

(2) declaring variables again. i see you have the variable "operation" which is neither declared nor initialized. declare "operation" at the top of main like so: int operation; (3) to use a switch statement, you have to switch on a variable that has been set. "operation" has not been set. I assume here is where you wanted to use your "#define MULTIPLY 1"

printf("enter 1 to multiply or 2 to divide: ");
scanf("%d",&operation);

switch(operation)
{
    case MULTIPLY:
    ...
    break;
 
    case DIVIDE:
    ...
    break;

    ... etc ...

    default:
        printf("not a valid operation!\n");
}

this obviously presumes a corresponding #define DIVIDE 2 line, etcetera... also note that when using "scanf" for anything other than strings use reference the variable using the "&" operator. (because scanf operates on address locations, not the variable itself)

alternately, you could stick with switching on the case 'x' format, but recall the switch is an int (operation) and 'x' is an integer equal to 120 (0x78). that can be a little confusing at first.

i think if you get these issues sorted out you'll be on your way to having a working program.


.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.