-
November 12th, 2012, 02:21 AM
#1
Problem With Declaring Variables In A Function
This is a c program that is failing to compile. The error occurs in the calcLabs() function. The error called out is (btw, I'm using VS 2010): Error 4 error C2143: syntax error : missing ';' before 'type'
I don't understand why the compiler is not letting me declare variables in the calcLabs() function!
Code:
#include<stdio.h>
#include<stdlib.h>
void calcPercent(double *);
double calcLabs();
double calcExams();
double calcFinal();
char calcLetter(float);
main()
{
char fullname[16];
double perGrade = 100;
char letGrade = 'A';
int scount = 0;
FILE * fptr;
fptr = fopen("grades.txt", "w");
if(fptr == NULL)
{
//message exit
}
fprintf(fptr, " GRADES REPORT\n\n" );
fprintf(fptr, " NAME %%GRADE LETTER\n" );
printf("\n Enter a student or 0 to exit: ");
scanf("%15[^\n]s", fullname);
fflush(stdin);
while(fullname[0]!='0')
{
printf("\n Processing ...");
calcPercent(&perGrade);
//calcLetter();
fprintf(fptr, "\n %-16s%9.2f%4c ", fullname, perGrade, letGrade);
scount++;
printf("\n Enter a student or 0 to exit: ");
scanf("%15[^\n]s", fullname);
fflush(stdin);
}
fprintf(fptr, "\n\n Student count is %d ", scount);
fclose(fptr);
return(0);
}
void calcPercent(double * ptr_perGrade)
{
double labTotal;
double examTotal;
double finalTotal;
labTotal = calcLabs();
examTotal = calcExams();
finalTotal = calcFinal();
//*ptr_perGrade = labTotal+examTotal+finalTotal;
}
double calcLabs(void)
{
printf("Got here calcLabs");
double score = 0.0; // THIS IS THE LINE CALLED OUT IN THE ERROR
/** double sum = 0.0;
double average = 0.0;
double result = 0.0;
for(int i = 0; i < 8; i++)
{
printf("\n Enter Lab: ");
scanf("%f", &score);
sum += score;
}
average = sum / 8;
result = average * .4;
**/
return 0.0;
}
double calcExams()
{
printf("Got here calcExams");
return 0.0;
}
double calcFinal()
{
printf("Got here calcFinal");
return 0.0;
}
char calcLetter()
{
printf("Got here calcLetter");
return 'a';
}
-
November 12th, 2012, 04:40 AM
#2
Re: Problem With Declaring Variables In A Function
The only error I get by compiling your code in VS 2010 (with SP1) is this one:
Code:
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
- because of the wrong signature for main(): main() must be declared as int, not void.
Last edited by VictorN; November 12th, 2012 at 04:45 AM.
Victor Nijegorodov
-
November 12th, 2012, 04:42 AM
#3
Re: Problem With Declaring Variables In A Function
Edit: didn't notice this was about C, not C++.
If I compile your code with the Comeau online compiler, I only get one error (and some warnings).
Code:
Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
Copyright 1988-2008 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions
"ComeauTest.c", line 10: error: return type "int" omitted in declaration of function
"main", so use int main() OR int main(int argc, char *argv[])
main()
^
...
1 error detected in the compilation of "ComeauTest.c".
Are you sure you compiled the exact code you posted?
Last edited by D_Drmmr; November 12th, 2012 at 12:30 PM.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
November 12th, 2012, 05:08 AM
#4
Re: Problem With Declaring Variables In A Function
Originally Posted by spiritualfields
This is a c program that is failing to compile.
You need to review the rules of the 'C' language when it comes to declaring variables. The rules are not the same as C++:
Code:
double calcLabs(void)
{
printf("Got here calcLabs");
double score = 0.0; // THIS IS THE LINE CALLED OUT IN THE ERROR
In the 'C' language, all variables within a block of code must be declared before any executable statements. With C++, you can basically declare variables anywhere within the block of code, not so with 'C'.
Correction:
Code:
double calcLabs(void)
{
double score = 0.0;
printf("Got here calcLabs");
Note now that the declaration appears before the printf(). All variables in that block must be declared before the call to printf().
Regards,
Paul McKenzie
-
November 12th, 2012, 07:38 AM
#5
Re: Problem With Declaring Variables In A Function
Yep, the stack space has to be allocated all at once in the beginning of the scope. I'm very surprised that Visual C still does that though, almost all other C compilers allow you to have C++ style declarations of variables.
-
November 12th, 2012, 09:19 PM
#6
Re: Problem With Declaring Variables In A Function
Originally Posted by Paul McKenzie
You need to review the rules of the 'C' language when it comes to declaring variables. The rules are not the same as C++: All variables in that block must be declared before the call to printf().
Regards,
Paul McKenzie
Thanks! That was the problem.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|