﻿ C-programming "To repeat particular steps after getting instead of terminating the program"

# C-programming "To repeat particular steps after getting instead of terminating the program"

``````//program to calculate horizontal Distance and Reduced Level by tacheometry
#include <stdio.h>
#include <math.h>

void main()
{
float m,s,cosine,t,c,d,pi,v,h,hi,bm,rl,r;
printf("\n");
scanf("%f %f %f %f %f", &s, &t, &bm, &r, &h);
m=100; c=0;
pi=22.0/7.0;
cosine=cos(t*pi/180.0);
d=((m*s*pow(cosine,2))+(c*cosine));
v=d*tan(t*pi/180.0); hi=bm+r-v; rl=hi+v-h;
printf("distance=%f meters, vertival distance=%f meters,height of instrument=%f meters,reduced level=%f meters",d,v,hi,rl);
}
``````

Above I have provided the program to calculate Horizontal distance and Reduced Level.

I have a question that after printing the answer that is 12th line in the image. Can I repeat the steps 8 to 12 again instead of restarting the program?

I am a beginner in C programming. So I am sorry if I didn't specified something important.

You need to learn loops (see comments) but to help, your code could look like this:

``````#include <stdio.h>
#include <math.h>

int main(void)
{
do {
float m,s,cosine,t,c,d,v,h,hi,bm,rl,r;
printf("\n");
if (scanf("%f %f %f %f %f", &s, &t, &bm, &r, &h)!=5)
break;
m=100; c=0;
cosine=cos(t*M_PI/180.0);
d=((m*s*pow(cosine,2))+(c*cosine));
v=d*tan(t*M_PI/180.0); hi=bm+r-v; rl=hi+v-h;
printf("distance=%f meters, vertival distance=%f meters,height of instrument=%f meters,reduced level=%f meters",d,v,hi,rl);
} while (1);
return(0);
}
``````

the `do {...} while(1);` is an infinite loop but the loop breaks when the user does not enter the 5 parameters. After the loop, the `main` function returns zero, which will be forwarded to the operating system, which interprets zero as success.