Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple IF Statement

Status
Not open for further replies.

JockVSJock

Technical User
Jun 28, 2001
59
US
Trying to get back into C++ and doing about an hr of programming on it for each day. I've writing a simple program where an end user inputs three #'s, and the output is a sum, product, and then it compares the three to see what is the largest and the smallest.

When I go to compile, I get an error on the first IF statement. I think I have it written right, but I get an error like it is looking for an ; to end the statement with. My code is below

thanks

-Chris

#include<iostream.h>

int main()
{
int iNum1, iNum2, iNum3;
cout<<&quot;Enter three numbers \n&quot;;
cin>>iNum1>>iNum2>>iNum3;
cout<<&quot;Input three different integers: &quot; << iNum1 << iNum2 << iNum3;
cout<<endl;

int iSum;
iSum=iNum1+iNum2+iNum3;
cout<<&quot;Sum is &quot;<<iSum;
cout<<endl;

int iProduct;
iProduct=iNum1*iNum2*iNum3;
cout<<&quot;Product is &quot;<<iProduct;
cout<<endl;

if ( iNum1 < iNum2 ) && ( iNum1 < iNum3 );
{
cout<<&quot;Smallest is &quot;<<iNum1<<endl;
}

if ( iNum1 > iNum2 ) && ( iNum1 > iNum3 );
{
cout<<&quot;Largest is &quot;<<iNum1<<endl;
}

return 0;
}
 
Your missing parens

if (( iNum1 < iNum2 ) && ( iNum1 < iNum3 ));
{
cout<<&quot;Smallest is &quot;<<iNum1<<endl;
}
 
OH... and the semicolon at the end is not needed... by using the semicolon the if will just be the if with nothing to do and both couts will happen every time.

Matt
 
You must enclose your if conditions in a bracket and don't use semicolon, otherwise the semicolon itself will be considered as the instruction to execute if the statement is true.

Then, your instruction will became

if (( iNum1 < iNum2 ) && ( iNum1 < iNum3 ))
{
cout<<&quot;Smallest is &quot;<<iNum1<<endl;
}


Now I think It will work.
 
OOoops, meanwhile I was responding Zyrenthian dit it before me, sorry. Consider it as a resume :)
 
If I don't use the semicolon at the end of the cout statement, I get and syntax error : missing ';' before '}', with it I get a warning ';' empty controlled statement found, is this the intent?

if (( iNum1 < iNum2 ) && ( iNum1 < iNum3 ));
{
cout<<&quot;Smallest is &quot;<<iNum1<<endl;
}

if (( iNum1 > iNum2 ) && ( iNum1 > iNum3 ));
{
cout<<&quot;Largest is &quot;<<iNum1<<endl;
}

Also, what would be the best way to write an if statment to compare three numbers?

thanks

-Chris

 
The semicolon must be put after cout line, but not at the end of the &quot;if&quot; statement.

About the best way to find minimum e maximum values of three number, your method is straightforward, then perfect. A more general method (for more then just three elements) may be this:


int elementsNumber = 3;
int elements[3];

elements[0] = 24;
elements[1] = 3;
elements[2] = 256;

int min = elements[0];
int max = elements[0];

for (int i=0; i<elementsNumber; i++) {
if (elements < min) {
min = elements;
}
if (elements > max) {
max = elements;
}
}

cout << &quot;min = &quot; << min << &quot;\nmax = &quot; << max << &quot;\n&quot;;

It's just a little bit more elegant.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top