Home > front end >  Visual studio is skipping the second scanf statement
Visual studio is skipping the second scanf statement

Time:10-05

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void) {
    float value1, value2;
    double returnValue; /* declaring the output of lollygag*/   /*declares the values that will be filled with user input*/
    printf("Enter a decimal value:  ");
    scanf(" %f ", &value1);

    printf("Enter the second decimal value:  ");
    scanf(" %f ", &value2);
    lollygag(value1, value2); /*calling the lollygag fuction in order to preform the operation*/
    printf(" %f ", returnValue); /*displays our final answer*/

}

float lollygag(returnValue, value1, value2) {


    if (value1==0 || value2==0)  /*tests to see if one of the values is 0*/
    {
        return -1.0;
        
    }
    else /* if neither value is 0 it will prefrom this operation*/
    {
        returnValue = value1 / value2; /*redefines retrunValue with our solution*/
    }
    return returnValue;
}

I use visual studio and when I run the program it basically skips the second scanf if I see a float value. It works just fine if I type an int. What would be causing this?

CodePudding user response:

The problem is that you are adding spaces around your input format specifiers. Don’t do that.

Additionally, you are:

  • Mixing floating point types. Pick one and stick to it.
  • Not prototyping functions before you use them. (I always recommend declaring the entire function before you use it.)
  • You need to read up more on using functions.

This should help:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

float lollygag(float value1, float value2);  // function prototype

int main(void) {
    float value1;
    printf("Enter a decimal value:  ");
    scanf("%f", &value1);  // no spaces

    float value2;
    printf("Enter the second decimal value:  ");
    scanf("%f", &value2);
    
    float returnValue = lollygag(value1, value2); /*calling the lollygag fuction in order to preform the operation*/
    printf("%f\n", returnValue); /*displays our final answer*/

}

float lollygag(float value1, float value2) {
    float returnValue;

    if (value1==0 || value2==0)  /*tests to see if one of the values is 0*/
    {
        return -1.0;
        
    }
    else /* if neither value is 0 it will prefrom this operation*/
    {
        returnValue = value1 / value2; /*redefines retrunValue with our solution*/
    }
    return returnValue;
}

Oh, yeah, almost forgot. When compiling, always turn up the warning level to the highest you can within reason. I always compile with MSVC using warning level 4 (/W4). You could do just fine with warning level 3. Somewhere in the VSCode options/settings/whatever (I have never used it) there is a spot where you can set that for the compiler. I just re-read the question, and see you are using VS. In VS right click on your project options and update the compile flags.


Sorry, one more edit: There are _two_ `returnValue` objects (variables) in the above program.
  • One is declared in main(). It is assigned the result of calling lollygag().
  • The other is local to lollygag().

They share the same name, but are not the same thing. You could easily change all the returnValues in lollygag() to quux and the program would work the same.

The same thing holds true for argument names. The argument value1 is a different object than the value1 variable in main().

Again, read up more on functions and this will eventually make sense.

  •  Tags:  
  • c
  • Related