hey there,
i have a small question
i will be reading two integers using scanf..
how do i check whether i have more than two int or less than two int or not even ints ?
hey there,
i have a small question
i will be reading two integers using scanf..
how do i check whether i have more than two int or less than two int or not even ints ?
I am using :
----------------------------------------------------------
int n1;
int n2;
printf("Enter two non-negative numbers :");
scanf("%d%d" , &n1, &n2);
----------------------------------------------------------
but i want to display an error message if I have more than 2 ints inputted / less than 2 ints inputted / something other than ints inputted .
scanf cant perform good validation, check this out:
its mostly code, so if you want more info, try a board search on
validation
also, using separate calls to scanf will improve reading in the
correct amount of numbers
No No's:
fflush (stdin); gets (); void main ();
Goodies:
Example of fgets (); The FAQ, C/C++ Reference
My Gear:
OS - Windows XP
IDE - MS Visual C++ 2008 Express Edition
ASCII stupid question, get a stupid ANSI
another thing.....
if the input is legal but the entered n1 or n2 or the calculated output value is out of the range of an unsigned long int value, then i would like to print a corresponding output message but how would i do the calculation ?
Here's what i have. if the number exceeds the maximum allowed
number by the data type, then the value is not stored in the
variable. there is no way of knowing if this will happen when it
happens, your variable will have some junk value that it had in
the first place. So how can you attempt to take into account of
this? cast your calculation to a larger data type, say double or
even a long double if thats bigger on your system. find out the
maximum value that can be stored by your long int, and if
the value of the calculation (when cast to double) exceeds that
number, you can print a message. see this quick program:
also try this link on maximum values for data typesCode:#include <stdio.h> #define MAXIMUM 2147483647.0 int main (void) { double x; long int y; printf ("Enter a number: "); scanf ("%lf", &x); if (x > MAXIMUM) printf ("\nError - Overflow in data type long int ... Exiting\n\n"); else { printf ("\nIn range ... Casting value to correct type\n\n"); y = (long int)x; printf ("Value is: %d\n\n", y); } return 0; }
No No's:
fflush (stdin); gets (); void main ();
Goodies:
Example of fgets (); The FAQ, C/C++ Reference
My Gear:
OS - Windows XP
IDE - MS Visual C++ 2008 Express Edition
ASCII stupid question, get a stupid ANSI
We can read input from scanf function.
scanf function return the number of input items successfully assigned.
Code:#include<stdio.h> main() { int x1,x2,result; printf("Enter two integers"); result= scanf("%d %d",&x1,&x2); //Read two integers from stdinput printf("Scanf returns %d",result); //print no of successful integer inputs }
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.