|
Post by loyalgurl on Sept 14, 2005 21:37:13 GMT 5.5
hi all any C programmers here? i need sum help plz.... i need some help with how to actually deal with the input data... for example, we ask the user to input a number.... how do we judge that whether the number is an integer, float, or a long integer? n also can we input numbers using the getchar() function? how will that be done? thanx a lot for ur help...an early help will be highly appreciated... thanx
|
|
|
Post by Saurabh Jain on Sept 15, 2005 11:51:10 GMT 5.5
Hi loyalgurl
Let me try look if ur data type u used is intiger type to store the asked value it willalways store intiger
So first we have to take data type float which to ask user to enter value
float num; int n,b; scanf("%f",$num); n=num - 0; if(n>0) printf("It is not intiger") if(n=0) printf("number is iniger");
this code can differentiate in number and intiger type
ur second querry abt getchar
getchar means get a char
if u wnat to get char
char a; a =getchar();
above code will store value in a
try it
|
|
|
Post by loyalgurl on Sept 15, 2005 20:17:53 GMT 5.5
hi saurabh thanx for trying......things r more complicated than this....i cannot use scanf function.....i hav to use getchar to input the number....o.k... n after that i hav to store it in a string..........or an array......n then chek whether it is an integer, a float, a long or a double? another consideration is that i am forbiden to use the atoi() fn.....which converts into an integer.... please try to help again.? thanx a ton..... bye for now take care
|
|
|
Post by uknowho on Sept 16, 2005 11:23:49 GMT 5.5
Hi ....
This works .....
#include <stdio.h> #include <conio.h>
void main() { char num_char='0'; int i=0,j=0,k,float_sig=0; char num_array[10] = {'0'}; int num=0; float number=0;
printf("\n Enter the number : ");
/* to get the number in the Array */ while(num_char != '\n') { num_char= getchar(); num_array=num_char; i++; } i--;
/* // printf("\n the number is : "); while (j!=i) { putchar(num_array[j]); j++; } printf("\n"); */
j=0; k=0; /* To decide whether the Array has a float or a int */ while (j!=i) { if(num_array[j]== '.' || float_sig == 1) { float_sig = 1; k++; } j++; }
k--; j=0;
/* to convert the array content into a float */ while (j!=i) { if(num_array[j]== '.') { j++; }else { number = (number*10); num = (int)num_array[j] - 48; number = number + num; j++; } }
/* to take care of the decimal, iff the array was a float */ if (float_sig==1) { number = number / (k*10); }
/* the final number */ printf("\n The number = %f", number); }
Hope this is what you wanted....
|
|
|
Post by Saurabh Jain on Sept 16, 2005 12:17:29 GMT 5.5
Hi loyalgul
try this
# include<stdio.h> #include <conio.h> void main() { char str[30],key; int i=0,number;
while(1) { key=getche(); if(key=='\r')
break;
else { str=key; i++; }
} str='\0';
number = atoi(str); printf("%d",number);
getch(); }
In above program \r is used to check if enter key is pressed that means end of number been typed
Look dear theer r 3 functions getch getchar and getche all take 1 character at a time but u ahve to press enter for getchar to enter single character in getch and getche enter key is not needed difference in these two is getch does not display on screen while getche displays on screen which is to be stored
look above example will do of intiger as number is type of inbtiger
u can also do same way in float for that u ahve to use another command like atoi
i think its atof
hope ull be able to solve by combination of 2 solutions that i give
God day
|
|
|
Post by loyalgurl on Sept 19, 2005 0:10:39 GMT 5.5
hi uknowho n saurabh thanx a lot for ur help yes it ws a lot of help.....n it did get me out of the problem.. thanx again well i m hoping that i might be needing ur help again in C/... before i say bye can u pls tell me is there any function in C that does the same work as exit(0) in process.h header file in C++ ? thanx a lot bye for now take care
|
|
|
Post by Saurabh Jain on Sept 21, 2005 10:44:48 GMT 5.5
Once again a smart Hello to loyalgul as i remember thers 1 function abort try it
Hey always use tubo c++ compiler Help ther is index of all functions and its also shows simillar functions like See also and it has an example of all functions
feel free to ask ur more querries Hope frds here in spicy can solve ur querries me not expert as me not [proffesionally related to comp.... when ever u asked ur new querry always ask by new thread so that frds can know that new question has been posted Chao Saurabh
|
|