I wrote a min/max program differently this time using a string to number conversion just to try something different. Anyway, the sum & min statements appear to work but the max part doesn't work. Where is my problem? How can I fix it? Thanks.
#include <iostream>
#include <sstream>
#include <string>
usingnamespace std;
int main()
{
string value;
int num;
int sum = 0;
int min;
int max;
for (int i=1; i<=5; i++)
{
cout<<"Enter value "<<i<<": ";
cin>>value;
if (!(istringstream(value) >> num)) num = 0;
if (num < min)
{
min = num;
}
if (num > max)
{
max = num;
}
sum += num;
}
cout<<"Sum= "<<sum<<endl;
cout<<"Min= "<<min<<endl;
cout<<"Max= "<<max<<endl;
return 0;
}
#include <climits>
//etc...
int min(INT_MAX); // min to the maximum signed integer value (0x7fffffff)
int max(INT_MIN); // max to the minimum signed integer value (0x80000000)
Thanks for the reply histrungalot! When I tried the INT_MIN & INT_MAX, both the min & max went bonkers. When I initialized with max=min=num;, both the min & max picked up the last entry.
The string to number conversion doesn't work too well right now compared to just using a straight number in the first place. My other min/max programs work properly unlike this one. I hoping that someone might have a solution to this problem. Thanks again!