-4

I try to compile this little simple program, but i get "debug assertion failed", can someone explain why?

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

#define answer 3.14

void main(int argc, char **argv) 
{
     float a = strtod(argv[1], 0);

     printf("You provided the number %f which is ", a);


     if(a < answer)
          puts("too low");
     else if(a > answer)
          puts("too high");
     else if (a == answer)
          puts("correct");
}

How to use:

Open CMD and drag this .exe into it, then write a space followed by a number and hit enter. E.g. C:\test.exe 240

Black
  • 18,150
  • 39
  • 158
  • 271
  • Minor: I wonder why you are mixing `float` with `double`. The value `3.14` is `double` as is the return value from `strtod`. Finally, your last `else if (a == answer)` is unnecessary since the previous tests were not met, and anyway, comparing a real number for equality is not good: especially comparing a `float` with a `double` value. – Weather Vane Nov 07 '15 at 18:37
  • I know that i am mixing float with double, but it works. Yes the last check will not work, but this is another problem. – Black Nov 07 '15 at 18:39
  • 1
    `if (argc > 1) a = strtod(argv[1], 0);` – Weather Vane Nov 07 '15 at 18:42
  • Are you sure it works? I gave the argument `3.14` and it responded `You provided the number 3.140000 which is too high`. Compiled as C. – Weather Vane Nov 07 '15 at 18:44
  • I compile in C++ and use c++. I will edit – Black Nov 07 '15 at 18:45
  • 1
    And `main` must be `int`, not `void`. But I can't see what generates your "debug assertion failed". Indicates your plateform (windows I guess) and compiler. – hexasoft Nov 07 '15 at 18:45
  • @WeatherVane, You are right, it does not work. But the main problem is fixed thx to your code. Thx – Black Nov 07 '15 at 18:47
  • @hexasoft, no it can be void too. There is no problem, it works. – Black Nov 07 '15 at 18:47
  • 3
    In a C++ source code file the includes should be [cstdio](https://msdn.microsoft.com/en-us/library/58dt9f24.aspx) and [cstdlib](https://msdn.microsoft.com/en-us/library/cw48dtx0.aspx) instead of `stdio.h` and `stdlib.h`. – Mofi Nov 07 '15 at 18:49
  • @Mofi, does not seem to be necessary, since it works without too. I also get error when i try to include them. – Black Nov 07 '15 at 18:49
  • @EdwardBlack: I don't said it don't work, I said it is defined this way. Compile with warnings and you will have an explicit message about that. Note that some compilers may treat that as an error. – hexasoft Nov 07 '15 at 18:54
  • @hexasoft, i always use void for `main`, i never get warnings about this. I use Visual Studio 2010 and have all Warnings Enabled (Wall). Actually i learned it in my education that it is not a problem at all to use void for main, and i never ran into any problems. – Black Nov 07 '15 at 18:56
  • Well - your main() is a trivial ten-liner and it seems you have a problem with it. – decltype_auto Nov 07 '15 at 19:01
  • @decltype_auto, it is not a problem which is caused by the `void` ... so what is your point? – Black Nov 07 '15 at 19:03
  • See http://stackoverflow.com/questions/4207134/what-is-the-proper-declaration-of-main and many other about `main` declaration. Being able to do something - even if it works - don't means it's the good way to do it. – hexasoft Nov 07 '15 at 19:04
  • @hexasoft, i never had problems with it ever, so i stick with it, but thx. – Black Nov 07 '15 at 19:04
  • 1
    @EdwardBlack: OK, Edward it you want it more plainly - very well. Your snippet is of lousy code quality, and you reject all good advice you get for free here. Was that enough down to the point? – decltype_auto Nov 07 '15 at 19:08

3 Answers3

1

Look on this rewritten code with comments (not compiled by me at all):

#include <cstdio>    // Include stdio.h  for C++ - see https://msdn.microsoft.com/en-us/library/58dt9f24.aspx
#include <cstdlib>   // Include stdlib.h for C++ - see https://msdn.microsoft.com/en-us/library/cw48dtx0.aspx

#define answer 3.14  // Define value of PI as double value.
                     // With f or F appended, it would be defined as float value.

int main(int argc, char **argv)
{
     if(argc < 2)    // Was the application called without any parameter?
     {
          printf("Please run %s with a floating point number as parameter.\n", argv[0]);
          return 1;
     }

     // Use always double and never float for x86 and x64 processors
     // except you have a really important reason not doing that.
     // See https://msdn.microsoft.com/en-us/library/aa289157.aspx
     // and https://msdn.microsoft.com/en-us/library/aa691146.aspx

     // NULL or nullptr should be used for a null pointer and not 0.
     // See https://msdn.microsoft.com/en-us/library/4ex65770.aspx

     double a = strtod(argv[1], nullptr);

     // %f expects a double!
     printf("You provided the number %f which is ", a);

     // See https://msdn.microsoft.com/en-us/library/c151dt3s.aspx
     if(a < answer)
          puts("too low.\n");
     else if(a > answer)
          puts("too high.\n");
     else
          puts("correct.\n");

     return 0;
}
Mofi
  • 46,139
  • 17
  • 80
  • 143
0

I found a possible solution, but i don't understand why it works. Maybe someone can explain why argc - 2 ?

float a = (argc -2)? 0 : strtod(argv[1], 0);
Black
  • 18,150
  • 39
  • 158
  • 271
  • 1
    it is to verify that the user enter correctly the command: `test.exe someNumber`. That is, two arguments (`argc = 2`) if not, you just consider the number entered is 0. – A.S.H Nov 07 '15 at 19:10
0

This code works perfectly on my setup, if I supply at least one commandline argument. With no arguments it crashes as expected.

pelya
  • 4,326
  • 2
  • 24
  • 23