A beginner's problem !

This is a discussion on A beginner's problem ! within the C++ Programming forums, part of the General Programming Boards category; Code: Hi everyone. Well, I have just started C++ programming and I solved one problem partly, actually the user enters ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    8

    A beginner's problem !

    Code:
    Hi everyone. Well, I have  just started C++ programming and I solved one problem partly, actually the user enters three numbers and I must check if there is a right triangle with sides whose lengths are those three numbers. The program works for integer .For example, if I enter :3,4,5 it works but when I enter 0.3 , 0.4 , 0.5 it says there there us no right traingle with these lenghts but it should be ! Here is the program :
    
    #include<iostream>
    using namespace std;
    int main(){
        double a,b,c;
        cout<<"Please enter three numbers";
        cin>>a>>b>>c;
        if(a*a+b*b==c*c || a*a+c*c==b*b || c*c+b*b==a*a)
        cout<<"There is a right triangle with these lenghts";
        else
        cout<<"There is not ... ";
    return 0;
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,024
    In the future, please put your actual message text outside the code tags. The code tags are for code only.

    Now, as for your problem. You basically have to remember that precision in a computer isn't unlimited, contrary to algebra. The chance that a*a + c*c equals exactly b*b is small. It will more likely be roughly equal to b*b. So you have to basically do

    if (abs(a*a + c*c - b*b) <= 0.0001)

    Where 0.0001 is some small range that works well.
    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.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    8
    Thank you very much ! I will ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  4. Beginner's Problem With First Funtion
    By Ohrange in forum C++ Programming
    Replies: 4
    Last Post: 04-19-2007, 07:59 PM
  5. A beginner's problem
    By NewToC in forum C Programming
    Replies: 5
    Last Post: 11-21-2002, 05:20 PM

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