Error :]

#include <stdio.h>
int main(void)
{
int x;
float sum;
float grade;
float avg;
x=0;
sum=0;
scanf("%f",grade);
sum=sum+grade;
x=x+1;
if(x==30)
avg=sum/x;
else if (x!=30)
return 10;
printf("%f",avg);
}




I wrote this one and im running it with Visual studio c++ 2010 and i get this error :
Debug Error!
Program:...jects\(If) Command First Try\Debug\(If) Command First Try.exe
Module:...jects\(If) Command First Try\Debug\(If) Command First Try.exe
File:c:\users\guy's\documents\visual studio 2010\projects\(if)command first try\(if) command first try\if command.cpp
Line:10

Run-Time Check Failure #3 - the variable 'grade' is being used without being initialized

(Press Retry to debug the application)
I have not understood what you are trying to do in the program but you shall change

scanf("%f",grade);

to

scanf("%f", &grade);
oh righttt I forgot it lol thanks bro and but still im running the program with CTRL + F5 and trying to run it and the cmd shows up , i write something , press enter and then it says press any key to continue and it closes.
thats how it looks now:


#include <stdio.h>
int main(void)
{
int x;
float sum;
float grade;
float avg;
x=0;
sum=0;
scanf_s("%f",&grade);
sum=sum+grade;
x=x+1;
if(x==30)
avg=sum/x;
else if (x!=30)
return 10;
printf("%f",avg);
}
What are you awaiting from the program?!!! Take a paper and a pensil and write down what statements will be executed.
I want to calculate the average of 30 students in a class.
Anyone please guys :\?
I already said you take a paper and a pensil and write down step by step what statements will be executed and what result will be obtained.
I have It Though not in your language , look i need to write down grades in cmd when i run the program and untill it gets to 30 grades written , then i need it to calculate the average of the grades. I made some fixes and now the cmd tells me to put in a grade but only one.. and when i put in a grade i get a press any key to continue messege from the cmd and thats how the program looks like now:
#include <stdio.h>
int main(void)
{
int x;
float sum;
float grade;
float avg;
x=0;
sum=0;
printf("Put in a Grade:");
scanf_s("%f",&grade);
sum=sum+grade;
x=x+1;
if(x==30)
avg=sum/x;
else if (x!=30)
return 10;
printf("%f",avg);
}
One more if you do not understand from the first time take a paper and a pensil and write down step by step what statements will be executed and what result will be obtained.
ok i wrote it down step by step man what now...
And what is the result you got?
Last edited on
according to the numbers i put in? i dont know like 582 was the average.
You are telling lie. You did not write down step by step the execution of the program. What is the last satement that will be executed?
Last edited on
The debugger told you exactly what your problem was. I've yet to figure out why this was ignored by your original helper.

the variable 'grade' is being used without being initialized

You must initialize your variables to something. When you declare float grade, it doesn't automatically become 0. It inherits whatever random value already exists in the memory slot it's allocated.

In order to avoid this, you need to initialize your variables to 0.
1
2
3
float sum = 0;
float grade = 0;
float avg = 0;

This should solve your problem.

EDIT:
Also, when posting code please use code tags to preserve tabs and improve readability. You can find them to the right of the text box by clicking the <> button.
Last edited on
@Thumper
The debugger told you exactly what your problem was. I've yet to figure out why this was ignored by your original helper.


Without you and without your debugger this question was already resolved.
Also, vlad from moscow is correct in his response. You should pass a reference to the last call to printf(), although you are not required to pass printf a pointer, you may pass it a value. (so your last printf should work just fine.)

@vlad from moscow
I would argue that the problem was not resolved, considering the OP seemed frustrated and never explicitly said the problem was resolved. In fact, he said the problem wasn't resolved.

Even though you were correct about having to pass a reference to scanf_s(), you completely ignored what the debugger was blatantly telling you. If the OP's variables aren't initialized, this statement: sum=sum+grade; will cause errors because sum wasn't initialized.
Initialize your variables. Always. Period.

EDIT:
@OP
So it asks you to put in a grade? Good, you have that in your code. It only asks you to put in one grade? That's because you only execute that code once.
The reason nothing outputs is because 'x' will always equal 1 (because that code is only executed once), so you never meet your if statement requirements to output something (x == 30). Instead, you return 10, which ends your program and tells the OS that there was a problem.
If you want to take multiple grades from a user, you need to use a loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
float sum = 0, avg = 0;
int x = 0;

bool done = false;
while(!done)
{
    float grade = 0;
    printf("Put in a Grade (enter negative number to end entry): ");
    scanf_s("%f", &grade);

    if(grade < 0)
        done = true; //end loop, invalid grade entered.

    sum += grade; //add grade to sum
    ++x; //increment x
}

//then calculate average after this loop, and output it.  
Last edited on
@Thumper
this statement: sum=sum+grade; will cause errors because sum wasn't initialized.

Here is the very beginning of the program

int x;
float sum;
float grade;
float avg;
x=0;
sum=0;

Where are you seeing that sum is not initialized?

It is a bad advice to initialize always variables and is saying about a bad style of programming. Variables can be assigned so there is no any need to initialize every variable.

Last edited on
@vlad from moscow
You have my apologies. I was incorrect, you were correct. I didn't mean to sound so critical.

It is true that variables can be assigned, but you have to initialize your variables if you're going to use them in a calculation before they're assigned.
Again, i did not see that sum was initialized, and i was incorrect in my statement.

But still, you don't need to pass printf() a reference.
Last edited on
Topic archived. No new replies allowed.