-
Write a program to take any number from the user and that can give the output of twice the number, without using "scanf"0
-
Member • Aug 30, 2012
Is this your doubt? or you already know it?Are you sure? This action cannot be undone. -
Member • Aug 30, 2012
There are various alternatives. Command line arguments, fgets etc.Are you sure? This action cannot be undone. -
Member • Aug 30, 2012
no i dont know itVishal0203Is this your doubt? or you already know it?
actually some one had asked me to do this
but i was failedAre you sure? This action cannot be undone. -
Member • Aug 30, 2012
sir[Prototype]There are various alternatives. Command line arguments, fgets etc.
can u please write the whole programAre you sure? This action cannot be undone. -
Member • Aug 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. -
Member • Aug 30, 2012
no i dont know it
Note:: I'm not using scanf() but I'm using fscanf().. is that okay??
actually some one had asked me to do this
but i was failed
#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. -
Member • Aug 30, 2012
i think its not correct ans. As scanf n fscanf() belong to same familyAre you sure? This action cannot be undone. -
Member • Aug 30, 2012
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.sndgpr26sir
can u please write the whole program
Well, you gonna do the same with command line arguments i.e. convert a string into a integer. Its surely a possible option.Vishal0203wrong info:
you cannot do it by fgets()Are you sure? This action cannot be undone. -
Member • Aug 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. -
Member • Aug 31, 2012
what conversion you're talking about?? Will you lease elaborate and educate me about it??[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.
#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. -
Member • Aug 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. -
Member • Aug 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. -
Member • Aug 31, 2012
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.Vishal0203Can you tell me what this part actually does?? ? that was cool!!
I've done a typecast to char* because atoi requires parameter as char* & not char.Are you sure? This action cannot be undone. -
Member • Aug 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. -
Member • Aug 31, 2012
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.Vishal0203I 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. -
Member • Aug 31, 2012
okay!! I surfed the net for strtol() 😀
really cool!
thanks to you!! 😀Are you sure? This action cannot be undone. -
Member • Aug 31, 2012
y r u asking?if u used this code then definitely u must run it.wat was the result?Vishal0203Note:: 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. -
Member • Aug 31, 2012
result was fine..sulochana anandy r u asking?if u used this code then definitely u must run it.wat was the result?
I'm getting the desired result.
but I'm using fscanf() which belongs to family of scanf(). That's the reason I askedAre you sure? This action cannot be undone. -
Member • Aug 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. -
Member • Aug 31, 2012
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 .Vishal0203result was fine..
I'm getting the desired result.
but I'm using fscanf() which belongs to family of scanf(). That's the reason I askedAre you sure? This action cannot be undone. -
Member • Sep 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. -
Member • Sep 1, 2012
you're right but, the ques is about the function and as far as i know, scanf() fscanf() both belong to same family.sulochana anandthats 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 .
anyways i got to learn a new thing 😀 atoi() i didn't know about it!Are you sure? This action cannot be undone. -
Member • Sep 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. -
Member • Sep 1, 2012
Those are C++ functions. This discussion is about C.PriyaJHow 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. -
Member • Sep 1, 2012
Oh k.Are you sure? This action cannot be undone.