Thread: I am new at C++ and my program is erroring part way through it

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    11

    Question I am new at C++ and my program is erroring part way through it

    my program compiles and executes fine but when i try to run it, it goes through a little bit then errors. here is my code
    thanx


    Code:
    #include <iostream>
    #include <stdlib.h>
    using namespace std;
    int main()
    {
    int xa;
    int ya;
    int xb;
    int yb;
    int xc;
    int yc;
    int sa;
    int cx;
    int cy;
    int oa;
    int boa;
    int sb;
    int ob;
    int bob;
    int oc;
    int od;
    int ox;
    int oy;
    int mxa;
    int mya;
    int mxb;
    int myb;
    int ca;
    int cb;
    int cc;
    int cd;
    int ce;
    int cf;
    int cix;
    int ciy;
    int sc;
    int sd;
    int ea;
    int eb;
    int ec;
    int ed;
    int ee;
    int ef;
    int ex;
    int ey;
       cout << "enter the x position of the first coordinate: ";
       cin >> xa;
       cout << "enter the y position of the first coordinate: ";
       cin >> ya;
       cout << "enter the x position of the second coordinate: ";
       cin >> xb;
       cout << "enter the y position of the second coordinate: ";
       cin >> yb;
       cout << "enter the x position of the third coordinate: ";
       cin >> xc;
       cout << "enter the y position of the third coordinate: ";
       cin >> yc;
       cx = (xa + xb + xc) / 3;
       cy = (ya + yb + yc) / 3;
       cout << "The centroid is (" << cx << "," << cy << ")" << endl;
       sa = (yb - ya) / (xb - xa);
       oa = xc * -sa;
       boa = yc - oa;
       sb = (yc - ya) / (xc - xa);
       ob = xb * -sb;
       bob = yb - ob;
       oc = oa - ob;
       od = bob - boa;
       ox = oc / od;
       oy = (oa * ox) + boa;
       cout << "The orthocenter is (" << ox << "," << oy << ")" << endl;
       mxa = (xa + xb) / 2;
       mya = (ya + yb) / 2;
       mxb = (xa + xc) /2;
       myb = (ya + yc) / 2;
       cb = mxa * -sa;
       ca = mya - cb;
       cc = mxb * -sb;
       cd = myb - cc;
       ce = cb - cc;
       cf = cd - ca;
       cix = cf / ce;
       ciy = (cb * cix) + ca;
       cout << "The circumcenter is (" << cix << "," << ciy << ")" << endl;
       sc = (cx - cix) / (cy - ciy);
       sd = (cx - ox) / (cy - oy);
       ea = cx * sc;
       eb = cx * sd;
       ec = cy - ea;
       ed = cy - eb;
       ee = ea - eb;
       ef = ed - ec;
       ex = ef / ee;
       ey = (ea * ex) + ec;
       cout << "\n\nThe Euler Line for this Triangle is (" << ex << "," << ey << ")" << endl;
          system("PAUSE");
          return 0;
    }

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Use a debugger to find out where exactly the error is, and what.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    If your new to c++ you probably don't know how to use the debugger yet, but it is very valuable. You can execute one line of your code at a time, and look at all the values for your variables to see what's happening, so it's good to learn how to use the debugger as quickly as possible.

    Looking at your code, it's possible you could be dividing by 0 somewhere in your calculations depending on what numbers you input.

    Also, having to type all those variable names is a reason why you would want to use arrays instead.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >int xa;
    >int ya;
    >int xb;
    >int yb;
    >int xc;
    >int yc;
    >int sa;
    >int cx;
    >int cy;
    >int oa;
    >int boa;
    >int sb;
    >int ob;
    >int bob;
    >int oc;
    >int od;
    >int ox;
    >int oy;
    >int mxa;
    >int mya;
    >int mxb;
    >int myb;
    >int ca;
    >int cb;
    >int cc;
    >int cd;
    >int ce;
    >int cf;
    >int cix;
    >int ciy;
    >int sc;
    >int sd;
    >int ea;
    >int eb;
    >int ec;
    >int ed;
    >int ee;
    >int ef;
    >int ex;
    >int ey;

    Instead of ints, change your variables to doubles or floats to perform the calculations.

Popular pages Recent additions subscribe to a feed