-3

My program is ignoring my scanf statement before the if statement that would take user input to close the program or to continue by adding new information(at the end of the while (1) inside of main).

int main (void)
{
while (1)
    {
        float weight = 0;   
        char animal , b;

        printf("Zoo Food Calculator: \n\n");
        printf("What is the animal type? \n [M]Mammal or [A]Amphibian or [R]Reptile: \n");
        scanf("%c", &animal);

        printf("What is the weight of the animal in pounds?:\n");
        scanf("%f", &weight);

        weight = round(weight);

        printf("weight in pounds: %f\n", weight);

        if (animal == 'M')
        {
             Mammals (weight) ;
             printf("For this animal you'll need %f lbs of food a week!\n", Mammals(weight));
        }
        else if (animal == 'A')
        {
        Amphibians (weight) ;
        printf("For this animal you'll need %f lbs of food a week!\n", 
Amphibians(weight));
        }

    else 
        {
            Reptiles (weight) ;
            printf("For this animal you'll need %f lbs of food a 
week!\n", Reptiles(weight));
        }

        printf("Do you want to input new information? Y/N \n");
        scanf("%c", &b);

        if (b == 'N' || b == 'n')
        {
            break;
        }    
     }
    return 0; 
}
  • 2
    You are calling them 2x Once before the `printf` and once during. Store the returned value and use that in the `printf`. – 001 Mar 29 '18 at 00:07
  • Because you're calling each of them twice. Read your code. Remember a function call is `thefunction(theparam)`. Now read your code again. If that's not enough, use a debugger to step through it to see what's happening. – Ken White Mar 29 '18 at 00:07
  • BTW, voting to close as a typographical error or unable to reproduce. *Why are my functions being called twice when I call them twice?* isn't really a question that's going to be of use to future readers. – Ken White Mar 29 '18 at 00:09
  • Also [The program doesn't stop on scanf("%c", &ch) line, why?](//stackoverflow.com/q/20306659) – 001 Mar 29 '18 at 00:12
  • When `scanf()` processes the number, it leaves the newline in the input buffer, unread. Your `"%c"` format happily reads the newline; the newline is neither `'N'` nor `'n'` so you are deemed to want to put in more information. Use `" %c"` in both places where you use `"%c"`; that will skip optional white space and wait for new input that is not a white space character. – Jonathan Leffler Mar 29 '18 at 02:53

2 Answers2

0

I'm not sure why my program is running the functions (Mammals, Amphibians, Reptiles) twice.

You are calling the functions twice:

Mammals (weight);  //<-- first time
printf("For this animal you'll need %f lbs of food a week!\n", Mammals(weight));
//                                                             ^
//                                                         second time

It is also ignoring my scanf statement before the if statement that would take user input to close

That's because the newline character is still in the input buffer after this scanf:

scanf("%f", &weight);

%f converts the float but the newline (entered when ENTER is pressed) is not part of the float, so it remains in the input buffer. The next scanf

scanf("%c", &b);

reads the newline that was in the input buffer, that's why it seems that "it has been ignored". You have to "clear" the input buffer, you can use this function:

void clear_stdin(void)
{
    int c;
    while((c = getchar()) != '\n' && c != EOF);
}

and call it after scanf("%f", &weight);, like this:

printf("What is the weight of the animal in pounds?:\n");
scanf("%f", &weight);
clear_stdin();

Another alternative if you don't want to use the clear_stdin function, would be to add an empty space in the scanf format like this:

scanf(" %c", &b);

with the empty space scanf will ignore the empty spaces, newline and tabs in the input buffer. For more information about the format string for scanf, please take a look at the documentation of scanf.

Pablo
  • 13,271
  • 4
  • 39
  • 59
  • Thanks, a bunch. I thought that as long as i passed an initialized variable into the function's parameters the printf would recognize that all i am asking for is the return value. – CuriousNemo Mar 29 '18 at 00:24
  • 1
    @CuriousNemo the computer executes exactly that what you tell him to do, no function does look ahead and tries to guess what the programmer intentions might be. If you call a function twice, it gets executed twice. – Pablo Mar 29 '18 at 00:34
0

Here is a corrected version:

void flush() //to clear the buffer to remove whitespaces after your input
{
    int n;
    while((n = getchar()) != 10 && n != -1);
}

int main ()
{
    for(;;) //this is a 'better' version of while(1), according to some
    {
        float weight = 0;
        char animal , b;

        printf("Zoo Food Calculator: \n\n");
        printf("What is the animal type? \n [M]Mammal or [A]Amphibian or [R]Reptile: \n");
        scanf("%c", &animal);

        flush();

        printf("What is the weight of the animal in pounds?:\n");
        scanf("%f", &weight);

        flush(); //to remove the Line Feed [Carriage Return]

        weight = (float) round((double) weight);

        printf("weight in pounds: %f\n", weight);

        float food; //we use this as variable for the result of your functions

        if (animal == 'M')
        {
            food = Mammals (weight); //you need to save the return value
            printf("For this animal you'll need %f lbs of food a week!\n", food);
        }
        else if (animal == 'A')
        {
            food = Amphibians (weight);
            printf("For this animal you'll need %f lbs of food a week!\n", food);
        }

        else
        {
            food = Reptiles (weight);
            printf("For this animal you'll need %f lbs of food a week!\n", food);
        }

        printf("Do you want to input new information? Y/N \n");
        scanf("%c", &b);

        flush();

        if (b == 'N' || b == 'n')
        {
            break;
        }
    }
    return 0;
}

instead of saving the return value under 'food' and afterwards using that in the printf, you could also use the printf u had but remove the first call.

lennihein
  • 81
  • 5