CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 19 of 19
  1. #16
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: I need help with writing a program in C++ (I'm still a newbie)

    Quote Originally Posted by lebro98 View Post
    Thanks for all the replies! It really helped me. But last thing and I won't bother you anymore, may somebody please tell me what to do in the main() function, because I really have no idea.
    For the posted code
    Code:
    Triangle t1;
    face();
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  2. #17
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: I need help with writing a program in C++ (I'm still a newbie)

    As I'm feeling in a good mood and you're tried first, consider
    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    class Triangle
    {
    public:
    	Triangle(int aa, int bb, int cc) : a(aa), b(bb), c(cc) {}
    
    	double face()
    	{
    		double p = (a + b + c) / 2.0;
    		return sqrt(p*(p - a)*(p - b)*(p - c));
    	}
    
    	bool isTriangle()
    	{
    		return (a + b > c && a + c > b && b + c > a);
    	}
    
    private:
    	int a, b, c;
    };
    
    int main()
    {
    	int a, b, c;
    	cout << "Enter the 3 sides of the triangle as integers: ";
    	cin >> a >> b >> c;
    
    	Triangle t1(a, b, c);
    
    	if (t1.isTriangle()) {
    		cout << "Is a triangle" << endl;
    		cout << "The face is " << t1.face() << endl;
    	} else
    		cout << "Is not a triangle" << endl;
    
    	return 0;
    }
    Happy coding
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #18
    Join Date
    Apr 2017
    Posts
    7

    Re: I need help with writing a program in C++ (I'm still a newbie)

    Thank you so much!!! I really am exhausted. I have to do this and 2 websites due tomorrow and have been dealing with this for 2-3 hours (I wasn't even close to what you wrote)

  4. #19
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: I need help with writing a program in C++ (I'm still a newbie)

    Quote Originally Posted by lebro98 View Post
    Thank you so much!!! I really am exhausted. I have to do this and 2 websites due tomorrow and have been dealing with this for 2-3 hours (I wasn't even close to what you wrote)
    You're welcome. Make sure you learn from this - so next time you can just write the code straight away.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured