Beginner C++ student. ERROR LNK2019 1120 HELP

Here's my code and when building it, I keep receiving...

Error 1 error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup D:\Visual Studio 2010 Professional (x86) - DVD (English)\NewAssignments\NewAssignments\MSVCRTD.lib(crtexe.obj) NewAssignments

Error 2 error LNK1120: 1 unresolved externals D:\Visual Studio 2010 Professional (x86) - DVD (English)\NewAssignments\Debug\NewAssignments.exe NewAssignments

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<iostream>
using namespace std;

int main()
{
	double popA, popB, rateA, rateB, newpopA, newpopB;
	int numOfYears = 0;

	cout << "Enter population for town A: ";
	cin >> popA;
	cout << "Enter population for town B (Must be larger than town A): ";
	cin >> popB;

	if (popA < popB)
	{
		cout << "Enter the growing rate of town A: ";
		cin >> rateA;
		cout << "Now enter the growing rate of Town B: ";
		cin >> rateB;

		if (rateA <= rateB)
			cout << "Invalid ";


		else if (rateA > rateB)
		{
			while (newpopA < newpopB)

			newpopA = (popA * (rateA / 100.0)) + popA;
			cout << "Population A: " << newpopA << endl;

			newpopB = (popB * (rateB / 100.0)) + popB;
			cout << "Population B: " << newpopB << endl;
			numOfYears++;

		}
	}
	if (popA > popB){
		cout << "In " << numOfYears<< " year(s) the population of town A will be greater than the population of town B" << endl;
		cout << "In " << numOfYears<< " population of town A is: " << popA << endl;
		cout << "In " << numOfYears<< " population of town B is: " << popB << endl;

}
	return 0;

}
I can see uninitialized variables newpopA, newpopB at line 27. The compiler doesn't know if one is greater than the other because no values have been assigned to them.
Last edited on
you haven't initialized newpopA and newpopB.
i would suggest put curly braces on the if and while statement for clarity and consistency.
the code now compiles but the logic is not right.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include"stdafx.h"
#include<iostream>
using namespace std;

int main()
{
	double popA, popB, rateA, rateB, newpopA, newpopB;
	int numOfYears = 0;

	cout << "Enter population for town A: ";
	cin >> popA;
	cout << "Enter population for town B (Must be larger than town A): ";
	cin >> popB;

	if (popA < popB)
	{
		cout << "Enter the growing rate of town A: ";
		cin >> rateA;
		cout << "Now enter the growing rate of Town B: ";
		cin >> rateB;

		if (rateA <= rateB)
		{
			cout << "Invalid ";			
		}
		else if (rateA > rateB)
		{
                       // these were never initialized.
			newpopA = 100;
			newpopB = 200;

			while (newpopA < newpopB)
			{
				newpopA = (popA * (rateA / 100.0)) + popA;
				cout << "Population A: " << newpopA << endl;

				newpopB = (popB * (rateB / 100.0)) + popB;
				cout << "Population B: " << newpopB << endl;
				numOfYears++;
			}

		}
	}
	if (popA > popB){
		cout << "In " << numOfYears << " year(s) the population of town A will be greater than the population of town B" << endl;
		cout << "In " << numOfYears << " population of town A is: " << popA << endl;
		cout << "In " << numOfYears << " population of town B is: " << popB << endl;

	}

	int x;
		cin >> x;
	return 0;

}
Last edited on
Topic archived. No new replies allowed.