PDA

View Full Version : Comparing 4 numbers...C++

mindsurfer
10-21-2007, 10:05 PM
hi ...
i am trying to find the smallest and the largest of four numbers.. but if,for example, i input a value like 100000000 , it shows me a result "Largest number is 1e+009".. can someone please tell me why it doesn't show "1000000000".. sorry for asking this kind of question as i am new to C++..

# include <iostream>

using namespace std;

int main()

{
float number1,number2,number3,number4;

cout << "Enter the value of number 1 : ";
cin >>number1;

cout << "Enter the value of number 2 : ";
cin >>number2;

cout << "Enter the value of number 3 : ";
cin >>number3;

cout <<"Enter the value of number 4 : ";
cin >>number4;

if (number1 > number2 && number1 > number3 && number1 > number4)
cout << "Largest number is : " <<number1 <<endl;

else if (number2 > number3 && number2 > number4)
cout << "Largest number is : " <<number2 <<endl;

else if (number3 > number4)
cout << "Largest number is : " <<number3 <<endl;

else
cout << "Largest number is : " <<number4 <<endl;

if (number1 < number2 && number1 < number3 && number1 < number4)
cout << "Smallest number is : " <<number1 <<endl;

else if (number2 < number3 && number3 < number4)
cout << "Smallest number is : " <<number2 <<endl;

else if (number3 < number4)
cout << "Smallest number is : " <<number3 <<endl;

else
cout << "Smallest number is : " <<number4 <<endl;

return 0;

}

ralph l mayo
10-21-2007, 11:28 PM
The limits header isn't useful here, and if it was you'd want to use <limits> and not <limits.h>

You need to change some settings in the cout stream to show the numbers the way you want them:

float num1 = 1e10F; // Or grab from cin
std::cout.setf(std::ios::fixed); // Use fixed notation instead of scientific
std::cout.precision(0); // Show 0 places after the decimal (may not be appropriate, use whatever you want here)
std::cout << num1 << std::endl;

Reference (http://www.cppreference.com/cppio/index.html)