does any one tell me why my 2 for loop does not work

Oct 18, 2016 at 7:39am
this program find the max and min number only 3 number


#include <iostream>
using namespace std;


void maximum(int a, int b, int c)
{
int i = 0;

for (int r = 0; r == 3 ; r++)
{
if (a < b)
{
i=a;
a=b;
b=i;
}// end a<b

if (b < c)
{
i=b;
b=c;
c=i;
} // end b < c

}// for end
cout << a << ">"<< b << ">" << c << endl;
}// end void bigger


void minimun(int a, int b, int c)
{
int i = 0;

for (int r = 0; r == 3 ; r++)
{
if (a > b)
{
i=a;
a=b;
b=i;
}// end a>b

if (b > c)
{
i=b;
b=c;
c=i;
} // end b > c

}// for end
cout << a << " < "<< b << " < " << c << endl;
}// end void smaller


int main () {

int u, m ,n ;

cout << "please, enter 3 real number separat by blank.\n" ;
cin >> u >> m >> n;
cin.ignore (1000, 10);

minimun(u, m, n);
maximum(u, m, n);

}

Last edited on Oct 18, 2016 at 7:40am
Oct 18, 2016 at 7:57am
please use tag
Oct 18, 2016 at 8:17am
Actually, I have no idea why you use a loop here. It might be appropriate for an array using, say, bubble sort on an arbitrary number of values; however, here you have only 3.

That aside, consider your line:
for (int r = 0; r == 3 ; r++)

You start the loop by setting r equal to 0.

As written, the loop will run ... if r equals 3 ... but it can't equal 3 because you set it equal to 0. So the loop won't run.

I suspect you intended to use < somewhere, rather than ==.


At present, in this forum the code tags don't seem to work for new input (although they do work if you go back and edit your post). It would, however, aid readability if you indented code blocks.
Topic archived. No new replies allowed.