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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Number Comparison?

Status
Not open for further replies.
Mar 29, 2002
33
US
I’m working on a comparison program to take a look at three numbers that are pre-identified in the program. The user will have the ability to select the largest or smallest number by using the (S for Small, B for Biggest). The output to the user will be the smallest or biggest depending on their request. Have the following code below, but can’t get the thing to run.
Thanks-


#include <iostream.h>

void int biggest (char & task_to_do)

{

cout<<&quot;Enter the number you would like to see: B
for Biggest, S for Smallest&quot;;
cin>>task_to_do;
while (Task_to_do != 'B' && Task_to_do != 'S')
{
cout<<&quot;Incorrect Input, must be B or S, re-enter: &quot;;
cin>>task_to_do;
}
}

double biggest
{
double n1, n2, n3;
n1 = 14;
n2 = 26;
n3 = 5;
x = n1;
while (x <= n3)
{
if n1>n2>n3
{
return n1;
}
else if n2>n1>n3
{
return n2;
}
else n3>n1>n2
{
return n3;
}
cout<<endl;
}
count<<endl;
}


double smallest
{
double n1, n2, n3;
n1 = 14;
n2 = 26;
n3 = 5;
x = n1;
while (x <= n3)
{
if n1<n2<n3
{
return n1;
}
else if n2<n1<n3
{
return n2;
}
else n3<n1<n2
{
return n3;
}
cout<<endl;
}
count<<endl;
}




void main()

{
int biggest
char task_to_do;
more = 'Y';

while (more == 'Y')
{
int biggest ( task_to_do);
if (task_to_do == 'B')
{
cout<<&quot;The biggest number in the comparison is:
&quot;<<number<<endl<<endl;
}
else
{
cout<<&quot;The smallest number in the comparison is:
&quot;<<number<<endl<<endl;
}
cout<<&quot;Do another comparison? (Y/N): &quot;;
cin>>more;
}

cout<<endl<<endl<<&quot;******End
Program******&quot;<<endl<<endl;
}
 
The problem is in the
Code:
if n2<n1<n3
sequences. To the compiler, it could do

n2 < n1 first which may produce either 1 or 0 depending on whether it is true or false. Then it generates 0 < n3 or 1 < n3, which isn't what you really want. This has to be changed to
Code:
if (n2 < n1 && n1 < n3)
You will need the brackets too otherwise it wont compile.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top