-
how to check for chars?
Okay, I have to somehow check an integer value for a character or punctuation character. The catch is that the variable has already been read in; therefor I cannot use getch() or.. can I???
Okay, as you can see below, I read in a value and store it as a float, after I store it as a float, I check and see if it is actually a float (double) value. If it is .0, then I know it is an integer so I convert the float to an integer and return 0 to main() to let it know that the user has entered a valid integer.
currently I have the following which checks for float values and it works as long as a period is not entered or a character is not entered. Any ideas ? Suggestions? Again, the value is set and I cannot use getch unless I can take it from a pre-assigned variable.
Code:
int input_validate() {
int guess;
double temp=12,temp2,i_part;
temp2=temp;
if (modf(temp,&i_part)>0.) /* Checks for a double value */
return -1;
else {
guess=(int)temp2; /*Stores the temp value into guess as Integer */
if ((guess<100)&&(guess>=0))
return 1;
else
return -1;
}
}
Any ideas how i can check for chars and punctuation??? Thanks.
-
Try reading value into a string, then parsing it and converting it using atof() or atoi().
-
RE: KEN
Okay, would this work correctly?
inside of a function of course:
Code:
int var1;
char strg[10];
strg=var1;
now my question is, how would I parse each individual character from that string? I understand a for loop and or a while loop, but how would I break it apart in the first place?
-
>>how would I parse each individual character from that string?
This prints each character one at a time. You can change it to do whatever you want.
Code:
for (i = 0: i < Length; i++)
{
putchar(mystring[i]);
}
-
Well it seems that you could benefit from the ctype library. There you will find such functions as isalpha(), isspace() and so forth.
-
string problem
how do i get a double variable stored into a string? I cannot just let the string get thh double.
here's what i have: assume var's initialized and declared:
Code:
double temp=90;
char temp_str[5]
temp_str=temp;
/* OTHER CODE HERE */
for(i=0; i<N; i++) {
if(isdigit(temp_str[i])!=0)
return 0;
else
return -1;
}
-
>>how do i get a double variable stored into a string?
Lookup sprintf()