how to make user function to count number of x entered (beginner)

hi there I want to know how to make a user define function to count total number of x entered, then the total number of x entered will be displayed when the user exit.
Thank you for any help because I'm still a beginner, here is my program:
------------------------------------------------------------------
#include<stdio.h>
#include<math.h>
#include<stdlib.h>

int main()
{
int a;
double x,i;
double answer1,answer2;

for (i=1;i>0;i++)
{
for (i=1;i>0;i++)
{
printf("Enter a value between 0 and 2:");
scanf("%lf", &x);

if ((x>0) && (x<=2))
i=-1;
else if ((x<0) && (x>2))
i=1;
}
answer1 = log (x);
answer2 = (x-1)-(((x-1)*(x-1))/2);

printf("log x = %lf and its approximation is %lf\n", answer1, answer2);

printf("press 1 to continue or press 0 to exit:");
scanf("%d", &a);
if(a=1)
i=1;
else if (a=0)
i=-1;
}
return (0);
}




Last edited on
your problem is you have an infinite loop.
and how to fix it?? I can't see which one, thanks.
on lines 11 and 13 you start a for loop that has a test value that will never be false
and on 30 and 33 you need to change those from
a=0
and
a=1

to a==0

and

a==1
Thanks that helps =)

so now I supposed to count how many time the user enter the value before he exit, how do I count it into another user define function?
wiiiii! I got it..

#include<stdio.h>
#include<math.h> /*Function to compute simple mathematical operation*/
#include<stdlib.h> /*Standard general library*/

void display (double highest,double min,int total); /*Function call to display the result for highest answer, minimum x entered & total question solved*/


int main()
{
double min=2.1,highest=-200,final;
int a,n,total=0;
double x,i;
double answer1,answer2;
FILE *as; /*File Pointer to save the result for log x*/
for (i=1;i>0;i++) /*This loop will check whether the user wants to continue or exit*/
{
for (i=1;i>0;i++) /*This loop will check whether the user entered the right value of x*/
{
printf("Enter a value between 0 and 2:");
scanf("%lf", &x);

if ((x>0) && (x<=2)) /*If..else statement to check whether the value of x entered is in the range*/
i=-1;
else if ((x<0) && (x>2))
i=1;
}
answer1 = log (x);
answer2=0;
for (n=1;n>0;n++)
{
final=(pow(-1,n+1)*pow(x-1,n))/n; /*Equation using <math.h> to calculate the approximation value for log x*/
answer2 += final;
if (fabs(final)<0.00001)
n=-10;
}

printf("log x = %.4lf and its approximation is %.4lf\n", answer1, answer2);
as = fopen("matric_number.txt","a"); /*The calculated value of x using standard log value will be save in matric_number file*/
fprintf(as,"%lf\n", answer1);
fclose(as);
printf("press 1 to continue or press 0 to exit:");
scanf("%d", &a);
if(a==1) /*If...else statement to check whether the user wants to exit or not*/
i=1;
else if (a==0)
i=-1;
total+=1;
if (x<min) /*If statement to check for the lowest value of x user entered*/
min=x;
if (answer1>highest) /*If statement to check for the highest log x solved*/
highest=answer1;
}


display (highest,min,total);
return 0;

}

void display (double highest,double min,int total) /*This function will display all the result for highest answer, minimum x entered & total question solved*/
{
printf("Highest logarithm solve is %.4lf\n", highest);
printf("Minimum x entered is %.3lf\n",min);
printf("Total of question solved is %d\n", total);

return;
}



Topic archived. No new replies allowed.