Menu

Help with functions...

Jenn
2007-10-30
2012-09-26
  • Jenn

    Jenn - 2007-10-30

    I'm working on a program for class and was just wondering if anyone might be able to help me figure out why my perimeter values and two of the area values print incorrectly? All perimeters should be unique but instead end up being 12, and the two area calculations not belonging to the equilateral triangle come out as gibberish. I'm using the formulas my instructor told us to use, and we are also to use the functions as shown. My input file values are:
    14 18 24

    26 26 26

    24 67 90

    80 0 4 (0 being the sentinel value)

    I'm just learning how to use functions and there must be something wrong with my logic. My source code is included below...if anyone can provide any insight it will be very greatly appreciated! :)

    include <iostream>

    include <fstream>

    include <iomanip>

    include <cmath>

    using namespace std;

    double areaEquilateral(double, double, double);
    double perimeterTriangle(double, double, double);
    double areaTriangle(double, double, double);

    int main()
    {
    double side1,
    side2,
    side3,
    eq_area,
    area,
    p;

    ifstream infile;
    ofstream outfile;
    
    infile.open(&quot;C:\\Users\\Jennbo\\Desktop\\prog8_inp.txt&quot;);
    
    if (!infile)
       {
        cout &lt;&lt; &quot;Failed to open input file.\n&quot;;
    
        system(&quot;PAUSE&quot;);
        return 2;
       }
    
    outfile.open(&quot;C:\\Users\\Jennbo\\Desktop\\prog8_out.txt&quot;);
    
    if (!outfile)
       {
        cout &lt;&lt; &quot;Failed to open output file.&quot;;
    
        system(&quot;PAUSE&quot;);
        return 2;
       }
    
    cout &lt;&lt; &quot;Jennifer Harrow\n&quot;
         &lt;&lt; &quot;C.S.1428.3\n&quot;
         &lt;&lt; &quot;11/01/07\n\n&quot;;
    
    outfile &lt;&lt; &quot;Jennifer Harrow\n&quot;
            &lt;&lt; &quot;C.S.1428.3\n&quot;
            &lt;&lt; &quot;11/01/07\n\n&quot;;
    
    infile &gt;&gt; side1 &gt;&gt; side2 &gt;&gt; side3;
    
    
    
    if (side1 &lt;= 0 || side2 &lt;= 0 || side3 &lt;= 0)
       cout &lt;&lt; &quot;There were no valid input values on the input file.&quot;;
    
    eq_area = areaEquilateral(side1, side2, side3);
    p = perimeterTriangle(side1, side2, side3);
    area = areaTriangle(side1, side2, side3);
    
    while (side1 &gt;0 &amp;&amp; side2 &gt;0 &amp;&amp; side3 &gt;0)
       {
        {       
          if (side1 == side2 &amp;&amp; side2 == side3)
            cout &lt;&lt; &quot;The triangle is an equilateral triangle.\n&quot; 
                 &lt;&lt; &quot;The sides of the triangle are &quot; &lt;&lt; setprecision(1) &lt;&lt; fixed
                 &lt;&lt; side1 &lt;&lt; &quot;, &quot; &lt;&lt; side2 &lt;&lt; &quot;, and &quot; &lt;&lt; side3 &lt;&lt; &quot;.\n&quot;
                 &lt;&lt; &quot;The area of the triangle is &quot; &lt;&lt; eq_area &lt;&lt; &quot;.\n&quot;
                 &lt;&lt; &quot;The perimeter of the triangle is &quot; &lt;&lt; p &lt;&lt; &quot;.\n\n&quot;;     
          else
            cout &lt;&lt; &quot;The sides of the triangle are &quot; &lt;&lt; setprecision(1) &lt;&lt; fixed 
                 &lt;&lt; side1 &lt;&lt; &quot;, &quot; &lt;&lt; side2 &lt;&lt; &quot;, and &quot; &lt;&lt; side3 &lt;&lt; &quot;.\n&quot;
                 &lt;&lt; &quot;The area of the triangle is &quot; &lt;&lt; area &lt;&lt; &quot;.\n&quot;
                 &lt;&lt; &quot;The perimeter of the triangle is &quot; &lt;&lt; p &lt;&lt; &quot;.\n\n&quot;;                         
        }
        infile &gt;&gt; side1 &gt;&gt; side2 &gt;&gt; side3;                   
       }
    
    infile.close();
    outfile.close();
    
    system(&quot;PAUSE&gt;NUL&quot;);
    return 0;
    

    }

    double perimeterTriangle(double side1, double side2, double side3)
    {
    double p;

    p = (side1, side2, side3) / 2.0;
    
    return p;
    

    }

    double areaEquilateral(double side1, double side2, double side3)
    {
    double eq_area;

    eq_area = (side1 * side1 * sqrt (3) ) / 4;
    
    return eq_area;
    

    }
    double areaTriangle(double side1, double side2, double side3)
    {
    double area,
    p;

    p = perimeterTriangle (side1, side2, side3);   
    area = sqrt ( p * ( p - side1 ) * ( p - side2 ) * ( p - side3 ) );
    
    return area;
    

    }

     
    • Anonymous

      Anonymous - 2007-10-31

      >> Always take Cliffords advice, he is a damn smart cookie ;>p
      Actually my advice would be to use a debugger. Unfortunately Dev-C++'s sucks. Insight is passable.

       
    • Osito

      Osito - 2007-10-30

      In your functions, when you return a value like that you have to make it static or it won't be around after the function finishes. Try static double p; instead of double p; and so forth.

       
    • BiT

      BiT - 2007-10-30

      well First it is good habit to declare your variables on a seperate line for each one

      double side1;
      double side2;
      double side3;
      double eq_area;
      double area;
      double p;

      you declare your variables as doubles yet your values are integers
      14 18 24

      26 26 26

      24 67 90

      80 0 4 (0 being the sentinel value)

      See Table Below and you should be able to fix your problem with a little thought.
      (Hope your not using Vista/64bit)

      Type Size Values
      unsigned short int 2 bytes 0 to 65,535
      short int 2 bytes -32,768 to 32,767
      unsigned long int 4 bytes 0 to 4,294,967,295
      long int 4 bytes -2,147,483,648 to 2,147,483,647
      int (16 bit) 2 bytes -32,768 to 32,767
      int (32 bit) 4 bytes -2,147,483,648 to 2,147,483,647
      unsigned int (16 bit) 2 bytes 0 to 65,535
      unsigned int (32 bit) 2 bytes 0 to 4,294,967,295
      char 1 byte 256 character values
      float 4 bytes 1.2e-38 to 3.4e38
      double 8 bytes 2.2e-308 to 1.8e308

       
    • Jenn

      Jenn - 2007-10-30

      Thank you both - I changed the proper values to ints (our instructor wants all numeric data formatted to one decimal place, she's VERY strict re: her instructions so I guess there's something to that) and made my return values static. I also rearranged my declared variables to get in the habit. :) Ran the program again and my output reads:

      The sides of the triangle are 14, 18, and 24.
      The area of the triangle is -1.$.
      The perimeter of the triangle is 12.0.

      The triangle is an equilateral triangle.
      The sides of the triangle are 26, 26, and 26.
      The area of the triangle is 84.9.
      The perimeter of the triangle is 12.0.

      The sides of the triangle are 24, 67, and 90.
      The area of the triangle is -1.$.
      The perimeter of the triangle is 12.0.

      I apologize if there's something really simple that I'm overlooking...I think I've been staring at this too long! If my while loop reads the new set of ints each time it runs, why would the totals for the perimeter be the same on all three and the area the same on two of them? Is there something wrong with the way I've ordered things?

       
      • Osito

        Osito - 2007-10-31

        Your perimeter calculation doesn't make sense.

        p = (side1, side2, side3) / 2.0;

        I'm not sure how that's interpreted in C++, but the perimeter of a triangle should be p = side1 + side2 + side3;

        That probably explains the answer always being 12.

         
    • BiT

      BiT - 2007-10-31

      Take a look at your while loop and your variables and values.
      When do you call your functions, do your variables change with each call of said function, do the values you pass to your functions change.

       
    • BiT

      BiT - 2007-10-31

      Also

      A good way to see when you are calling functions since this is a simple console app is to place a line at the beginning of each function telling you when it has been called. Example below

      double perimeterTriangle(double side1, double side2, double side3)
      {

      // send text to screen saying you called the function
      cout << "perimeterTriangle Function" << endl;

      double p;

      p = (side1, side2, side3) / 2.0;

      return p;
      }

      ////////////////////////////////////////////////////
      Even better is to add to that the values you passed
      ////////////////////////////////////////////////////

      double perimeterTriangle(double side1, double side2, double side3)
      {

      // send text to screen saying you called the function with values passed to function
      cout << "perimeterTriangle Function with values ";
      cout << "side 1: " << side 1 << endl;
      cout << "side 2: " << side 2 << endl;
      cout << "side 3: " << side 3 << endl;

      double p;

      p = (side1, side2, side3) / 2.0;

      return p;
      }

       
      • Anonymous

        Anonymous - 2007-10-31

        Interestingly while giving advice about debugging, you reproduced the error that Osito had pointed out 12 hours before!

        I suggest a modification to your suggestion:

        // send text to screen saying you called the function
        cout << FUNCTION << endl;

        The FUNCTION string is always equal to the function name in which it occurs.

        Using sone funky pre-processor magic, you can do something even more elaborate:

        if !defined NDEBUG

        include <stdio.h>

        define DEBUG_printf( format, ... ) printf( FILE, LINE, FUNCTION, "%s[%d]:%s: "format, VA_ARGS )

        else

        define DEBUG_printf( format, ... )

        endif

        double perimeterTriangle(double side1, double side2, double side3)
        {

        double p;

        p = (side1 + side2 + side3) / 2.0;

        DEBUG_printf( "side1:%f side2:%f side3:%f p:%f", side1, side2, side3, p ) ;
        return p;
        }

        Which when called thus:

        perimeterTriangle( 12.0, 13.0, 15.0 ) ;

        will output:

        main.cpp[13]:perimeterTriangle: side1:12.000000 side2:13.000000 side3:15.000000 p:20.000000

        showing the file name, line number and the function name as well as any values added in the DEBUG_printf call. And because of the conditional compilation, the debug code disappears on release builds.

        Clifford

         
    • Anonymous

      Anonymous - 2007-10-31

      Forgive me for pointing out the obvious, but the perimeter of a triangle is the sum of the sides, not the sum of the sides divided by 2. You have calculated the "semi-perimeter", which is needed by the area calculation. You should remove the / 2.0 and have:

      s = perimeterTriangle (side1, side2, side3) / 2.0 ;
      area = sqrt ( s * ( s - side1 ) * ( s - side2 ) * ( s - side3 ) );

      in the area calculation.

      See: http://www.btinternet.com/~se16/hgb/triangle.htm

       
    • BiT

      BiT - 2007-10-31

      I only did a copy and paste from the original post, probably could have picked any of the functions...I just picked the one that was wrong ;)

      Was just trying to get them to think about the functions and how they called them and how they could visually see when it was called what was sent ect....

      Note***
      Always take Cliffords advice, he is a damn smart cookie ;>p

       
    • Jenn

      Jenn - 2007-10-31

      First off I can't thank you all enough. I don't know how I missed the problem with the basic perimeter calculation itself(commas instead of + signs) and the unnecessary division by 2.0 - glad you pointed that out since I've apparently forgotten everything I learned in high school math. But that's neither here nor there - by following your advice I FINALLY realized that I needed to be calling my functions from within the while loop instead of where I had them originally. I think having them outside the while loop meant that the calculations were only being performed on the first set of ints read from the input file and the 2nd and 3rd sets read went unnoticed by said functions. Argh and I spent all this time worrying about something I should have seen right away! With my future courses I hope I can become as knowledgable as you guys one of these days - I am truly grateful for your attention to my problem and your suggestions - this is a great forum! Thanks again!

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.