Can any one write this program without using "scanf"

Write a program to take any number from the user and that can give the output of twice the number, without using "scanf"

Replies

  • Vishal Sharma
    Vishal Sharma
    Is this your doubt? or you already know it?
  • [Prototype]
    [Prototype]
    There are various alternatives. Command line arguments, fgets etc.
  • sndgpr26
    sndgpr26
    Vishal0203
    Is this your doubt? or you already know it?
    no i dont know it
    actually some one had asked me to do this
    but i was failed
  • sndgpr26
    sndgpr26
    [Prototype]
    There are various alternatives. Command line arguments, fgets etc.
    sir
    can u please write the whole program
  • Vishal Sharma
    Vishal Sharma
    There are various alternatives. Command line arguments, fgets
    wrong info:
    you cannot do it by fgets()
  • Vishal Sharma
    Vishal Sharma
    no i dont know it
    actually some one had asked me to do this
    but i was failed
    Note:: I'm not using scanf() but I'm using fscanf().. is that okay??

    #include "stdio.h"
    #include "conio.h"
    int main()
     
    {
       
    int n,a;
    fscanf(stdin,"%d",&n);
    a=2*n;
    fprintf(stdout,"%d",a);
    getch();
    return 0;
    }
    
  • Vishal Sharma
    Vishal Sharma
    i think its not correct ans. As scanf n fscanf() belong to same family
  • [Prototype]
    [Prototype]
    sndgpr26
    sir
    can u please write the whole program
    You got the info, give it a try yourselves. We're here to help you learn & what you're asking is not the correct way.

    Vishal0203
    wrong info:
    you cannot do it by fgets()
    Well, you gonna do the same with command line arguments i.e. convert a string into a integer. Its surely a possible option.
  • Vishal Sharma
    Vishal Sharma
    you gonna do the same with command line arguments
    Can you give a small example?
  • Vishal Sharma
    Vishal Sharma
    [Prototype]
    You got the info, give it a try yourselves. We're here to help you learn & what you're asking is not the correct way.



    Well, you gonna do the same with command line arguments i.e. convert a string into a integer. Its surely a possible option.
    what conversion you're talking about?? Will you lease elaborate and educate me about it??

    #include "stdio.h"
    #include "conio.h"
    int main()
    { 
    char n[1]; int a,b;
    fflush(stdin);
    fgets(n,sizeof(n),stdin);
    b = int(n);
    printf("%d",b);
    //a=2*n;
    //fprintf(stdout,"%d",a);
    getch();
    return 0;
    }
    
    Is this you're talking about?? But doesn't give a desired output!! ๐Ÿ˜–
  • [Prototype]
    [Prototype]
    Same can be done with commandline.

    #include
    #include
     
    int main()
    {
    char n[20];
    int z;
     
    fgets(n, sizeof(n),stdin);
     
    z = atoi((char*)n);
     
    printf("String converted to integer is -> %d\n",z);
    printf("Adding 10 to the converted string -> %d\n",z+10);
     
    return 0;
    }
    
  • Vishal Sharma
    Vishal Sharma
    z = atoi((char*)n);
    Can you tell me what this part actually does?? ? that was cool!!
  • [Prototype]
    [Prototype]
    Vishal0203
    Can you tell me what this part actually does?? ? that was cool!!
    That's actually the heart of the system atoi (ASCII to INTEGER) is the function which converts the string into integer. However, a major drawback attached to it is that it doesn't report error in case you pass something like "asdads" as input instead of numbers in form of string. You can use strtol() instead of atoi(), but I've just used it to keep the things simple.

    I've done a typecast to char* because atoi requires parameter as char* & not char.
  • Vishal Sharma
    Vishal Sharma
    doesn't report error in case you pass something like "asdads" as input instead of numbers in form of string.
    I think we can make a check for number or string using conditional statements before passing it to atoi(), isn't it??
  • [Prototype]
    [Prototype]
    Vishal0203
    I think we can make a check for number or string using conditional statements before passing it to atoi(), isn't it??
    How so? Everything that will come as an input will be a string i.e. despite you're sending input as 123456, its will be treated as a string. To make the string distinguishable as an integer, we're using that function.
  • Vishal Sharma
    Vishal Sharma
    okay!! I surfed the net for strtol() ๐Ÿ˜€
    really cool!
    thanks to you!! ๐Ÿ˜€
  • sulochana anand
    sulochana anand
    Vishal0203
    Note:: I'm not using scanf() but I'm using fscanf().. is that okay??

    #include "stdio.h"
    #include "conio.h"
    int main()
     
    {
     
    int n,a;
    fscanf(stdin,"%d",&n);
    a=2*n;
    fprintf(stdout,"%d",a);
    getch();
    return 0;
    }
    
    y r u asking?if u used this code then definitely u must run it.wat was the result?
  • Vishal Sharma
    Vishal Sharma
    sulochana anand
    y r u asking?if u used this code then definitely u must run it.wat was the result?
    result was fine..
    I'm getting the desired result.
    but I'm using fscanf() which belongs to family of scanf(). That's the reason I asked
  • rahul69
    rahul69
    Well here is my attempt :
    #include
    #include
    main()
    {
    int no,i;
    char s[40];
    gets(s);
    no=atoi(&s);
    printf("%d",no);
    getch();
    return 0;
    }
    simply gets() can do...๐Ÿ˜Ž
  • sulochana anand
    sulochana anand
    Vishal0203
    result was fine..
    I'm getting the desired result.
    but I'm using fscanf() which belongs to family of scanf(). That's the reason I asked
    thats right.but u are using fscanf "stdin" but scanf need not to use stdin.The fscanf() function read from the named input stream. The scanf() function reads from the standard input stream .
  • Vishal Sharma
    Vishal Sharma
    gets()
    gets() is not a good suggestion as it overloads the buffer. Hence,
    avoid using gets as much as possible.

    for more info,
    #-Link-Snipped-#
  • Vishal Sharma
    Vishal Sharma
    sulochana anand
    thats right.but u are using fscanf "stdin" but scanf need not to use stdin.The fscanf() function read from the named input stream. The scanf() function reads from the standard input stream .
    you're right but, the ques is about the function and as far as i know, scanf() fscanf() both belong to same family.
    anyways i got to learn a new thing ๐Ÿ˜€ atoi() i didn't know about it!
  • Priya Nadkarni
    Priya Nadkarni
    How about cin(console input)? Its simple.

    Program-
    #include
    int main()
    {
    int num;
    cin>>num;
    cout< return 0;
    }
  • [Prototype]
    [Prototype]
    PriyaJ
    How about cin(console input)? Its simple.

    Program-
    #include
    int main()
    {
    int num;
    cin>>num;
    cout< return 0;
    }
    Those are C++ functions. This discussion is about C.
  • Priya Nadkarni
    Priya Nadkarni
    Oh k.

You are reading an archived discussion.

Related Posts

Hello everybody! I'm doing my final year Diploma in Computer Science Engineering.I want to do a project(creating an anti phishing software).I need CE to provide me some ideas and helps!...
i got an information tat ter s an off-campus HCL placement in tagore engineering college,chennai. is it true? do rply asap
I've been offered a place with a civil engineer for work experience. I haven't taken a subject involving design/technology, an I don't have experience with design and technical drawing. But...
Which phone i should buy between 15k to 19k??????????
Hi evrybody, can anybody provide me the placement papers of CAPGEMINI Thanx in advance