Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

easy quick help plz :)

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
trying to do hw for school, using borlandc++

get the following code gives an error saying
"function should return a value in function main()"
can someone please tell me what i am doing wrong

main()
{
float maze[2][4];
int row, col;
maze[0][0] = 2.30;
maze[0][1] = 2.30;
maze[0][2] = 2.30;
maze[0][3] = 2.30;
maze[1][0] = 2.30;
maze[1][1] = 2.30;
maze[1][2] = 2.30;
maze[1][3] = 2.30;

for (row=0; row>2; row++)
{ for (col=0; col>4; col++)
{ printf("$%.2f \n", maze[row][col]);}
}
return;

}
 
In C89 (the current C standard supported by ANSI/ISO compilers), a definition of:

main() {
/* ... */

is treated as though you had written:

int main() {
/* ... */

So, you must return a value from main():

at the end, change your return statement to:

return 0;

This indicates successful program execution. Be aware though that the new C standard doesn't allow implicit int return from main() so you should really use either:

int main(void) or int main(int argc,char **argv)

Russ
bobbitts@hotmail.com
 
1. what does the following one mean? why you put > instead of <?
for (row=0; row>2; row++)
{ for (col=0; col>4; col++)
{ printf(&quot;$%.2f \n&quot;, maze[row][col]);}
}
2. Your main() by default return an int. It means you must end with return 0; instead of return;.

John Fill
 
Firstly i apologize for my bad english.

A)
in your code main function is forced to return a value.(with using return command) but you
didn't use any value to return. in this case Which value does main function should return with?.
so this condition causes return error->&quot;function should return a value in function main()&quot;
To avoid this problem, declare your main code as &quot;void main()&quot; (at the same time you must
remove return command at the end of main function) or remove return command at the end of your main function. or modify to return 0;

B)
Also, why did you use > instead of <. This causes other problem. When you put > operator
you never come into the loop.

for works like this:

for (statement 1; statement 2; statement 3) {
/*loop body */;
}

step 1) statement 1 is applied. /* Only 1 time operated */
step 2) statement 2 is controlled, if statement 2 is true (!= 0) goto step 3 else exit from loop.
step 3) loop body is executed.
step 4) statement 3 is applied. goto step 2.

in your code :
for (row = 0; row > 2; ++row) {
.....
}

step 1) row = 0; ok.
step 2) row > 2; false( row = 0 now), exit from loop.

You can use/study this code:

main()
{
float maze[2][4];
int row, col;

maze[0][0] = 2.30;
maze[0][1] = 2.30;
maze[0][2] = 2.30;
maze[0][3] = 2.30;
maze[1][0] = 2.30;
maze[1][1] = 2.30;
maze[1][2] = 2.30;
maze[1][3] = 2.30;

for (row = 0; row < 2; row++)
for (col = 0; col < 4; col++)
printf(&quot;$%.2f \n&quot;, maze[row][col]);
return 0;
}

*****Mehmet S. DÝNDAR********
 
The problem with using void as the return value of main() is that it makes your code non-portable. Furthermore, any compiler is free to reject code that uses void as the return value of main(). This is because, as explained above, the C standard provides no definition for main() that uses void as the return value, so no compiler implementor is obligated to support such a construct.

Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top