Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I'm writing a code that will take three numbers as input and determine whether
"SUM OF ANY TWO NUMBERS" is greater then third number..
The output is annoying me. . . if u put A=1, B=1, C=2

C++
#include <iostream>

using namespace std;

int main(int A, int B, int C)
{
    cout << "To Check The Sum Of Two Number Is Greater" << endl
         << "Enter A Integer" << endl; cin >> A;
    cout << "Enter B Integer" << endl; cin >> B;
    cout << "Enter C Integer" << endl; cin >> C;

if(A+B>C)
{
    if(B+C>A)
    cout << "Sum Of B & C Is Greater Then A" << endl;
}
else
    cout << "Sum Of A & B Is Greater Then C" << endl;
{
    if(C+A>B)
    cout << "Sum Of C Is Greater Then B" << endl;
}
return 0;
}
Posted
Comments
Sergey Alexandrovich Kryukov 20-Nov-12 14:55pm    
First of all, stop passing A, B, C as parameters... it looks like you don't know what a parameter is for, and how the entry point "main" should look... Learn it and come back.
And do you know what a function is? Create another function for this calculation...
--SA
Albert Holguin 20-Nov-12 15:05pm    
What's the problem? ....you have no problem statement or question.
Usman Hunjra 20-Nov-12 15:58pm    
Yes Sir actually I'm a student of BS(Computer Science) and new in C++ ..
But I think I'm gona learn a lot from here . . .

You really need to review the syntax of if blocks. My first recommendation is to always use curly braces (i.e. { } ) for your if blocks (and loops as well). While it's not required for ones that contain a single line of statements in them, it makes it a lot easier to see what's going on, and easier to edit later if you decide to add more statements.

And yes, this is relevant, because right off the bat I notice a problem here:
C++
else
    cout << "Sum Of A & B Is Greater Then C" << endl;
{
    if(C+A>B)
    cout << "Sum Of C Is Greater Then B" << endl;
}


This probably doesn't do what you intended. If the else part is reached, then the line immediately after it is executed. Then you are no longer in the else block. The following if statement will always be executed, because it is not attached to the else, you have just created it in a different scope.

I'm not entirely sure what you meant to do there, but start by looking at that point.
 
Share this answer
 
Comments
Usman Hunjra 20-Nov-12 16:00pm    
Thanks for your consideration Sir.
Your brackets are wrong on your if statements. For example, the very first evaluation is "if A+B is greater than C" then you will enter the first brackets below that... getting to the "if B+C is greater than A" statement. Fine... except that if B+C is greater than A, you are outputting the statement. But if it isn't, you are doing nothing... there is no else (false) condition defined so you not print anything. If I made A=3, B=0, and C=2, I would get nothing back as the output.

Further, on your outer if you have an else but it isn't bracketed properly. Only the cout statement is part of that else logic. The brackets that follow it mean nothing... that code will get executed just as if the brackets weren't there. If I made A=2, B=3, and C=2 I would get "Sum Of A & B Is Greater Then C" followed by "Sum Of C Is Greater Then B"... I'm guessing this isn't what you want.

If statements look like:

C#
if (true/false evaluation)
{
   // True Part
}
else
{
   // False Part
}


Good luck. And yes... you shouldn't pass in the variables as parameters to main.
 
Share this answer
 
Comments
Usman Hunjra 20-Nov-12 16:03pm    
Exactly Sir. . .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900