-
atof, float and structs
Hi,
I'm new here and first one thing... is there any way that i can receive the messages from the board by e-mail???
And now the real reason of this message...
I have a problem using atof() and i can't undestand what's happening since i have a struct and i have used it already with floats and i had no problem... but now when the function begins i receive a message from the watch box telling it's an illegal use of floating points... :o( I have tried to change the way i get the string but it didn't work anyway... i have gotten out the NULL char at the end of the string and nothing, i have changed a lot of things but nothing seems to work out... and i didn't find any useful help in the board yet... can someone help me PLEASEEEEEEEE!!!!
This function will get the information from the keyboard to make me able of doing some operations without knowing how many arguments will be typed
here goes the code:
Code:
#define ENTER 13
#define MAXS 6
#define INTGR 1
#define FLT 2
#define STRG 3
typedef struct tokens{
int telem;
union { int ielem;
float felem;
char opelem;
} tokelem;
}*TOKEN;
TOKEN GetToken (void) {
char c;
TOKEN tok=NULL;
char S[MAXS];
int i = 0;
float fl;
c = getche();
while ( c == ' ') c=getche();
while ( (c != ' ' ) && (c!= ENTER) ){
if (c>='0'&& c<='9'){
while ( c!=' '){
S[i]=c;
i++;
c=getche();
}
S[i]='/0';
tok = malloc (sizeof(struct tokens));
if (strstr(S,".") == NULL ) {
tok->telem = 1;
tok->tokelem.ielem = atoi(S);
}
else {
tok->telem = 2;
fl = atof(S);
tok->tokelem.felem = fl;
}
return tok;
}
else
switch ( c ) {
case '+':
case '-':
case '/':
case '*': tok->telem = 3;
tok->tokelem.opelem = c;
return tok;
default : GetToken(); break;
}
}
tok = NULL;
return tok;
}
Code Tags added by Kermi3
-
Code Tags
I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you ;)
For example:
Without code tags:
for(int i=0;i<5;i++)
{
cout << "No code tags are bad";
}
With Code Tags:
Code:
for(int i=0;i<5;i++)
{
cout << "This code is easy to read";
}
This is of course a basic example...more complicated code is even easier to read with code tags than without.
I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found on the code tag post at the top of every forum. I also suggest you take a look at the board guildlines if you have not done so already.
This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.
If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.
Good Luck with your program,
Kermi3
Lead Moderator
-
@frigga: Where does it say the error is? Can you cut/paste the error message?
-
the error happens when i'm running it when it tries to convert the string to float (you can see i even tried to get the value in another float to see if the problem was in my struct)
when running
fl = atof(S);
it gives me a message: scanf: floating point formats not linked
Abnormal program termination
i'm using Turbo C 2.01 and i don't know to fix it really :o(
I have seen a message telling about it after i had posted this message but i don't know how to fix it :o/
(I think i need another compiler... :o/)
-