-1

New to programming in general, creating a program in C with VisualStudio that would take a floating point from the user and return if it is negative or not, then get it as an integer, and finally return the first digit of the integer. Code looks something like this:

double extractDigit1(x){
     double userFloatValue = x;
     (userFloatValue >= 0) ? printf(" %f is a positive value.", userFloatValue) :
                             printf(" %f is a negative value.", userFloatValue);
}

int main(){
     double userValue;
     scanf_s("%lf", &userValue);
     extractDigit1(userValue);
}

Entering 1234.345 gets me:

"1202590843.000000 is a positive value."

instead of:

"1234.345 is a positive value."
gustavovelascoh
  • 1,208
  • 1
  • 14
  • 28

2 Answers2

3

The root problem is

  • double extractDigit1(x) is not a correct function prototype, it should be double extractDigit1(double x)

    Wiht what your write, x was considered as int which is not compatible with the rest of code.

Note that

  • Your functions return nothing whereas their prototypes tell so

    It's an undefined behavior that can lead to anything

Finally

Corrected code:

#include <stdio.h>

double extractDigit1(double x){
    double userFloatValue = x;
    (userFloatValue >= 0) ? 
        printf(" %f is a positive value.", userFloatValue) :
        printf(" %f is a negative value.", userFloatValue);
    return 0;
}

int main(){
   double userValue;
   scanf("%lf", &userValue);
   extractDigit1(userValue);
   return 0;
}
Mathieu
  • 8,840
  • 7
  • 32
  • 45
  • Thank you very much this worked. I'll accept your answer in like 6 mins. – flapjacks2341 Jul 08 '19 at 15:09
  • 2
    `scanf_s` is a microsoft function that take more parameters than `scanf`. In `scanf_s`, buffer sizes have to be specified to prevent buffer overflow. So functions are not interchangeable. – Mathieu Jul 08 '19 at 15:15
  • 1
    IIRC, more parameters are required only for format specfiers like `%[`, `%c` and `%s`, right? How is the `scanf_s` a problem in the OP's case? – Spikatrix Jul 08 '19 at 15:16
  • @Spikatrix [Indeed](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/scanf-s-scanf-s-l-wscanf-s-wscanf-s-l?view=vs-2019)! – Mathieu Jul 08 '19 at 15:21
  • But then how is that a problem as your answer states? – Spikatrix Jul 08 '19 at 16:26
  • 1
    @Spikatrix It is not, I was persuaded that `scanf_s` was requiring the buffer size in all cases. The root error is just the `x` variable defined as an `int` since not typed. That said, I discourage the `scanf_s` usage. – Mathieu Jul 08 '19 at 16:30
  • 1
    Yes but your answer stating it as a problem "_You mix `scanf_s` and `scanf`: you called the first like the second_" is not right as the OP's `scanf_s` is perfectly valid – Spikatrix Jul 08 '19 at 16:31
  • Again you're right, I was correcting when you tell me ;-) – Mathieu Jul 08 '19 at 17:03
-2
double userFloatValue = x;
int my_var = (int)userFloatValue ;
         (userFloatValue >= 0) ? printf(" %d is a positive value.",   my_var ) :
                                 printf(" %d is a negative value.",  my_var ) ;

Use scanf rather than scanf_s scanf("%f", &a);

Jin Thakur
  • 2,711
  • 18
  • 15