# Thread: Two errors I don't know how to deal with.

1. ## Two errors I don't know how to deal with.

Hello. I am getting the following two errors on the code below, and I don't know how to fix them. Can someone help me please? The function is designed to calculate the weighted average of homework, quiz, and test grades read in from an input file, as well as a later function that calculates the total average after a dropped homework grade.

The errors are:
Code:
```1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
1>C:\Users\Trey\documents\visual studio 2010\Projects\Program 5\Debug\Program 5.exe : fatal error LNK1120: 1 unresolved externals```
The program is as follows:
Code:
```// Trey Brumley
// CMPS 1043-101
// December 3, 2012
// Program 5 - Arrays
// ==================

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

// Prototypes
void totalAverage(int x, int y, int z);
void droppedGradeAverage(int x, int y, int z);

// Main Function
int main()
{
ifstream infile;
infile.open("input.txt");
ofstream outfile;
outfile.open("output.txt");

int homework[7], quiz[9], test[3], sum1=0, sum2=0, sum3=0;
for (int i = 0; i<7; i++)
{
infile >> homework[i];
sum1 += homework[i];
}
for (int i = 0; i<9; i++)
{
infile >> quiz[i];
sum2 += quiz[i];
}
for (int i = 0; i<3; i++)
{
infile >> test[i];
sum3 += test[i];
}

totalAverage(sum1, sum2, sum3);

return 0;
}

// Prints a header on output file
{
ofstream outfile;
outfile.open("output.txt");

outfile << "Trey Brumley" << endl;
outfile << "CMPS 1043-101" << endl;
outfile << "December 3, 2012" << endl;
outfile << "Program 5 - Arrays" << endl;
outfile << "==================" << endl << endl;

return;
}

// Finds the weighted and total average of each category
void totalAverage(int x, int y, int z)
{
ofstream outfile;
outfile.open("output.txt");

double a, b, c, d, e, f, g;

a = x/7;
b = y/9;
c = z/3;
d = a*0.15;
e = b*0.25;
f = c*0.60;
g = d+e+f;

outfile << "The average for Homework Grades is " << setprecision(1) << a << "." << endl;
outfile << "The weighted average of homework (15%) amounts to " << setprecision(1) << d << "." << endl;
outfile << "The average for Quiz Grades is " << setprecision(1) << b << "." << endl;
outfile << "The weighted average of quizzes (25%) amounts to " << setprecision(1) << e << "." << endl;
outfile << "The average for Test Grades is " << setprecision(1) << c << "." << endl;
outfile << "The weighted average of tests (60%) amounts to " << setprecision(1) << f << "." << endl;

outfile << "The total weighted average of all grades is " << setprecision(1) << g << "." << endl;

return;
}

{
ifstream infile;
infile.open("input.txt");
ofstream outfile;
outfile.open("output.txt");

int x = 100;
int sum4=0, sum5=0, sum6=0, sum7=0;
int homework2[7], quiz2[9], test2[3];

for (int i = 0; i<7; i++)
{
infile >> homework2[i];
if ( homework2[i]<x)
x = homework2[i];
sum4 += homework2[i];
}
sum7=sum4-x;
for (int i = 0; i<9; i++)
{
infile >> quiz2[i];
sum5 += quiz2[i];
}
for (int i = 0; i<3; i++)
{
infile >> test2[i];
sum6 += test2[i];
}

return;
}

void droppedGradeAverage(int x, int y, int z)
{
ofstream outfile;
outfile.open("output.txt");

double a, b, c, d, e, f, g;

a = x/6;
b = y/9;
c = z/3;
d = a*0.15;
e = b*0.25;
f = c*0.60;
g = d+e+f;

outfile << "The average for Homework Grades after dropping the lowest grade is " << setprecision(1) << a << "." << endl;
outfile << "The weighted average of homework (15%) amounts to " << setprecision(1) << d << "." << endl;
outfile << "The average for Quiz Grades is " << setprecision(1) << b << "." << endl;
outfile << "The weighted average of quizzes (25%) amounts to " << setprecision(1) << e << "." << endl;
outfile << "The average for Test Grades is " << setprecision(1) << c << "." << endl;
outfile << "The weighted average of tests (60%) amounts to " << setprecision(1) << f << "." << endl;

outfile << "The total weighted average of all grades minus the dropped homework grade is " << setprecision(1) << g << "." << endl;

return;
}```

