Question:
Can you analyze my coding (C) and tell me where I went wrong?
2010-12-07 18:41:47 UTC
basically this program is suppose to allow a user to enter in a students name, 3 test scores, determine avg, and then sort avg from low to high.

my editor gave me 2 errors, just dont know what they mean.

: error C2556: 'void main(void)' : overloaded function differs only by return type from 'int main(void)'
: error C2371: 'main' : redefinition; different basic types

thanks in advance!!

#include
struct Student
{
char name[20];
int t1, t2, t3;
float avg;

};

void main()
{
Student s;
printf("enter a name");
gets(s.name);
printf("enter 3 scores");
scanf(" %d %d %d", &s.t1, &s.t2, &s.t3);
s.avg=(s.t1 + s.t2 + s.t3) / (float) 3;
printf("%s\n", s.name);
printf(" %d %d %d %f\n", s.t1, s.t2, s.t3, s.avg);

}


void load (struct Student s[])
{
int i;
for(i=0; i<30; i++);
{
printf("enter name");
gets(s[i].name);
printf("Enter 3 scores");
scanf(" %d %d %d ", &s[i].t1, &s[i].t2, &s[i].t3);
s[i].avg=(s[i].t1 + s[i].t2 + s[i].t3) /(float)3;

fflush(stdin);

}
}

void print(struct Student s[])
{
int i;
for(i=0; i<30; i++);
{
printf("%s\n", s[i].name);
printf("%d %d %d\n", s[i].t1, s[i].t2, s[i].t3);
printf("f\n\n", s[i].avg);

}
}
void sort ( struct Student s[])

{
int i, j;
Student t;
for (i=0; i<29; i++);
for(j=0; j<29; j++);

if(s[j].avg > s[j+1].avg)
{
t=s[j];
s[j]=s[j+1];
s[j+1]= t;
}
}

void main ()
{
Student s[30];
load (s);
print(s);
sort (s);
print (s);

}
Four answers:
Wajahat Karim
2010-12-08 00:46:33 UTC
dear you have made main() function two times..... one thing should be remembered is never ever name any function like main. Because main() is a function, from where c++ compiler start working......



i would like to give u a tip on solving any compiler errors. hope u will like it. the errors you have placed in question are solutions to your problems....



: error C2556: 'void main(void)' : overloaded function differs only by return type from 'int main(void)'

: error C2371: 'main' : redefinition; different basic types



in these after error there is written a 5 character error code..... for example C2556.... copy that code and search it in msdn website.... you will get whole details of this error with solution.....



please see these links for your errors/.....

for error C2556

http://msdn.microsoft.com/en-us/library/9ydc7a3e.aspx



for error C2371

http://msdn.microsoft.com/en-us/library/t6xy3ys5%28VS.71%29.aspx



here is code which i have modified



#include

struct Student

{

char name[20];

int t1, t2, t3;

float avg;



};





//changed name of main to input() ******************************************

void input()

{

Student s;

printf("enter a name");

gets(s.name);

printf("enter 3 scores");

scanf(" %d %d %d", &s.t1, &s.t2, &s.t3);

s.avg=(s.t1 + s.t2 + s.t3) / (float) 3;

printf("%s\n", s.name);

printf(" %d %d %d %f\n", s.t1, s.t2, s.t3, s.avg);



}





void load (struct Student s[])

{

int i;

for(i=0; i<30; i++);

{

printf("enter name");

gets(s[i].name);

printf("Enter 3 scores");

scanf(" %d %d %d ", &s[i].t1, &s[i].t2, &s[i].t3);

s[i].avg=(s[i].t1 + s[i].t2 + s[i].t3) /(float)3;



fflush(stdin);



}

}



void print(struct Student s[])

{

int i;

for(i=0; i<30; i++);

{

printf("%s\n", s[i].name);

printf("%d %d %d\n", s[i].t1, s[i].t2, s[i].t3);

printf("f\n\n", s[i].avg);



}

}

void sort ( struct Student s[])



{

int i, j;

Student t;

for (i=0; i<29; i++);

for(j=0; j<29; j++);



if(s[j].avg > s[j+1].avg)

{

t=s[j];

s[j]=s[j+1];

s[j+1]= t;

}

}



//replaced void with int of main() ******************************************

int main ()

{

Student s[30];

load (s);

print(s);

sort (s);

print (s);

return 0; //added a return 0; statement *************************************

}



i hope you have got what u were looking for... :)
husoski
2010-12-08 02:54:29 UTC
You have two main() functions. Also, it sounds like you are compiling in C++ mode, not C. Did you name the source file CPP by mistake?



I think you want to comment out or delete the first main() function, since it is the one that ISN'T attempting to sort the data. Quick tip: To *safely* comment out a section of C code, surround it with #if 0 and #endif, like:



#if 0

...this does not compile

#endif



If you use /* and */ instead, a comment inside will mess things up, since /**/ comments don't nest.
?
2010-12-08 02:47:12 UTC
it tells you in your error it is int main not void main you are changing the return type of a built in function dont do that no reason to.. just return; at the end of the main... you also have two mains which I dont exactly understand merge them together or something dont use two mains
Entropy
2010-12-08 02:50:56 UTC
You have two main() functions (one at the top just under the struct, one at the bottom). You can 'overload' methods by reusing names, but they must have distinct parameterization so the compiler can tell them apart. Yours don't have any difference.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...