0

This is for my into to C class, and I can't figure out why I'm getting this error:

while (scanf ("%d", (int)ph[i].vi.temperature) < 0) { 
warning: format '%d' expects argument of type 'int *', but argument 2 has type 'int'

Code:

struct vitalInformation {
    float temperature;
    unsigned int systolicPressure;
    unsigned int diastolicPressure;
};

struct activityInformation {
    unsigned int stepCount;
    unsigned int sleepHours;
};

union patientHealth{
    struct vitalInformation vi;
    struct activityInformation ai;
} ph[100]; 

int i = 0;

int menu(){
    int option;

    printf ("Please enter the number for the desired action (1, 2, 3):\n");
    printf ("1 - Enter some patient vital information\n");
    printf ("2 - Enter some patient activity information\n");
    printf ("3 - Print summary information on the patient information and exit the program\n");

    scanf ("%d", &option);

    while (scanf("%d", &option) || option<1 || option>3) {
        printf ("Please enter 1, 2, or 3\n\n");
        printf ("Please enter the number for the desired action (1, 2, 3):\n");
        printf ("1 - Enter some patient vital information\n");
        printf ("2 - Enter some patient activity information\n)");
        printf ("3 - Print summary information on the patient information and exit the program\n");

        fflush (stdin);
        scanf ("%d", &option);

    }

    return option;
}

void patientVitalInfo(int *countP, float *minT, float *maxT, int *minS, int *maxS, int *minD, int *maxD) {
    printf ("Enter the temperature: ");
    scanf ("%f", &ph[i].vi.temperature);

    while (scanf ("%d", (int)ph[i].vi.temperature) < 0) {
        printf ("Please enter an integral unsigned number\n");
        printf ("Enter the temperature: ");

        fflush (stdin);
        scanf ("%f", &ph[i].vi.temperature);
    }
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Kat
  • 9
  • 1
  • 2
  • `scanf` requires pointers as arguments because it is storing values. You are not passing a pointer which is exactly what the error message says. You appear the have done it twice correctly and once incorrectly, but I'm not sure why you're reading at least 2 values. Seems like a copy/paste and logic error. – Retired Ninja Mar 27 '18 at 00:26
  • 1
    Note that `ph[i].vi.temperature` is a `float`. – chux - Reinstate Monica Mar 27 '18 at 00:27
  • Code later has `scanf ("%f", &ph[i].vi.temperature);`, why not do the same instead of `scanf ("%d", (int)ph[i].vi.temperature)`? – chux - Reinstate Monica Mar 27 '18 at 00:29
  • And don't `fflush (stdin);` -- it isn't defined behavior on most systems and probably isn't doing what you think it is. – David C. Rankin Mar 27 '18 at 00:30
  • What exactly are you trying to accomplish with that `while` loop? Are you attempting to see if the floating point entered is also an integer? If so, that's not how you do it. Change `temperature` to `int` and use `%d` to read it. – dbush Mar 27 '18 at 00:30
  • `while (scanf("%d", &option) || option<1 || option>3)` also doesn't make sense. You might read up on what scanf returns. – Retired Ninja Mar 27 '18 at 00:32
  • I bet this line: `while (scanf ("%d", (int)ph[i].vi.temperature) < 0) {` is supposed to be: `while ( ph[i].vi.temperature < 0.0) {`. – bruceg Mar 27 '18 at 00:39
  • the while loop is supposed to validate the user's input since body temp shouldn't be a neg number, but I may have approached it the wrong way. I also changed temperature to an int and used %d to read it, but it still gives me the same error – Kat Mar 27 '18 at 00:40
  • 1
    @DavidC.Rankin: The behavior of `fflush` for input streams is defined for many of the people asking questions here. It is not defined by the C standard but is [defined by Microsoft](https://msdn.microsoft.com/en-us/library/aa272686(v=vs.60).aspx): “If the stream is open for input, fflush clears the contents of the buffer.” – Eric Postpischil Mar 27 '18 at 00:42
  • I understand -- that's why I said "most". (windoze is just 1 of many, and though it would like to think otherwise -- it doesn't qualify as "most") – David C. Rankin Mar 27 '18 at 00:44
  • @DavidC.Rankin: So stop telling people to remove it. They need it. Their instructor gave them a template or other information instructing them to use it, and they use it to clear buffers when writing their first simple programs. They need to clear the buffers to clear away other characters between their inputs, and the instructor decided it was the simplest way to deal with this for beginners. If they follow your advice to remove it, it is going to break the intended behavior, and they will have to figure out why a later `scanf` appears to be not working. – Eric Postpischil Mar 27 '18 at 00:57
  • @EricPostpischil - Where is the OS defined in the question? Huh? Go do a survey. How many answers on SO explicitly state that `fflush(stdin)` is *Undefined Behavior*? Do you downvote each you find? `fflush(stdin)` is `100%` non-portable and is poor practice and it should not be used, despite what MickeySoft says. – David C. Rankin Mar 27 '18 at 01:00
  • @DavidC.Rankin: The question does not state the OS because the OP is not asking about the OS-specific behavior and because beginners often do not know enough to specify a question completely. They are not asking about `fflush`, it is incidental to their question. **We** are knowledgeable enough to know why it is there, and to know that it is needed in the OP’s situation. Fine, it is non-portable, go ahead and tell them that. But you can say “fflush is not portable” instead of “don’t `fflush(stdin)`”, which will screw up their program and cause them more headaches trying to figure out why. – Eric Postpischil Mar 27 '18 at 01:08
  • Hey, I'm not going to argue the point with you. If you think `fflush(stdin)` is the right way to go, and that's what you want to tell people to use -- more power to you brother. But, you know better, and so do I. – David C. Rankin Mar 27 '18 at 01:16
  • 1
    @DavidC.Rankin: I am not telling people to use it and have not recommend to do so. I am recognizing that the beginners who use it have a reason for doing so and advising them to remove it from their code will cause problems in the situation they are in. There **are** reasons for using non-portable behavior. When somebody calls `pthread_create`, do you warn that it is non-portable behavior, so they should remove it? When somebody calls `CreateWindow`, do you warn that it is non-portable behavior? `fflush(stdin)` is not different from those. It is an extension, which the C standard welcomes. – Eric Postpischil Mar 27 '18 at 01:33
  • I'm just going to chock this conversation up to the upcoming [Blue Moon](https://www.timeanddate.com/moon/phases/). We both know `fflush(stdin)` is bad practice, and will invoke *Undefined Behavior* on a majority of systems where `stdin` is not defined as *"seekable"* for the purposes of `fflush` -- which is about every system except windows. I stand by my comment. Or do we delete [**fflush(stdin); is undefined behaviour, don't do it.**](https://stackoverflow.com/a/38325926/2173917) – David C. Rankin Mar 27 '18 at 02:03
  • @DavidC.Rankin: No, we do not both know `fflush(stdin)` is bad practice. Portability is not always a goal. There are times for using non-portable code, and facilitating student projects is one. Almost no commercially useful C program is entirely portable—doing almost anything other than abstract computation requires interacting with the system in ways not defined by the C standard. You cannot open a GUI window without using features not defined by the C standard. The C standard invites extensions (“A conforming implementation may have extensions…”), and extensions are needed and useful. – Eric Postpischil Mar 27 '18 at 12:06
  • Kat do you have any feedback? – sancho.s ReinstateMonicaCellio Mar 29 '18 at 00:31
  • @sancho.s sorry for the late reply; I just turned it in with some of the modifications you all suggested. Thank you so much to everyone who replied – Kat Apr 12 '18 at 02:59

1 Answers1

2

The error reported comes from your line

while (scanf ("%d", (int)ph[i].vi.temperature) < 0) {

which is casting whatever you get form ph[i].vi.temperature (in this case, a float) into an int, while scanf requires a pointer to an int.

Now, in your case, you seem to need the temperature as an int, while ph[i].vi.temperature holds a float, so you would rather use another int variable, say

int itemp;
scanf ("%d", &itemp);

for the input and then

ph[i].vi.temperature = (float) itemp;

for the casting. Or, you could simply scanf ("%f", &ph[i].vi.temperature); and then keep the integral part. I wouldn't know your needs nor the logic behind your code.

Note: I am not sure you are using the return value of scanf in a way matching your needs either. In your case, scanf can return 0, 1 or EOF.