Thread: Code compiling but not working!

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    3

    Code compiling but not working!

    This program is to find the area of a triangle using three vertices. It compiles but I get these warnings in the main function:
    37:21: warning: variable 'aTriangle' set but not used [-Wunused-but-set-variable]45:7: warning: 'area' is used uninitialized in this function [-Wuninitialized]

    I get the first one at the struct triangle aTriangle declaration and the second at cout area. The output is 0. I don't understand why.

    Code:
    //-----Include required headers here-----
    #include<iostream>
    #include<cmath>
    using namespace std;
    //-----End of headers-----
    
    
    //-----Don't change/delete structs-----
    struct vertex{
       float x;
       float y;
    };
    
    
    struct triangle{
        vertex vertices[3];
    };
    //-----Structs end here
    
    
    
    
    //-----Add new functions here(if any)-----
    
    
    //-----New functions end here-----
    
    
    float cal_area(triangle aTriangle) {
        //Write your solution below this line
        float s,area,a,b,c;
        a=sqrt(pow((aTriangle.vertices[1].x-aTriangle.vertices[2].x),2)-pow((aTriangle.vertices[1].y-aTriangle.vertices[2].y),2));
        b=sqrt(pow((aTriangle.vertices[2].x-aTriangle.vertices[3].x),2)-pow((aTriangle.vertices[2].y-aTriangle.vertices[3].y),2));
        c=sqrt(pow((aTriangle.vertices[1].x-aTriangle.vertices[3].x),2)-pow((aTriangle.vertices[1].y-aTriangle.vertices[3].y),2));
        s=0.5*(a+b+c);
        area=sqrt(s*(s-a)*(s-b)*(s-c));
        return (area);
        //Dont write below this line
    }
    int main()
    {
        struct triangle aTriangle;
        aTriangle.vertices[1].x=1;
        aTriangle.vertices[2].x=2;
        aTriangle.vertices[3].x=5;
        aTriangle.vertices[1].y=4;
        aTriangle.vertices[2].y=5;
        aTriangle.vertices[3].y=20;
        float cal_area(triangle aTriangle);
    cout<<area<<endl;
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > float cal_area(triangle aTriangle);
    This just prototypes the function - it doesn't actually call it.

    You need something like
    area = cal_area(aTriangle);

    > aTriangle.vertices[1].x=1;
    Also, arrays start at subscript 0, not 1.
    So all your [3] subscripts are out of bounds.
    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.

  3. #3
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by Abhishek Suresh View Post
    Code compiling but not working!
    Are you sure about that ?

    Quote Originally Posted by Abhishek Suresh View Post
    Code:
        struct triangle aTriangle;
    You don't need to explicitly write that just
    Code:
    triangle aTriangle;
    Is enough. This is not C, in C++ struct declarations are similar to class declarations.

    Quote Originally Posted by Abhishek Suresh View Post
    Code:
     aTriangle.vertices[3].y=20;
    This is not correct, array indices start from 0 which means with the 3 elements in the array the last element has index 2 not 3.

  4. #4
    Registered User
    Join Date
    May 2016
    Posts
    3
    I have corrected the array indices so now they go from 0 to 2.

    I also added a variable float answer in the main function, and wrote
    answer=cal_area(triangle aTriangle);
    cout<<answer<<endl;
    but I get this error:
    main.cpp:50:30: error: expected primary-expression before 'aTriangle'
    answer=cal_area(triangle aTriangle);

    Google gives me other cases of this errror but not before a variable like this.

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    For function calls, just the name of the argument is enough here.
    Code:
    answer=cal_area(aTriangle);
    

  6. #6
    Registered User
    Join Date
    May 2016
    Posts
    3
    Thank you all! There was another error in the formula I used and I corrected it, it works now.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    BTW, you don't need to use pow() to calculate the square of a number. It's a lot of overhead for such a simple operation. Squaring a number x just means x * x. If it were my project, I would write an inline (template) function or macro to square numbers, and the code will be more readable, and as a side effect, probably have increased speed.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No to macro. That will only contribute to destabilizing your code.
    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.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Elysia View Post
    No to macro. That will only contribute to destabilizing your code.
    Maybe getting off on a tangent here, but how does a macro like this:

    Code:
    #define SQUARE_NUMBER(x) ((x) * (x))
    destabilize anything? It's pretty clear what it does, both in the name and the definition. It's very concise, and easy to understand. Macros, when used correctly, are a powerful and useful tool.

    The other way to go would be a template function, something like this:

    Code:
    #include <type_traits>
    
    template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
    T SquareNumber(const T& t)
    {
      return t * t;
    }
    If you ignore the second template parameter, it's still pretty clear, but I would argue not as clear as the macro.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elkvis
    If you ignore the second template parameter, it's still pretty clear, but I would argue not as clear as the macro.
    I don't see how this is less clear than the macro:
    Code:
    template<typename T>
    T SquareNumber(const T& t)
    {
        return t * t;
    }
    and you don't have to worry about accidents related to side effects, or that some included header already defined SQUARE_NUMBER (no big deal with a bit of #ifdef and #undef, but still). Granted, the error message will probably be more puzzling without the enable_if, but it should be easy to resolve given the good choice of name.
    Last edited by laserlight; 05-24-2016 at 07:55 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Maybe getting off on a tangent here, but how does a macro like this:
    #define SQUARE_NUMBER(x) ((x) * (x))
    destabilize anything?
    Take a not so simple example of trying to square an array of numbers
    Code:
    *dst++ = SQUARE_NUMBER(*src++);
    Anytime you pass parameters with side effects to a macro, you're potentially in trouble.
    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.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Salem View Post
    Take a not so simple example of trying to square an array of numbers
    Code:
    *dst++ = SQUARE_NUMBER(*src++);
    Anytime you pass parameters with side effects to a macro, you're potentially in trouble.
    That's an interesting scenario that I hadn't considered.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ code not compiling, help please/
    By KidFlash in forum C++ Programming
    Replies: 8
    Last Post: 12-23-2010, 08:30 AM
  2. Compiling C code
    By forumuser in forum C Programming
    Replies: 16
    Last Post: 10-13-2008, 04:17 PM
  3. Compiling x64 code in VC++ 6.0?
    By cpjust in forum Windows Programming
    Replies: 7
    Last Post: 07-29-2008, 09:36 AM
  4. Help on compiling code
    By mattyp1977 in forum C++ Programming
    Replies: 4
    Last Post: 03-26-2006, 05:07 PM
  5. compiling c code
    By MadCow257 in forum C++ Programming
    Replies: 2
    Last Post: 02-16-2006, 09:26 AM

Tags for this Thread