CrazyEngineers
  • Can any one write this program without using "scanf"

    sndgpr26

    sndgpr26

    @sndgpr26-Yb3sXd
    Updated: Oct 21, 2024
    Views: 992
    Write a program to take any number from the user and that can give the output of twice the number, without using "scanf"
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Vishal Sharma

    MemberAug 30, 2012

    Is this your doubt? or you already know it?
    Are you sure? This action cannot be undone.
    Cancel
  • [Prototype]

    MemberAug 30, 2012

    There are various alternatives. Command line arguments, fgets etc.
    Are you sure? This action cannot be undone.
    Cancel
  • sndgpr26

    MemberAug 30, 2012

    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
    Are you sure? This action cannot be undone.
    Cancel
  • sndgpr26

    MemberAug 30, 2012

    [Prototype]
    There are various alternatives. Command line arguments, fgets etc.
    sir
    can u please write the whole program
    Are you sure? This action cannot be undone.
    Cancel
  • Vishal Sharma

    MemberAug 30, 2012

    There are various alternatives. Command line arguments, fgets
    wrong info:
    you cannot do it by fgets()
    Are you sure? This action cannot be undone.
    Cancel
  • Vishal Sharma

    MemberAug 30, 2012

    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;
    }
    
    Are you sure? This action cannot be undone.
    Cancel
  • Vishal Sharma

    MemberAug 30, 2012

    i think its not correct ans. As scanf n fscanf() belong to same family
    Are you sure? This action cannot be undone.
    Cancel
  • [Prototype]

    MemberAug 30, 2012

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • Vishal Sharma

    MemberAug 30, 2012

    you gonna do the same with command line arguments
    Can you give a small example?
    Are you sure? This action cannot be undone.
    Cancel
  • Vishal Sharma

    MemberAug 31, 2012

    [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!! 😖
    Are you sure? This action cannot be undone.
    Cancel
  • [Prototype]

    MemberAug 31, 2012

    Same can be done with commandline.

    #include<stdio.h>
    #include<stdlib.h>
     
    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;
    }
    
    Are you sure? This action cannot be undone.
    Cancel
  • Vishal Sharma

    MemberAug 31, 2012

    z = atoi((char*)n);
    Can you tell me what this part actually does?? ? that was cool!!
    Are you sure? This action cannot be undone.
    Cancel
  • [Prototype]

    MemberAug 31, 2012

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • Vishal Sharma

    MemberAug 31, 2012

    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??
    Are you sure? This action cannot be undone.
    Cancel
  • [Prototype]

    MemberAug 31, 2012

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • Vishal Sharma

    MemberAug 31, 2012

    okay!! I surfed the net for strtol() 😀
    really cool!
    thanks to you!! 😀
    Are you sure? This action cannot be undone.
    Cancel
  • sulochana anand

    MemberAug 31, 2012

    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?
    Are you sure? This action cannot be undone.
    Cancel
  • Vishal Sharma

    MemberAug 31, 2012

    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
    Are you sure? This action cannot be undone.
    Cancel
  • rahul69

    MemberAug 31, 2012

    Well here is my attempt :
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    int no,i;
    char s[40];
    gets(s);
    no=atoi(&s);
    printf("%d",no);
    getch();
    return 0;
    }
    simply gets() can do...😎
    Are you sure? This action cannot be undone.
    Cancel
  • sulochana anand

    MemberAug 31, 2012

    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 .
    Are you sure? This action cannot be undone.
    Cancel
  • Vishal Sharma

    MemberSep 1, 2012

    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-#
    Are you sure? This action cannot be undone.
    Cancel
  • Vishal Sharma

    MemberSep 1, 2012

    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!
    Are you sure? This action cannot be undone.
    Cancel
  • Priya Nadkarni

    MemberSep 1, 2012

    How about cin(console input)? Its simple.

    Program-
    #include<iostream.h>
    int main()
    {
    int num;
    cin>>num;
    cout<<num*2;
    return 0;
    }
    Are you sure? This action cannot be undone.
    Cancel
  • [Prototype]

    MemberSep 1, 2012

    PriyaJ
    How about cin(console input)? Its simple.

    Program-
    #include<iostream.h>
    int main()
    {
    int num;
    cin>>num;
    cout<<num*2;
    return 0;
    }
    Those are C++ functions. This discussion is about C.
    Are you sure? This action cannot be undone.
    Cancel
  • Priya Nadkarni

    MemberSep 1, 2012

    Oh k.
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register