-2

I'm trying to make a calculation in C and trying to return the result value of a mathematical calculation but nothing prints out to screen when passing to main fuction. I have tried various methods but something is missing. Can you help me correct the code in a way that will help me in the future to write more efficient code when passing arguments to main? Thanks in advance!

#include <stdio.h>

#define     PI      3.14159
#define     REM     144.612925

static double Raden_CALC(double imp)
{

    double result;
    result = ((imp + imp) * PI) / REM;
    return result;
}

main()
{

    double number;
    printf("Enter a number: ");

    fflush(stdin);
    scanf("%.4lf", &number);

    Raden_CALC(number);
    printf("\nResult: ", number);
}
knoxgon
  • 1,070
  • 2
  • 15
  • 31
  • 1
    You ignored the returned value. The function doesn't (can't) alter the input parameter. – Jonathan Leffler Mar 05 '15 at 15:17
  • 1
    Be very wary of [Using `fflush(stdin)`](http://stackoverflow.com/questions/2979209/using-fflushstdin). It is, at best, not portable. Also, to ensure output appears in a timely manner, make sure outputs end with a newline: `printf("Result: %f\n", number);`. – Jonathan Leffler Mar 05 '15 at 15:18
  • …so, for example, instead of relying on this horrible piece of undefined behavior and the only slightly less horrible `scanf()`, you could (should) use `fgets()` and `strtod()`. – The Paramagnetic Croissant Mar 05 '15 at 15:19
  • Thank you everyone! I don't usually commit that mistake but as a human being I keep forgetting adding % in the printf. Thanks yet again! – knoxgon Mar 05 '15 at 15:30

4 Answers4

2
int main()
{
    double number;
    printf("Enter a number: ");

    fflush(stdin);
    scanf("%.4lf", &number);

    number = Raden_CALC(number); // <-- Assign the return value to number 
    printf("\nResult: %f\n", number); // <-- Add %f to print the number
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
chmike
  • 20,922
  • 21
  • 83
  • 106
2

You're not using the value from the function... change:

Raden_CALC(number);
printf("\nResult: %lf", number);

to

number = Raden_CALC(number);
printf("\nResult: %lf", number);

or

printf("\nResult: %lf", Raden_CALC(number));
jpw
  • 44,361
  • 6
  • 66
  • 86
1

If you return something from a function you need to use the value returned.

double result=Raden_CALC(number);
printf("Result: %lf\n", result);

That might be a crazy explanation, but I'll try. If you ask someone "Please give me an apple", and he gives you one, what will you do? Here you're just ignoring it.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
1

Maybe you want to modify the value in the function pass it's address and use the pointer to alter the data like this

void Raden_CALC(double *number)
 {
    *number = 2 * (*number * PI) / REM;
 }

and then in main()1

int main()
 {
    double number;

    printf("Enter a number: ");
    if (scanf("%.4lf", &number) == 1)
     {
        Raden_CALC(&number);
        printf("\nResult: %f", number);
     }
    return 0;
 }

also, don't fflush(stdin) because it's undefined behavior, and printf() cannot work as you used it, you need to pass a format specifier just like you do in scanf() and then it will replace them with the arguments you pass, in this case number.

You should also, check that scanf() worked and the input was valid, otherwise you will use an uninitialized value which causes undefined behavior.

The following was extracted from the C11 standard draft 1570:

7.21.5.2 The fflush function

Synopsis

   #include <stdio.h>
   int fflush(FILE *stream);
  1. If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.

To achieve the effect you think fflush(stdin) has you can use this

#define flushinput() do {                                    \
        int chr;                                             \
        while (((chr = getchar()) != EOF) && (chr != '\n')); \
    } while (0)

and then after scanf()ing use the macro as if it was a function.


1Note that main() returns int.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • So using fflush with characters and strings is best right? Just like the following example: scanf("%[^\n]", name); fflush(stdin); printf("Name: %s", name); – knoxgon Mar 05 '15 at 15:31
  • NO `fflush(stdin)` is always wrong because `fflush()` is defined for output streams only. – Iharob Al Asimi Mar 05 '15 at 15:32
  • Yes but sometimes an error occurs for example when asking for 4 people to enter their names if not fflush is used then the compiler automaticly skips other scanf() and printout only the first scanf. I do understand now when reading about it but any better solutions? – knoxgon Mar 05 '15 at 15:41
  • That is not true, have you tried it? the only problems with `scanf()` is how it's used in the wrong way most of the time, ignoring it's returned value, and forgetting that it leaves the trailing `'\n'` characters, which causes some wierd behavior but you can always remove it with `getchar()`, `fflush(stdin)` is simply wrong. – Iharob Al Asimi Mar 05 '15 at 15:44
  • Yes but can you try following code and give the feedback. Because it acted exactly the way I assumed it would. Skipped all scanf and printed out the all printf and didn't even asked to enter remaning 3 scanf. #include main() { char name[50]; char name1[50]; char name2[50]; char name3[50]; printf("Enter your name: "); scanf("%[^\n]", name); printf("Enter your name: "); scanf("%[^\n]", name1); printf("Enter your name: "); scanf("%[^\n]", name2); printf("Enter your name: "); scanf("%[^\n]", name3); } – knoxgon Mar 05 '15 at 15:49
  • It doesn't work with `fflush(stdin)` it works if you gather the `'\n'` that was left in the input buffer like this `int chr; while (((chr = getchar()) != EOF) && (chr != '\n'));`, inserting it after every `scanf()` does the job, and in this case it would work better with `fgets()`. – Iharob Al Asimi Mar 05 '15 at 15:53
  • ah all right, just noted but frankly I still haven't experienced anything wrong with fflush alongside with scanf string input but if you say it's not recommended then I do believe in it! After all you're an experienced programmers – knoxgon Mar 05 '15 at 15:57
  • @CalFox I don't say it's not recommended the standard says it's undefined behavior. – Iharob Al Asimi Mar 05 '15 at 15:58