TL;DR :
Is there a type (let's name it convert_type
) that allows to do type casting without loosing information, even when dealing with the float
type ?
Example of usage :
float f1 = 12.34;
long i1 = 32;
convert_type tmp1 = (convert_type) f;
convert_type tmp2 = (convert_type) i;
float f2 = (float) tmp1; // should be 12.34
long i2 = (long) tmp2; // should be 32
CONTEXT :
I want to create such a function :
convert_type ask(char* question, val_type input_type)
with the type val_type
defined by :
typedef enum {
INT_T,
STR_T,
CHAR_T,
FLOAT_T
} val_type;
The argument question
has a string to print in order to ask the input.
The idea of this function is to ask in the stdin
an input of type input_type
, and ask again until the input is valid.
Example of usage of ask()
:
int main(int argc, char const *argv[]) {
int nb = (int) ask("How many tries to you want ?\n --> ", T_INT);
char * message = (char*) ask("What is the victory message ?\n --> ", T_STR);
float prob = (float) ask("What is the probability of winning ?\n --> ", T_FLOAT);
printf("\n\n");
printf("Number of tries : %d\n", nb);
printf("Victory message : %s\n", message);
printf("Probability : %f\n", prob);
return 0;
}
I implemented it using the type unsigned long
, and I have trouble with float
type.
Example of execution :
How many tries to you want ?
--> many
The input must be a valid integer.
How many tries to you want ?
--> -2131
What is the victory message ?
--> We are the champions !!!
What is the probability of winning ?
--> 3.4.3
The input must be a valid float value.
What is the probability of winning ?
--> 2.54
Number of tries : -2131
Victory message : We are the champions !!!
Probability : 2.000000
It is noticeable that unsigned long
doesn't work well with float
type (the 2.54
became 2.00
because unsigned long contains an integer value I guess).
What type can I use instead ?