| Mshabeeb (1) | |
|
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 | |
|
|
|
| Reaper1 (132) | |||||
|
well first off, if you want the largest of 5 number, you need to have them inputted first using the cin command. example
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
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. | |||||
|
|
|||||
| Shinigami (288) | |
|
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
|
|