Help with code

Hey all, could someone help me understand what I am doing wrong here? When I try to build this code I get a response: "fatal error LNK1169: one or more multiply defined symbols found" and "_main already defined in idealLength.obj" how do I make this code work?

#include <iostream> // required for cout
#include <cmath> // required for sqrt

using namespace std;

class Tank // set the class Tank which will classify the values of the tank
{
public:
double heightPr;
double heightPy; // The variables of class Tank defined here
double length;
double width;
double volume;
double surfaceAreaPr;
double surfaceAreaPy;
double costPerSMPr;
double costPerSMPy;
double costPr;
double costPy;
double totalCost;
Tank() // Declare the values of some variables from within the Tank class
{
heightPr = 10;
costPerSMPr = 300;
costPerSMPy = 400;
}
};

int main()
{
Tank Test; //call Tank class under name Test
double idealLength, idealTotalCost(999999999999);
cout << "Enter tank volume >>> "; // get the user to input the value of initial volume
cin >> Test.volume;

for(int k=2; k<=40; k=1)
{
Test.length = k;
Test.width = Test.volume/(Test.length * Test.heightPr + .333333333333 * (Test.length/2) * Test.length); //equations that solve for the width and surface area of the two parts to the tank
Test.surfaceAreaPr = (Test.length * Test.width) + (Test.heightPr * Test.length * 2) + (Test.heightPr * Test.width * 2);
Test.surfaceAreaPy = sqrt((Test.length/2) * (Test.length/2) + (Test.length/2) * (Test.length/2)) * Test.width + sqrt((Test.width/2) * (Test.width/2) + (Test.length/2) * (Test.length/2)) * Test.length;


if(Test.surfaceAreaPr <= 300) // the algorithim that decides how much the tank will cost
{
Test.costPr = Test.costPerSMPr * Test.surfaceAreaPr;
}
else
{
if(Test.surfaceAreaPr > 300 && Test.surfaceAreaPr <= 500)
{
Test.costPr = Test.costPerSMPr * 300 + (Test.costPerSMPr * .9) * (Test.surfaceAreaPr - 300);
}
else
{
if(Test.surfaceAreaPr > 500)
{
Test.costPr = Test.costPerSMPr * 300 + (Test.costPerSMPr * .9) * 200 + (Test.surfaceAreaPr - 500) * (Test.costPerSMPr * .85);
}
}
}
if(Test.surfaceAreaPy <= 150)
{
Test.costPy = Test.costPerSMPy * Test.surfaceAreaPy;
}
else
{
if(Test.surfaceAreaPy > 150 && Test.surfaceAreaPy <= 300)
{
Test.costPy = Test.costPerSMPy * 150 + (Test.surfaceAreaPy - 150) * (Test.costPerSMPy * .8);
}
else
{
if(Test.surfaceAreaPy > 300)
{
Test.costPy = Test.costPerSMPy * 150 + 150 * (Test.costPerSMPy * .8) + (Test.costPerSMPy * .6) * (Test.surfaceAreaPy - 300);
}
}
}

Test.totalCost = Test.costPr + Test.costPy; // putting the two parts together to generate the final cost
if (idealTotalCost > Test.totalCost)
{
idealTotalCost = Test.totalCost;
idealLength = Test.length;
}
}

cout << "The optimal length is " << idealLength << " meters.";
cout << "The cost at this length is " << idealTotalCost << ".";
return 0;


}

Found a few mistakes. You can't initialize variables in a class, unless they are const, so I removed them. Your for(int k=2; k<=40; k=1) should be for(int k=2; k<=40; k++), and I added << endl at both cout's on the bottom of the program, to put each on a separate line. Here is your code that I was able to compile and run using MS Visual C++ 2008 Express Edition. Of course, I don't know if your math is correct or not, but I'm guessing it is.
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Tank Class.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream> // required for cout
#include <cmath> // required for sqrt

using namespace std;

class Tank // set the class Tank which will classify the values of the tank
{
public:
	double heightPr;
	double heightPy; // The variables of class Tank defined here
	double length;
	double width;
	double volume;
	double surfaceAreaPr;
	double surfaceAreaPy;
	double costPerSMPr;
	double costPerSMPy;
	double costPr;
	double costPy;
	double totalCost;
};

int main()
{
	Tank Test; //call Tank class under name Test
	Test.heightPr = 10; // initialized your variables here
	Test.costPerSMPr = 300; // and here
	Test.costPerSMPy = 400;// and also, here
	double idealLength, idealTotalCost(999999999999);
	cout << "Enter tank volume >>> "; // get the user to input the value of initial volume
	cin >> Test.volume;

	for(int k=2; k<=40; k++)
	{
		Test.length = k;
		Test.width = Test.volume/(Test.length * Test.heightPr + .333333333333 * (Test.length/2) * Test.length); //equations that solve for the width and surface area of the two parts to the tank
		Test.surfaceAreaPr = (Test.length * Test.width) + (Test.heightPr * Test.length * 2) + (Test.heightPr * Test.width * 2);
		Test.surfaceAreaPy = sqrt((Test.length/2) * (Test.length/2) + (Test.length/2) * (Test.length/2)) * Test.width + sqrt((Test.width/2) * (Test.width/2) + (Test.length/2) * (Test.length/2)) * Test.length;


		if(Test.surfaceAreaPr <= 300) // the algorithim that decides how much the tank will cost
		{
			Test.costPr = Test.costPerSMPr * Test.surfaceAreaPr;
		}
		else
		{
			if(Test.surfaceAreaPr > 300 && Test.surfaceAreaPr <= 500)
			{
				Test.costPr = Test.costPerSMPr * 300 + (Test.costPerSMPr * .9) * (Test.surfaceAreaPr - 300);
			}
			else
			{
				if(Test.surfaceAreaPr > 500)
				{
					Test.costPr = Test.costPerSMPr * 300 + (Test.costPerSMPr * .9) * 200 + (Test.surfaceAreaPr - 500) * (Test.costPerSMPr * .85);
				}
			}
		}
		if(Test.surfaceAreaPy <= 150)
		{
			Test.costPy = Test.costPerSMPy * Test.surfaceAreaPy;
		}
		else
		{
			if(Test.surfaceAreaPy > 150 && Test.surfaceAreaPy <= 300)
			{
				Test.costPy = Test.costPerSMPy * 150 + (Test.surfaceAreaPy - 150) * (Test.costPerSMPy * .8);
			}
			else
			{
				if(Test.surfaceAreaPy > 300)
				{
					Test.costPy = Test.costPerSMPy * 150 + 150 * (Test.costPerSMPy * .8) + (Test.costPerSMPy * .6) * (Test.surfaceAreaPy - 300);
				}
			}
		}

		Test.totalCost = Test.costPr + Test.costPy; // putting the two parts together to generate the final cost
		if (idealTotalCost > Test.totalCost)
		{
			idealTotalCost = Test.totalCost;
			idealLength = Test.length;
		}
	}

	cout << "The optimal length is " << idealLength << " meters." <<endl;
	cout << "The cost at this length is $" << idealTotalCost << "." << endl;
	return 0;
}
Topic archived. No new replies allowed.