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 SharmaIs this your doubt? or you already know it?
-
[Prototype]There are various alternatives. Command line arguments, fgets etc.
-
sndgpr26
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 failed -
sndgpr26
sir[Prototype]There are various alternatives. Command line arguments, fgets etc.
can u please write the whole program -
Vishal Sharma
There are various alternatives. Command line arguments, fgets
wrong info:
you cannot do it by fgets() -
Vishal Sharma
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; }
-
Vishal Sharmai think its not correct ans. As scanf n fscanf() belong to same family
-
[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.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() -
Vishal Sharma
you gonna do the same with command line arguments
Can you give a small example? -
Vishal Sharma
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!! ๐ -
[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
z = atoi((char*)n);
Can you tell me what this part actually does?? ? that was cool!! -
[Prototype]
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. -
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]
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?? -
Vishal Sharmaokay!! I surfed the net for strtol() ๐
really cool!
thanks to you!! ๐ -
sulochana anand
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; }
-
Vishal Sharma
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 asked -
rahul69Well here is my attempt :
#include
simply gets() can do...๐#include main() { int no,i; char s[40]; gets(s); no=atoi(&s); printf("%d",no); getch(); return 0; } -
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 .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 asked -
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
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! -
Priya NadkarniHow about cin(console input)? Its simple.
Program-
#include
int main()
{
int num;
cin>>num;
cout<return 0;
} -
[Prototype]
Those are C++ functions. This discussion is about C.PriyaJHow about cin(console input)? Its simple.
Program-
#include
int main()
{
int num;
cin>>num;
cout<return 0;
} -
Priya NadkarniOh 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