if else statements

can u please help me with these.. my code runs..unfortunately theres still something wrong.. can u please help me solve this? been doing these for the past few hours.. please help me.. heres the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int Num1, Num2, Num3, Num4, Num5;
int greatest;
	cout<<"Please Enter Five Integers.\n\n";
	cin>>Num1>>Num2>>Num3>>Num4>>Num5;
	if(Num1==Num2 && Num2==Num3 && Num3==Num4 && Num4==Num5)
	cout<<"All The Numbers You have entered are equal!";
        else if (Num1 >Num2)
		greatest =Num1;
		else if (Num2>Num3)
		greatest=Num2;
		else if (Num3>Num4)
		greatest=Num3;
		else if (Num4>Num5)
		greatest=Num4;
		else if (Num5>Num1)
		greatest=Num5;
	cout<<greatest<<" is the greatest Number!";
Last edited on
The previous code should have worked.. This will not. if Num1 is greater than Num2, no other comparisons will be performed. Though you should really learn to use arrays and loops for this..
yea.. the previous one do work.. but i need a different code with the same output.. cause if i use that ill have the same code with my friend because we did it together.. so im trying a new one.. for the arrays and loops.. our teacher discouraged us from using it because were still not there.. so i need to use this conditions..
though it the code runs.. though it has wrong ouputs at times.. and when i enter same values it does say " all the numbers are equal" but after that a window appears.. saying debug error
Alternative 1. (pseudocode)
greatest = num1
if( greatest < num2 ) greatest = num2;
...
if( greatest < numN ) greatest = numN;
Notice that this does more comparisons than the one you worked out with your friend. This is a lot like how you'd do it with a loop though.

Alternative 2.
int max( int a, int b ) {
   if( a < b ) return a;
   else return b;
}

greatest = max( max( num1, num2), max( max( num3, num4 ), num5 ) )
This one works exactly how your original worked, just with functions. Though now that I think about it, if you haven't learned arrays, you probably haven't learned functions either..
tnx.. i tried the first alternative you made.. tnx for heLping.. :)

ill try ur 2nd alternative later.. ill try first ur 1st alternative with now the lowest value :)
by anyways.. how do u input multiple values in one line only?
instead of the inputting the values like this:
1
2
3
4
5
u can input it like this
1 2 3 4 5

our teacher said it was possible.. i tried every means i thought would work (with cin) but it didnt work.. it always need to be entered again..

operator >> doesn't care whether numbers are separated by newlines or spaces. It should work without any changes to your code.
how about this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// find the greates number in five
// cpp forum

#include<iostream>
using namespace std;

int findGreatest(int, int, int, int, int);

int main(){
	int a,b,c,d,e;
	cout << "enter five numbers: ";
	cin >> a >> b >> c >> d >> e;
	
	cout << "the greatest of all: ";
	cout << findGreatest(a,b,c,d,e) << endl;

return 0;
}


int findGreatest(int a, int b, int c, int d, int e){

	int greatest = 0;
	
	if (a>b) greatest=a;
	else greatest=b;

	if (c>d && c > greatest)
			greatest = c;
		//else greatest is already greatest
	else if (d > greatest) //d is bigger
			greatest = d;

	if (e > greatest) // processed a,b,c,d; now e.
		greatest = e;

	return greatest;
}

/*
enter five numbers: 22 33 44 55 11
the greatest of all: 55

enter five numbers: 1 2 3 4 5
the greatest of all: 5

enter five numbers: 345 66 33 5 999
the greatest of all: 999

enter five numbers: 999 2 3 4 555
the greatest of all: 999
*/


Last edited on
tnx for the heLps.. very apPreciated..:)
Topic archived. No new replies allowed.