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

This is a discussion on Two errors I don't know how to deal with. within the C++ Programming forums, part of the General Programming Boards category; Hello. I am getting the following two errors on the code below, and I don't know how to fix them. ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    66

    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 header();
    void totalAverage(int x, int y, int z);
    void droppedGrade();
    void droppedGradeAverage(int x, int y, int z);
    
    // Main Function
    int main()
    {
        ifstream infile;
        infile.open("input.txt");
        ofstream outfile;
        outfile.open("output.txt");
    
        header();
        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);
        droppedGrade();
    
        return 0;
    }
    
    // Prints a header on output file
    void header()
    {
        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;
    }
    
    void droppedGrade()
    {
        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];
        }
    
        droppedGradeAverage(sum7, sum5, sum6);
    
        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. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    66
    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. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,564
    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. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    66
    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.
    Last edited by Trey Brumley; 12-02-2012 at 02:13 PM.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,564
    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
    Last edited by jimblumberg; 12-02-2012 at 02:30 PM.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    66
    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. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    66
    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. #9
    Registered User
    Join Date
    May 2010
    Posts
    2,564
    C/C++ doesn't automatically round up. If you want to round up you will need to do that yourself, mathematically.

    Jim

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,185
    Quote Originally Posted by Trey Brumley View Post
    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).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do you deal with run-time errors?
    By waterborne in forum C++ Programming
    Replies: 5
    Last Post: 06-23-2010, 03:00 PM
  2. Deal or No Deal arrays
    By shel5210 in forum C Programming
    Replies: 2
    Last Post: 03-31-2010, 01:40 AM
  3. Deal or No Deal listbox prob
    By kryptkat in forum Windows Programming
    Replies: 5
    Last Post: 03-30-2009, 06:53 PM
  4. Replies: 5
    Last Post: 09-18-2008, 02:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21