# Thread: Help with loops in functions?

1. ## Help with loops in functions?

Hi all, i'm new to programming. This is a code to calculate the areas of different shapes. The user should choose the shape, pick whether they want the measurement in cm or m, then be asked how many shapes they are calculating the area of.

I am wondering whether I can make this code with with the for loops in the functions, or will I have to move them back up to the main body.
I have also tried making the return types void.

SQUARE: Will ask how many shapes you want to calculate the area of but will only calculate the area for one regardless
CIRCLE/TRIANGLE: Will ask if you want the answer in cm or m but will then loop back to the main menu.

Sorry this is such a long code!

Code:
```double squareCM (double a,double b);
double squareM (double a, double b);
double circleCM (double a);
double circleM (double a);
double triangleCM (double a,double b);
double triangleM (double a, double b);

double a,b;
main()
{
char ch,pa;

while(ch!='0')
{
system("cls");
printf("\n 1 - Rectangle");
printf("\n 2 - Circle");
printf("\n 3 - Triangle");
printf("\n 0 - Exit");

ch=getche();
system("cls");

if(ch=='1')
{
printf("\n 1 - Answer in CM");
printf("\n 2 - Answer in M");
printf("\n 0 - Exit");

pa=getche();
system("cls");

if(pa=='1')
{
squareCM(a,b);
}
else if(pa=='2')
{
squareM(a,b);
}
}

else if(ch=='2')
{
printf("\n 1 - Answer in CM");
printf("\n 2 - Answer in M");
printf("\n 0 - Exit");

pa=getche();
system("cls");

if(pa=='1')
{
circleCM(a);
}
else if(pa=='2')
{
circleM(a);
}
}

else if(ch=='3')
{
printf("\n 1 - Answer in CM");
printf("\n 2 - Answer in M");
printf("\n 0 - Exit");

pa=getche();
system("cls");

if(pa=='1')
{
triangleCM(a,b);
}
else if(pa=='2')
{
triangleM(a,b);
}
}

}

}
double squareCM(double a,double b)
{
int counter,d;

printf("Please enter the amount of shapes you are calculating the area of\n");
scanf("%d",&counter);

for(d=1; d<=counter; d++)
{
scanf("%lf",&a);
scanf("%lf",&b);
double num;
num=(a*b);
return num;

}

}

double squareM (double a,double b)
{
double num;
num=((a*b)/100);
return num;

int counter,d;

printf("Please enter the amount of shapes you are calculating the area of\n");
scanf("%d",&counter);

for(d=1; d<=counter; d++)
{
scanf("%lf",&a);
scanf("%lf",&b);

printf("%lf\n",squareM(a,b));
getch();
}
}
double circleCM (double a)
{
double num;
double pi;
num=(pi*a*a);
return num;

int counter,d;
double area;

printf("Please enter the amount of shapes you are calculating the area of\n");
scanf("%d\n",&counter);

for(d=1; d<=counter; d++)
{
scanf("%lf",a);
area = pi*a*a;
printf("The area of the circle is: %lf\n", circleCM(a));
getch();
}
}
double circleM (double a)
{
double num;
double pi;
num=((pi*a*a)/100);
return num;

int counter,d;
double area;

printf("Please enter the amount of shapes you are calculating the area of\n");
scanf("%d\n",&counter);

for(d=1; d<=counter; d++)
{
scanf("%lf",a);
area = ((pi*a*a)/100);
printf("The area of the circle is: %lf\n", circleM(a));
getch();
}
}
double triangleCM (double a, double b)
{
double num;
num=((a*b)*0.5);
return num;

int counter,d;
double area;

printf("Please enter the amount of shapes you are calculating the area of\n");
scanf("%d\n",&counter);

for(d=1; d<=counter; d++)
{
scanf("%lf\n",&a);
scanf("%lf\n", &b);
area=(a*b)*0.5;

printf("The area of the triangle is: %lf\n",triangleCM(a,b));
getch();
}
}
double triangleM (double a, double b)
{
double num;
num=(((a*b)*0.5)/100);
return num;

int counter, d;
double area;

printf("Please enter the amount of shapes you are calculating the area of\n");
scanf("%d\n",&counter);

for(d=1; d<=counter; d++)
{
scanf("%lf",&a);
scanf("%lf", &b);
area=(((a*b)*0.5)/100);

printf("The area of the triangle is: %lf\n",triangleM(a,b));
getch();

}
}```

Thanks,
Katie 2. There are a number of problems with your code, some of which I am listing here:
• Where are your header inclusions? You use system, so you should #include <stdlib.h>. You use printf, so you should #include <stdio.h>. You also use the non-standard getche, so you should #include <conio.h>, which is a non-standard header, or use some other method of reading user input that uses the standard library instead (e.g., getchar, scanf, fgets with sscanf).
• You declared two global variables:
Code:
`double a,b;`
Generally, global variables should be avoided as they tend to make it more difficult for you to reason about your code. As we shall see later, you do not need these global variables at all.
• This:
Code:
`main()`
should be:
Code:
`int main(void)`
• You declared ch and then compared it to '0' before giving it an initial value.
• Looking at your squareCM function, you do not actually use the parameters a and b as parameters. Rather, you use them as you would any other ordinary local variables, i.e., to store user input within the function. Therefore, you do not need them as parameters, and hence you do not need the global variables to pass as arguments.
• In various places in your code, you called scanf without checking its return value. This can be problematic because an invalid input might be entered by the user.
• Note that unless you are using the loop index and hence it would be more convenient otherwise, it is conventional to loop starting from 0:
Code:
`for (d = 0; d < counter; d++)`
• Notice that the loop in squareCM only loops at most once because you return unconditionally from within the loop body.
• squareM has similiar problemas squareCM, except that this time you returned a value so early that the rest of the function is unreachable.
• Why CM and M? You should name your functions with descriptive names, or if that is truly impractical, comment.
• In circleCM, you use pi without having given it a value. Just naming a variable pi is insufficient to make it π Code:
` double a ,b;`
I declared this as a global variable because I received an error stating that I had too few arguments and that 'a' and 'b' were undeclared. Which it is still doing now, i'm not sure how to resolve this issue.

Sorry if this is a silly question:
- I am taking the ch value from the user with scanf so I thought you don't need to initialize it?

Katie 4. Originally Posted by KatieS
I declared this as a global variable because I received an error stating that I had two few arguments and that 'a' and 'b' were undeclared. Which it is still doing now, i'm not sure how to resolve this issue.
Parameters are properly either used to receive input from the caller (i.e., the value of the parameter is used in the function), or used to send output to the caller (i.e., instead of using the return value, a pointer is passed such that the function can modify what the pointer points to, and hence the caller can access what the pointer points to), or both. In your case, you are not using the parameters as you should, so the solution is to have no parameters: just declare local variables named a and b within the various functions. Originally Posted by KatieS
I am taking the ch value from the user with scanf so I thought you don't need to initialize it?
Look carefully:
Code:
```char ch,pa;

while(ch!='0')```
What is the value of ch at this point, before control has entered the loop body for the first time? 5. >I declared this as a global variable because I received an error stating that I had too few arguments and that 'a' and 'b' were undeclared. Which it is still doing now, i'm not sure how to resolve this issue.

Could you post the error message you get.
As a matter of fact there are a lot of things which need to be fixed in you code to get it working. I suggest that you break down the problem into parts and work on to get atleast one thing working and then you can start building on top of it. Popular pages Recent additions c programming, functions, loops 