0
votes

So am trying to learn c by-myself (basically not having any previous experience in any programming language) and now I have some issues with prototyping some of my functions to use in header files.

For the sake of learning I only use the < stdio.h > lib and only use the printf and scanf functions and for now it only prints to console.

I was able to code a working prototype function for my menu that only uses the printf function but the scanf gives me more issues and it just refuses to compile and am having trouble to see where my thinking error is.

my main program:

#include "menu.h"
#include "circlefunctions.h"
#include "input.h"

int main(void){

    float diameter;
    double straal;
    double oppervlakte;
    double omtrek;


        while(1){

            menu();
            user_input();
            system("cls");

        switch(user_input())
        {

    case 1:

        printf(" ----------------------------------------\n");
        printf(" Typ de diameter van de cirkel: ");
        scanf("%g", &diameter);
        printf(" ----------------------------------------\n");

        straal = diameter / 2;
        oppervlakte = PI * (straal * straal);
        omtrek = 2 * PI * straal;

        printf(" De straal = %f \n\n", straal  );
        printf(" De oppervlakte = %f \n\n" , oppervlakte);
        printf(" De omtrek = %f \n" , omtrek);
        printf(" ----------------------------------------\n");

        break;

    case 2:
        return(0);

    case 3:
        return(0);

    case 9:
        return(0);

    case 0:
        return(0);
        }
    }
 return 0;
}

and the stubborn header:

#include <stdio.h>

void user_input();

    void user_input(){
        scanf("%d", &user_input);
  }

The error that I get while trying to compile is in input.h the part with; scanf("%d", &user_input);

errorcode: format '%d' expects argument type of 'int ', but argument 2 has type 'void () ()'. And I also got an error on the switch in the main program that the switch quantity is not an integer. I suspect that this error is related but am not sure. I still have to debug that part but if anyone is willing to point me to the right documentation i would much appreciate it.

And a second question that I have is also related to headers: I have < stdio.h > already included in "menu.h". Would I need to include it again in "input.h"? (if i understand correctly how the preprocessor works i should not have to include it but I can't find anywhere where this is explained in simple terms unfortunately.)

Edit:

Thank you all for providing valuable information.

@zenith Thank you for your example. I hope you don't mind me asking some more.

I have replaced my code with yours in the "input.h" and it will compile and run now. However the behavior has changed. For some unclear reason i now have to input the choice twice before the program accepts my input. So the 1st input gets ignored after an enter and it will only accept the 2nd input.

Could you perhaps point me in the direction what causes this bug? or perhaps point me to some documentation where this is explained? I don't want to take up to much of you valuable time of-course.

Edit 2

Thanks for the reply and info. I got the bug out and it is working as intended(that was silly of me not to see that). And to the rest who replied: Ill take your information of-course and also learn from that. Thank you all!

2
Regarding user_input(). For one thing, you shouldn't have executable code in a header file. For another, you are trying the use the address of the function itself, instead of a providing a function argument.Weather Vane

2 Answers

4
votes

user_input() doesn't return anything, since it's declared void.

But you're trying to use the non-existing return value: switch(user_input()).

This causes undefined behavior.


Additionally, this:

scanf("%d", &user_input);

tries to read an int from stdin and store it in the memory address of the user_input function. Not a good idea. Again, undefined behavior.


What you probably want the function to look like:

int user_input(){
    int number; // store user input to this variable
    scanf("%d", &number);
    return number; // return the user input so that it can be used outside the function
}
0
votes

If you have header files declared in a previous header file. You will not need to include it again in the subsequent included header files. I tend to not include header files in my local *.h files just for that reason. It avoids circular includes if you declare your includes in the .c files as much as possible.

Your scanf function has as its second argument a function of type void(), void(). Meaning it takes no arguments and returns nothing or "void". I think you want your user_input to be a variable of type 'double' that is filled somewhere, maybe via some user input from the console using a call to 'gets' from stdin.

HTH