2. > 1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
WinMain is the entry point for a GUI program.
You have just main, which is the entry point for a console program.

The easiest fix is to create another project, and make sure you choose "Win32 console" as the project type.
Then copy in the source code from the old project to the new project.

3. K, so the errors are gone, but now it's not printing my header, and the values the program is turning up are values along the lines of 7e+001, 2e+001, etc. How do I fix those?

4. There are a couple of problems. Firs when you open an output file stream using the default open mode you will truncate (erase) the file when opening it. And because you are opening this file in every function you will erase the contents in every file.

Another problem is that you are opening your file in main() and then re-opening the same file in your functions, you really should only open the same file once. You should learn to properly pass the open stream to your functions. You also should always check that your files open successfully.

Also what is contained in your input.txt file?

Jim

5. These numbers in order like this:

100
95
50
100
0
75
80
100
80
100
95
85
100
95
95
75
93
82
94

Got all functions to print. Now the only problem is the numbers showing up as 7e+001 et al.

6. Since you are using the setprecision() function without using the ios::fixed specifier when you set the precision to 1 you will get scientific notation with just one digit showing so a number like 95 would show as 9e+01 instead of 95. I recommend adding the following line before your current printouts:

Code:
`outfile << fixed << showpoint;`
After adding those lines I get the following output:
Trey Brumley
CMPS 1043-101
December 3, 2012
Program 5 - Arrays
==================

The average for Homework Grades is 71.0.
The weighted average of homework (15%) amounts to 10.7.
The average for Quiz Grades is 91.0.
The weighted average of quizzes (25%) amounts to 22.8.
The average for Test Grades is 89.0.
The weighted average of tests (60%) amounts to 53.4.
The total weighted average of all grades is 86.8.
The average for Homework Grades after dropping the lowest grade is 83.0.
The weighted average of homework (15%) amounts to 12.4.
The average for Quiz Grades is 91.0.
The weighted average of quizzes (25%) amounts to 22.8.
The average for Test Grades is 89.0.
The weighted average of tests (60%) amounts to 53.4.
The total weighted average of all grades minus the dropped homework grade is 88.6.

Jim

7. Thanks for the help in getting it to print. It does print now but the problem is, I'm getting the wrong results. The data I should be replicating shows that the first seven numbers should average out to 71.4, and it averages directly to 71. I don't see how it's doing that. The other two seem to be reading just fine, except they too are rounding down.

The answers I'm getting for the straight-up averages are 71, 91, and 89. I SHOULD be getting exact answers, which is why the setprecision(1), which should lead to 71.4, 91.7, and 89.7. What is causing this error in mathematics?

8. K, got that one figured out, by adding a .0 after the divisors, but now there's one more problem and I think I'll be done. The final answer I should be receiving is 87.5. I'm receiving 87.4. The outside mathematics using my above values that I should be getting (which I am now), I should end up with 87.455 as an average, which rounds up to 87.5.

9. C/C++ doesn't automatically round up. If you want to round up you will need to do that yourself, mathematically.

Jim

10. Originally Posted by Trey Brumley
K, got that one figured out, by adding a .0 after the divisors, but now there's one more problem and I think I'll be done. The final answer I should be receiving is 87.5. I'm receiving 87.4. The outside mathematics using my above values that I should be getting (which I am now), I should end up with 87.455 as an average, which rounds up to 87.5.
An easy way is to add 0.5 (add extra 0s to that to shift the digit to the right position as required) to the number you wish to round.
For example,

double x = y + 0.5;

This will round it properly. How?

Take, say, y = 1.
Then, y + 0.5 = 1.5 = 1 (truncated).

Take, say, y = 1.5.
Then, y + 0.5 = 2 = 2 (truncated).