### Finding the largest number using if

I am new to C++.

I was trying to do a program that find the largest among 5 doubles but I always get the same output

here is my code:

/*
programmer: Mshabeeb

C program to count the largest number
*/

#include <iostream>
int main() {
double x,y,z,a,b,largest;

cout<<"Please input five double values:"<<x<<" "<<y<<" "<<z<<" "<<a<<" "<<b<<endl;

largest=x;

if (y>largest) largest=y;
else if (z>largest) largest=z;
else if (a>largest) largest=a;
else if (b>largest) largest=b;
cout<<"The largest value found in the input is"<<largest<<endl;

return 0;
}

I always get the same output whatever the values inputted are:

Please input five double values:4.89922e-270 -1.57834 -2.54553e-42 1.52069e-314 -6.90553e-42
The largest value found in the input is4.89922e-270

I should do it this way to get the grade and I don't know where is the mistake.
I will be thankful if you can help me before midnight

well first off, if you want the largest of 5 number, you need to have them inputted first using the cin command.

example

 ``1234567891011121314`` ``````#include using namespace std; void main() { int a, b, c, d, e; cout << "Enter 5 numbers: " cin >> a; cin >> b; cin >> c; cin >> d; cin >> e; }``````

As for finding the largest you need to see how many if-then/if-else statements you need, if you need, now you can either write them seperate like you have or a different way.

example

 ``1234567891011`` ``````if (a > b && a > c && a > d && a > e) { largest = a; cout << "The largest number is << a << endl; } else if (b > a && b > c && b > d && b > e) { largest = b; cout << "The largest number is << b << endl; } and so on``````

your making more work for you because your doing it sepeately, meaning youll have more if-then statements.

cout is whats use to print something to the screen and cin allows the program to get the user input from the keyboard.
First: use arrays or vectors from STD to store numbers.
Second: don't use so many if - else. Use loop to search. And you will need only a few if - else.

Edit:
you will need one array and one if(), no else;
Last edited on
Topic archived. No new replies allowed.