123
123
Last edited by justin69enoch; 01-25-2003 at 08:49 PM.
>>I get a double value from another function
>>and return a value of 1 if it is a double.
Eh?
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
In my program main() not posted due to length... it calls several functions that in turn call functions.
This is a small portion of my last function to write. ANyway, the main program calls this function, yet passes it either a double or an integer depending on the input from the main function. The variable name is temp. This function has to determine if the input has a decimal place in it or not and if it does, it returns a -1; if however it is an integer, it should return a 1.
Does that help??... or even make sense?
There is no function overloading in C, so this is as close as you get without using a structure or a void pointer.
Call it:Code:int function( int i, float f, int status ) { if( status != 0 ) { /* use the integer */ return 1; } else { /* use the float */ } return 0; }
function ( 0, myFloat, 0 ); /*use a float */
function( myInt, 0.0, 1 ); /* use an int */
Quzah.
Hope is the first step on the road to disappointment.
I won't know if it is an integer or a float until i run the function. This function has to determine what it is.
Thanks for the quick responses thus far though. I know I haven't been entirely clear.
>>This function has to determine what it is.
Like quzah said, the function can't determine that, you have to tell it up front. Thats what the parameter types are for.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
How can you not know what you're processing? Surely you're getting the value from some place. If it's user input, validate it first, then pass the correct data to the appropriate function. You can't just not know what you're dealing with and expect it to work.
Example: I have a data file that contains 300 bytes of information. What is it? See how impossible that question is? You have to tell it what you're reading. Bytes are just bytes. They can "be" anything you want. It doesn't mean the values are accurate, just that you can access the bytes of data.
Quzah.
Hope is the first step on the road to disappointment.
The user will input a number. if it is a float, I need to reject the input and tell them that it is invalid; that is basically what I am doing. But the thing is, if I just limit integers from 0 - 500, and someone enters 10.3, it will simply round it to 10 and store 10 as a value rather than reporting an error. ??? any suggestions?
Then your answer is simple. You have to read the input from the keyboard with fgets(), into a char array. Then you perform you own custom validation on the data to ensure its what you expected. Or maybe this function will help you do some of the work too.Originally posted by justin69enoch
The user will input a number. if it is a float, I need to reject the input and tell them that it is invalid; that is basically what I am doing. But the thing is, if I just limit integers from 0 - 500, and someone enters 10.3, it will simply round it to 10 and store 10 as a value rather than reporting an error. ??? any suggestions?
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Okay, this is what I have so far... It only accepts one char per enter... If i use fgets, won't all it do is store the string into one allocated spot in an array? If so, how would I be able to verify that a '.' is not in the string itself?
I know this is a stupid thing for you guys, but I am really trying to learn this stuff. All help is appreciated and again, i thank you for the quick responses. This forum is really great to get help.
Code:#define SIZE 2 int main() { char A[SIZE]; char text; int sum,count=0; printf("Enter Data: "); scanf(" %c", &text); while (count<SIZE) { if (text != '.') { A[count]=text; printf("Checksum: %c %d\n\n",sum,sum); scanf(" %c", &text); count++; } else { printf("this is an error!\n" break; \ } } }
Last edited by justin69enoch; 01-22-2003 at 09:34 PM.
Loop through your char array and use isdigit to determine if the input is a integer.
Try doing a board search, you never know what you might find
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Code:#include <ctype.h> #include <stdio.h> #include <math.h> int is_float(double num); int main(void) { double temp; printf("Enter a integer: "); scanf("%lf",&temp); while (is_float(temp)) { printf("Invalid input. Please enter an integer: "); scanf("%lf",&temp); } return 0; } int is_float(double num) { double ipart; if (modf(num,&ipart) > 0.) return 1; return 0; }