Hi, im creating some program, which reads number from stdin, and save them into variable X, but how can i make condition that program will print error on the screen if X is not number

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main()
{
  float x;
     while ( (scanf("%e",&x)) != EOF)
     if ( x is not number)
        {
          printf("you didnt entered number");
          break;
        }
          else
          printf("%.10e\n", x);
    return 0;
}

can i make it somehow ? + i would like to work with data type float or doble, not with string or char.

Recommended Answers

All 11 Replies

Dave Sinkula , neither of that links helped me, but i figured it how somehow, const NaN was helpful

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
  float x;
  while (scanf("%e",&x) != EOF)
      if ((fpclassify(x) == FP_NAN))
          {
            printf("shit"); // :-D
            break;
          }
      else
        printf("%.10e\n", x);
        
  return EXIT_SUCCESS;
}

Perhaps I misunderstood your meaning of "not a number"? It sounded to me like you didn't want "1%2r1_9" to be considered a number. Instead you are after particular floating point signal-type values?

Perhaps I misunderstood your meaning of "not a number"? It sounded to me like you didn't want "1%2r1_9" to be considered a number. Instead you are after particular floating point signal-type values?

hm, now i tested my code, and found out, that when I type
íáíáýí, or áýáýž99 it gives me error, but if i write 99íáýíáý program continues... how to deal with this ? :S

-- hmm, ive read those link you have posted, but that function "mygetd" which is mention there, is limited to a known length of char
i need function where length of the input will not be limited

hm, now i tested my code, and found out, that when I type
íáíáýí, or áýáýž99 it gives me error, but if i write 99íáýíáý program continues... how to deal with this ? :S

-- hmm, ive read those link you have posted, but that function "mygetd" which is mention there, is limited to a known length of char
i need function where length of the input will not be limited

Well, if you wanna go all the way with an unlimited input length I'd feel compelled to ask whether C++ might be an option.

Otherwise in C I might choose to discard characters beyond some "absurd length" and continue with a fixed size. Big can o' worms opens up for the perfectionist.

Well, if you wanna go all the way with an unlimited input length I'd feel compelled to ask whether C++ might be an option.

Otherwise in C I might choose to discard characters beyond some "absurd length" and continue with a fixed size. Big can o' worms opens up for the perfectionist.

if i work only with data type float / double, there can be unlimited input, but if i work wich char there is limit..thats clear... thats why i wanna avoid using char in this case, and thats why im searching how to make that program will give error when non-number is entered by some clever algoritm :P

So you think you solved it? Enter... let's say: "H" or better yet "3.s" and tell us back what's the behavior you observed.

So you think you solved it? Enter... let's say: "H" or better yet "3.s" and tell us back what's the behavior you observed.

you probably didnt read whole topic, because as i said
when i enter "s.3" it gives me an error but when i enter "3.s" it doesnt give me error and my program break down...so answer is no..i didnt solve it..thats why im asking for help :)

you probably didnt read whole topic, because as i said
when i enter "s.3" it gives me an error but when i enter "3.s" it doesnt give me error and my program break down...so answer is no..i didnt solve it..thats why im asking for help :)

When I posted the last reply from you, available to me was this http://www.daniweb.com/forums/post1026352.html#post102635.
Afterwards, I saw you and Dave were discussing the errors.

It is my opinion that before you seek to implement "arbitrary length", you need to master the parsing and validating part of the input.

When I posted the last reply from you, available to me was this http://www.daniweb.com/forums/post1026352.html#post102635.
Afterwards, I saw you and Dave were discussing the errors.

ok..nevermind ;) i see no reason to argue :)i just wanna solve this problem, because i cant move on, if i cant even work with data on the input :S

ok..nevermind ;) i see no reason to argue :)i just wanna solve this problem, because i cant move on, if i cant even work with data on the input :S

No, no arguing here. Just explaining. ;)
Parsing the data and validating it, is what you are missing. Obviously, you understand that the programmer loose control of the program when the user can enter anything.
I think Dave's snippets show a good start on it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.