0
#include <stdio.h>
#include <stdlib.h>

int main()
{
    float grade1 = 0.0;
    float grade2 = 0.0;
    float grade3 = 0.0;

    printf("Enter your three test grades in decimal form: \n");

    scanf(" %f", grade1);
    scanf(" %f", grade2);
    scanf(" %f", grade3);

    float avg = (grade1 + grade2 + grade3) / 3;
    printf("Average: %.2f \n", avg);

    system("pause");
    return 0;
}

I am using a tutorial on the new boston YT channel and this code does not work on my compiler while tutorial code does work. I have Visual Studio Community 2015.

Blazikan
  • 13
  • 5
  • 2
    Turn on _all_ compiler warnings: `float grade1 = 0.0; ... scanf(" %f", grade1);` should warn about type mis-match. `float` vs `float*`. – chux - Reinstate Monica Oct 15 '17 at 17:49
  • 3
    ... does the tutorial *really* omit the `&`s? – Weather Vane Oct 15 '17 at 17:49
  • ... the Boston YT channel tutorial I found about `scanf` describes string input, where the `&` is not present, because the array decays to the pointer required. Youtube tutorials might be bettered by one of [these sources](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). – Weather Vane Oct 15 '17 at 17:54

2 Answers2

1

You should scan the address of the float, etc

scanf( " %f", &grade1);
James Maa
  • 1,764
  • 10
  • 20
1

The guide code could not be the same, it would not work. I suggest you to enable the warnings in your compiler, which will save you from really trivial mistakes in the future.

So, the error is here:

scanf(" %f", grade1);
scanf(" %f", grade2);
scanf(" %f", grade3);

The three variables need & operator to receive the value.

scanf(" %f", &grade1);
scanf(" %f", &grade2);
scanf(" %f", &grade3);

I would recommend you to have a look at: https://www.tutorialspoint.com/cprogramming/c_operators.htm

Community
  • 1
  • 1
Fabio
  • 336
  • 3
  • 17