Code works only half way

I am working on a console application that is to take inputs from the user and output the largest so far only the first two inputs seem to work, the last three are ignored, please help resolve this.


// This program accepts five numbers from the user and outputs the largest number entered.

#include <iostream>
#include <cmath>

using namespace std;

int score, number1, number2, number3, number4, number5;


if (number1 > number2 && number3 && number1 > number4 && number5)
{
cout << "\nThe highest score is: " << number1 <<endl;
}

else if (number2 > number1 && number3 && number2 > number4 && number5)
{
cout << "\nThe highest score is: " << number2 <<endl;
}

else if (number3 > number1 && number2 && number1 > number4 && number5)
{
cout << "\nThe highest score is: " << number3 <<endl;
}

else if (number4 > number1 && number2 && number1 > number3 && number5)
{
cout << "\nThe highest score is: " << number4 <<endl;
}

else if (number5 > number1 && number2 && number1 > number3 && number4s)
{
cout << "\nThe highest score is: " << number5 <<endl;
}

else

{
cout << "please enter a MAX Score";
}

int main()
{
cout << "\nEnter your first number: ";
cin >> number1;

cout << "\nEnter your second number: ";
cin >> number2;

cout << "\nEnter your third number: ";
cin >> number3;

cout << "\nEnter your fourth number: ";
cin >> number4;

cout << "\nEnter your fifth number: ";
cin >> number5;


cin >> score;
return 0;
}
I'm sure someone can tell you your code actually does, but I think what you want would look like this.

if ((number1 > number2) && (number1 > number3) && (number1 > number4) && (number1 > number5))

it is still outputting for only the first two....
I do not see how this does anything?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
cout << "\nEnter your first number: ";
cin >> number1;

cout << "\nEnter your second number: ";
cin >> number2;

cout << "\nEnter your third number: ";
cin >> number3;

cout << "\nEnter your fourth number: ";
cin >> number4;

cout << "\nEnter your fifth number: ";
cin >> number5;


cin >> score;
return 0;
}


If what you posted compiles, you need to get a new compiler, because it should not.
It's easier to store the maximum number in a separate variable and then determine its value by comparing to the numbers one by one. Something like:
int maxNum = number1;
if (number2 > maxNum) maxNum = number2;
if (number3 > maxNum) maxNum = number3;
if (number4 > maxNum) maxNum = number4;
if (number5 > maxNum) maxNum = number5;
cout << "The highest score is " << maxNum << '\n';

Better still, store the numbers in a vector. That way you can compute the maximum regardless of how many there are by putting the code in a loop.
Topic archived. No new replies allowed.