Pages

Store data without Scanf (C Program)

Saturday, 30 November 2013

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

/*

this program takes inputs from user and stores them into specific locations wherever  user specify
without using scanf function.

*/

void omarin(char *s, ... ); // this function do the functionality of scanf .

int main()
{
    printf("\n\t\t>> NOTES <<\n");
    printf("\nthe program will take inputs from you and stores them into variables without \
scanf function.\n\n");
    printf("you can change types of input you want to enter from main. \n\n");
    printf("now please enter integer value , float value ,and  char ,please put between them\
spaces and finally press enter to stores them in variables.\n\n ");

    int integer_value;
    float float_value;
    char c;
    omarin("d f c ",&integer_value,&float_value,&c);
    printf(" integer = %d\tfloat = %f\tchar = %c\n",integer_value,float_value,c);
    return 0;
}

void omarin(char *s, ... )
{
    char sr[20][20]={'\0'};
    char st[30]={'\0'};
    gets(st);
    int lenst=strlen(st);
    int counter=0;
    int arr[20]={0};
    int k;
    for(k=0;k<=lenst;k++)
    {
        if(st[k]==' '||st[k]=='\0')
        {
            arr[counter]=k;
            counter++;

        }
    }
    int h;
    int index=0;
    int jk=0;
    for(h=0;h<10;h++)
    {
        int jk=0;

        if(arr[h]!=0)
            {
                int j;
                for(j=index;j<arr[h];j++)
                {
                    sr[h][jk]=st[j];
                    jk++;
                }
                index=arr[h];

            }
    }



    union Printable_t {
                            int *     i;
                            float *   f;
                            char *    c;
                            char *    s;
                      } Printable;
    va_list arguments;
    va_start(arguments,s);
    int len=strlen(s);
    int i;
    int w=0;
    for(i=0;i<len;i++)
    {
        switch(s[i])
        {
        case 'd':

               Printable.i=va_arg(arguments,int*);

               *(Printable.i)=atoi(sr[w]);

               w++;
               break;
        case 'f':
               Printable.f=va_arg(arguments,float*);
               *(Printable.f)=atof(sr[w]);
               w++;
               break;
        case 'c':
                 Printable.c=va_arg(arguments,char*);
                 *(Printable.c)=(sr[w][1]);
                 w++;
                 break;

        }

    }

}