Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Homework
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
 
#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 20-Nov-12 9:51am
ctype.h1.7K
Comments
Sergey Alexandrovich Kryukov at 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 at 20-Nov-12 15:05pm
   
What's the problem? ....you have no problem statement or question.
Scenic Design at 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 . . .
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
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.
  Permalink  
Comments
Scenic Design at 20-Nov-12 16:00pm
   
Thanks for your consideration Sir.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
 
if (true/false evaluation)
{
   // True Part
}
else
{
   // False Part
}
 
Good luck. And yes... you shouldn't pass in the variables as parameters to main.
  Permalink  
Comments
Scenic Design at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 575
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,476
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,917


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 20 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100