i have to create a calculator which i have but i cant get it to run yet. i keep getting "12: error: subscripted value is neither array nor pointer" i'll highlight it for you. if anyone can fix it please do thank you.

#include<math.h>
#include<stdio.h>
int main()
{
float num1,num2,answer,i;
char c, menu;

display_menu ();
{
int i;
for (i=0; i<=4;i++)
[B]printf("%s\n",menu [i]);[/B]
}
char menu_item[7][14]={"Adition","Subtraction","Multiplcation","division","squareroot","percentage","Quit"};

get_input();
{
char c;
while (1)
{
display_menu();
printf("enter command\n");
c=getch();
switch (c)
{
case 'A':
case'a': Addition();
printf("enter two numbers:\n");
scanf("%f",&num1);
scanf("%f",&num2);
answer=num1+num2;
break;
case'S':
case's': Subtraction();
printf("Enter two numbers to subtract:\n");
scanf("%f",&num1);
scanf("%f",&num2);
if (num1>num2) {
answer=num1-num2;
}
 else  if (num1<num2){
 answer=num2-num1;
 }
 break;
 case'M':
 case'm': Multiplication();
 printf("enter two numbers to multiply:\n");
 scanf("%f",&num1);
scanf("%f",&num2);
answer=num1*num2;
break;
case'D':
case'd': Division();
printf("enter");
scanf("%f",&num1);
scanf("%f",&num2);
answer=num1/num2;
break;
case'R':
case'r': Squareroot();
printf("enter number to be square rooted:\n");
scanf("%f",&num1);
answer = sqrt(num1);
break;
case'P':
case'p': Percentage();
printf("Enter first number to be the percent and the second number to be\n");
scanf("%f",&num1);
scanf("%f",&num2);
answer = num1*num2/100;
break;
case'Q':
case'q': return;
}
printf("Answer is %f\n",answer);
}
}
}

The error means exactly what it says. Does "menu" look like an array or a pointer to you? I should hope not...

lines 8 and 16: remove the semicolon on those two lines. I assume you are attempting to write functions there ??? If that is correct then you can not nest functions inside other functions like that -- see main() on line 3. Move the code for those functions outside main(). Or you could just finish up main() before starting those functions. Either way, what you did won't work.

like Dragon said, you cant nest subroutines inside main, as you're doing. each function (including main) has to each be separate from each other.

other than that, the problem you've identified is that you're trying to treat a single char variable ("menu") as an array of chars. i think the confusion is simply that you've made a typo and called the wrong variable in your printf() statement. the "menu_item" array is the one i think you meant to call.

printf("%s\n",menu [i]);

should be

printf("%s\n",menu_item[i]);

still you need to fix the nesting issues as mentioned. it wont ever compile the way it currently is arranged.

This article has been dead for over six months. Start a new discussion instead.