Write a program to detect an error while inputting a data.Use return value of scanf()statement.

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
	int a,b,c,v;
	printf("Enter value of 'A','B' & 'c' :");
	v=scanf("%d %d %d",&a,&b,&c);
	(v<3 ? printf("\n Error in Inputting") : printf("\n Values successfully read."));
	getch();
}

/*In the above program the printf() statement returns values equal to the number of variables correctly
read.The conditional statement check the value of variable V and prints the respective messages.*/ 

The program prompts the user to enter values for three integers, ‘a’, ‘b’, and ‘c’. It then uses the scanf() function to read in these values from the user input. The return value of scanf() is assigned to the variable v.

The program checks the value of v to determine if all three values were correctly read in. If v is less than 3, it means that an error occurred while inputting the data, and the program outputs an error message. Otherwise, it outputs a success message.

This approach can be useful for detecting errors in input data, especially when the input data is critical to the correct functioning of the program. By checking the return value of scanf(), the program can ensure that the input data is correct and handle any errors that occur during input gracefully.

Thanks

Write a program to detect an error while inputting a data.Use return value of scanf()statement.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top