Sunday 23 September 2012

Chapter 10 - Taking input from User

Until now all you did was assigned values to the variables and printed them.

In this tutorial we will learn to take input from the users. The syntax for scanning an input is similar to that of printf() function. Both pritf() and scanf() functions are defined withing stdio.h header 

The programs below will work in the following steps:
  1. We will first declare and initialize a variable .
  2. Take a value from the user and store it in our variable (this step will re initialize the value of that variable).
  3. Print the value stored in that variable.

In order to understand how each data type scans value (and also for future references) i have written 3 separate codes for each of the data types we have learned so far.

Note that the execution of the program stops when it encounters a scanf() function and does not proceed until the user enters some input

scanf() for 'int' data type: 

In the code below we will declare a number; say age then ask the user to enter his age.
To make the code interesting we will subtract his age from 2012 and print "You were born in ..."

/*  
 Author : Ryan Sequeira  
 Date : 23rd September 2012  
 Title : Calculates the Year of Birth 
*/  

#include<stdio.h>
#include<conio.h>

void main()
 {
  int age = 0,yob = 0;  // initialized the variabe to avoid errors

  clrscr();

  //prompting the user to enter age
  printf("Please enter your age: ");

  //scanf takes the input and stores it in variable age
  scanf("%d",&age);

  //calculate year of brith
  yob = 2012 - age;

  //printing the Year of Birth
  printf("\nYou were born in: %d", yob);

  getch();
 }

As you can see, the scanf() function has a similar syntax, the only difference is that you have to add '&' to prefix to the variable name. Like everything else this also has meaning. After we are done dealing with all the three data types i will explain the reason behind it. 

Now lets check the float data type.

scanf() for float data type: 

In this example we will write a code to convert distance in kilometers to miles. First we will declare a variable say distance. Scan the value from the user and store it in distance. then we will apply the following conversion to convert kilometers into miles.

1 Kilometer  =  0.621371 Miles

/*  
 Author : Ryan Sequeira  
 Date : 23rd September 2012  
 Title : Converts distance from Km to Miles 
*/  

#include<stdio.h>
#include<conio.h>

void main()
 {
  float km = 0.0, miles = 0.0;  // initialized the variabe to avoid errors

  clrscr();

  //prompting the user to enter distance
  printf("Please enter distance in kilmoeters: ");

  //scanf takes the input and stores it in variable km
  scanf("%f",&km);

  //calculate distance in miles
  miles = km * 0.621371;

  //printing the distance in miles
  printf("\nThe distance is miles is: %.2f", miles);

  getch();
 }

Nothing special to mention, the method to scan a float is similar to that of int and the '.2' between % and f is to display value only up to 2 decimal places . So 1 more data type to go before the explanation for '&' .


scanf() for char data type: 

All i could think of is a code that takes a character input and prints the ACSII value of it. We all have implemented this in the previous chapter, the only change here being, user gets to enter the character.

/*  
 Author : Ryan Sequeira  
 Date : 23rd September 2012  
 Title : Gives the ASCII code of the key entered 
*/  

#include<stdio.h>
#include<conio.h>

void main()
 {
  char ch = 'a'; // initialized the variabe to avoid errors

  clrscr();

  //prompting the user to enter a digit
  printf("Enter a digit: ");

  //scanf takes the input and stores it in variable ch
  scanf("%c",&ch);

  //printing the ACSCII value
  printf("\nThe ASCII value is: %d", ch);

  getch();
 }


The mystery behind '&'

I had said this in the first chapter that data (in computer) is stored in the memory and has an address associated with it. Writing & as prefix to variable name (i.e. &age, &ch, etc.) gives the address of the variable. When you declare a variable the compiler assigns it an address in main memory and hence when you take a value from the user you tell scanf to store it in the address which is given by & followed by variable name.

If you are unable to understand what i have explained or if you can provide a better explanation please comment and i will make the necessary changes.

Now that you have learned to take input from the users, there are a lot of things that you can do. Try out the things listed out in Activities

Activities


  1.  Try to implement all the programs given in the previous chapters, but make use of scanf to set the values of the variables used. 
  2.  Try to write a program that takes the marks of 5 subjects(store it in 5 variables) and the total marks (out of) from the user and calculates his/her percentage.
 By doing this you will be able to write an actual program, one which takes input from users, processes it and produces results.

No comments:

Post a Comment