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
#include<iostream>
usingnamespace 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 ";
elseif (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.
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.
#include"stdafx.h"
#include<iostream>
usingnamespace 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 ";
}
elseif (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;
}