User Input

Let us see how to accept values from a user and display the output

main()
{
     
      int phone;
      printf("Pl Enter Your Phone Number ..... ");
      scanf("%d" , &phone);
      printf("Your Phone Number Is .... %d",phone);
      getch();
     
      }

This program ask user for phone number , stores the value in a variable called phone and display the output

Now let us examine the new element in the program

scanf("%d" , &phone);  - scanf is another function like ptintf , just like printf is used to display something on the screen scanf is used to get input from the keyboard and store it in a variable . 

 Let us see some more programs which take other data types

For floats we have

main()
{
     
      float salary;
      printf("Pl Enter Your salary ..... ");
      scanf("%f" , &salary);
      printf("Your salary Is .... %f",salary);
      getch();
     
      }

For doubles we have

 main()
{
     
      double pi;
      printf("Pl Enter value of pi ..... ");
      scanf("%lf" , &pi);
      printf("value of pi is .... %lf",pi);
      getch();
     
      }

For character  we have

main()
{
     
      char c;
      printf("You are male or female(M/F) ..... ");
      scanf("%c" , &c);
      printf("You are .... %c",c);
      getch();
     
      }


So we see how we can accept different data types from user and display them , sometimes if we use many scanf in the same program the value stored in previous scanf causes problem , so in order to solve this we use a function called fflush(stdin) , this function just clears the memory from previous data , but in order to use flush(stdin) function ,  we have to ask the compiler to include the file containing this function and we do this by writing  #include<stdio.h> at very top of the program . 

This might confuse you at this moment but as we go further everything would be clear

Now let us write a little bit of complex program using all what we learned

#include<stdio.h>
main()
{
      int mobile_number;
      int age;
      int land_line;
      char gender;
      float salary;
      double tax;
     
      printf(">>>>>>>>>>>Welcome<<<<<<<<<<<<< \n");
      printf("Enter Mobile Number............ ");
      scanf("%d",&mobile_number);
      fflush(stdin);
      printf("Enter age...................... ");
      scanf("%d",&age);
      fflush(stdin);
      printf("Enter Land Line Number......... ");
      scanf("%d",&land_line);
      fflush(stdin);
      printf("Enter Gender (M/F)............. ");
      scanf("%c",&gender);
      fflush(stdin);
      printf("Enter Salary................... ");
      scanf("%f",&salary);
      fflush(stdin);
      printf("Enter All Time Tax............. ");
      scanf("%lf",&tax);
      fflush(stdin);
      printf("===========================================\n");
      printf("Your Mobile Number Is %d\n",mobile_number);
      printf("Your Age Is %d\n",age);
      printf("Your Land Line Number Is %d\n",land_line);
      printf("===========================================\n");
      printf("Your Gender Is %c\n",gender);
      printf("Your Salary Is %f\n",salary);
      printf("Your Total Tax Is %lf\n",tax);
      printf("===========================================\n");
           
      getch();
     
      }  
  
It may look big but it is very simple actually , it just ask the user for some information , stores the information in  variables and display the output
      

Before going further let me say something about commenting a program , well comments are used by the programmer to explain some piece of code 

comments are not  part of the program , 

comments just make the program user friendly and readable , 

we can write comments beginning with two slashes like 

//comments goes here 

for multiple lines 

/* multi line comments */ 

One example will be

/*
Program to accept a phone number and display it (this is a comment)
*/
main()
{
     
      int phone;      // we declare a variable
      printf("Pl Enter Your Phone Number ..... ");
      scanf("%d" , &phone);     //accept value
      printf("Your Phone Number Is .... %d",phone);    //display the output
      getch();
     
 }

No comments:

Post a Comment