Getting errors. How do i correct it?

Using the square below the main() function, write the code for a user-written function called larger which will compare two integers sent to it to determine which is larger and print (to the screen or console) a message indicating which is larger. I get errors when I debig it. How do I fix it.

1
2
3
4
5
6
7
8
9
10
11
12
  #include <stdio.h>
void larger(int x, int y);

int main()
{
   int grade1, grade2;
   printf("n\nEnter two numeric grades between 0 and 100:  ");
   scanf(   "%d%d   ", &grade1, &grade2);
   larger(grade1, grade2);
   return(0);
}



The errors:

1>------ Build started: Project: Chapter5, Configuration: Debug Win32 ------
1>Compiling...
1>Chapter5.cpp
1>c:\users\jennifer\documents\visual studio 2008\projects\chapter5\chapter5\chapter5.cpp(8) : warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\stdio.h(306) : see declaration of 'scanf'
1>Linking...
1>Chapter5.obj : error LNK2019: unresolved external symbol "void __cdecl larger(int,int)" (?larger@@YAXHH@Z) referenced in function _main
1>C:\Users\Jennifer\Documents\Visual Studio 2008\Projects\Chapter5\Debug\Chapter5.exe : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://c:\Users\Jennifer\Documents\Visual Studio 2008\Projects\Chapter5\Chapter5\Debug\BuildLog.htm"
1>Chapter5 - 2 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
This is the important message:
1>Chapter5.obj : error LNK2019: unresolved external symbol "void __cdecl larger(int,int)" (?larger@@YAXHH@Z) referenced in function _main

It is telling you that the linker can't find the code for function larger()
So how would i fix that. I understand it can't find it.
no where in my book does it describe a user written function larger
Well, at line 2 there is a prototype declaration. That lets the compiler know what type of parameters the function takes, and what type of value it returns. But that's all.

Where is the code defining the body of the function, specifying what action it has to carry out with the input parameters, in order to produce a value and then return it?
Well in the declaration it is set for int x and int y but the code defines int grade1 and int grade2. Would that be where my problem relies?
I changed my code to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>

int larger(int x, int y);

int main()
{
   int grade1;
   int grade2;

   printf("Enter two numeric grades between 0 and 100:  ");
   scanf( "%d%d", &grade1, &grade2);

   printf( "Larger is: %d\n", larger(grade1, grade2);



   system("pause");
   return 0;
}


comes up no errors but it doesnt add my integers
No, sorry. That code doesn't compile. There a missing closing ')' on line 13.

You need to define the function body:
1
2
3
4
5
int larger(int x, int y)
{
    // do something here
    // return something
}


Because you already defined the prototype at the top, this new code can be placed at the bottom, after the end of main().
I fixed it!!! Just need to use my brain more!!! Thanks!!

My final code and works!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int larger(int x, int y);

int main()
{
   int grade1;
   int grade2;

   printf( "Enter two numeric grades between 0 and 100:  ");
   scanf( "%d%d", &grade1, &grade2);

   printf( "Larger is: %d\n", larger(grade1, grade2));



   system("pause");
   return 0;
}
	int larger(int x, int y)
	{
		int larger = x;

		if ( y > larger )
		{
			larger = y;
		}
		return larger;
	}
Ok that's good! Not just that it works but that you also get how it all fits together.

(your function could be simplified slightly, but that is not really important right now).
Topic archived. No new replies allowed.