hi everyone i need help in those problems Question 1: Write a program that checks if an integer value entered by the user is present in a file or not. If present, the program should indicate the order of this value in the file i.e. it is value number 1, or value number 20 etc… Your program should also check the file not found error case. In the first run shown below, the file was not in the folder. In the remaining runs, the file is there and the messages displayed by the program are shown. The number of values in the file is unknown. Assume that they are not repeated i.e. each value is written only once. Note: You should stop searching once you find the value you are looking for. You can use a logical variable and keep looking for the value of the user as long as it is not found and you did not reach the end of the file. Both conditions should be included in the loop. Question 2: Write an interactive program that displays a menu ( use do-while) containing many choices as shown below: 1- split a real number 2- check if a number is perfect square 3- check if a number is prime 4- check if a number is odd 5- Exit You have to write 5 functions as indicated below: Function 1 to display the choices shown above. Function 2 to receive a real value and return its whole part and decimal part separately (not print them); split function Function 3 is a logical function that receives a positive integer number and returns 1 if it is perfect square, 0 otherwise. Perfect squares are 4,9,16,25 etc… Function 4 is a logical function to receive a positive integer number and returns 1 if it is prime, 0 otherwise. Function 5 is also a logical function to check if an integer number is odd or not. In the main function, your program will ask the user to select a choice then, read the data and call the right function and print the right message. Sample runs of your programs are shown below. for sample output _ http://myfreefilehosting.com/f/7d74d2cea0_0.14MB thanks for help .
fp=fopen("file.txt","r"); while(fscanf(fp,"%d",&i)!=EOF) { if (i==number) { break; } } fclose(fp); Code (markup):
can you check please , what is the rong with my code ?!! #include<stdio.h> int main () { int x , z ; FILE *infile ; printf("enter the value you want") ; scanf ("%d",&x) ; infile = fopen("data.txt", "r") ; fscanf(infile,"%d",&z) ; while ( fscanf(infile,"%d",&z) !=EOF){ if(z==x){ printf ("the value exist in the file") ; break ; else printf("does not match") ; } } fclose (infile) ; return 0 ; }
thanks i removed else but when i enter a value which exists in the input file the program stops try my code and you will see it
it is my new code #include<stdio.h> int main () { int x , z ; FILE *infile ; printf("enter the value you want") ; scanf ("%d",&x) ; infile = fopen("data.txt", "r") ; fscanf(infile,"%d",&z) ; while ( fscanf(infile,"%d",&z) !=EOF){ if(z==x){ printf ("the value exist in the file") ; break ; } } fclose (infile) ; return 0 ; }
It will stop because you put a break; in: if(z==x){ printf ("the value exist in the file") ; break ; } } which makes the while loop quit when you find a match. Try: if(z==x){ printf ("the value exist in the file") ; }
who has any idea about this function Function 2 to receive a real value and return its whole part and decimal part separately (not print them); split function ??