Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
int number;
int min_value = 100000;
for (int i = 0; i <= 4; i++)
{
   Console.WriteLine("Enter a number: ");
   number = Convert.ToInt16(Console.ReadLine());
   if (number < min_value)
      min_value = number;
 
   Console.WriteLine(number);
}
Posted 3-Dec-12 8:07am
Edited 3-Dec-12 11:54am
v2
Comments
Matt T Heffron at 3-Dec-12 13:13pm
   
And your question is????
Things to think about...
What happens when you enter "100001" (so it is > min_value)? What do you think SHOULD happen?
What happens when you enter "abc"? Again, what do you think SHOULD happen?
Sergey Alexandrovich Kryukov at 3-Dec-12 13:15pm
   
Exactly. Absolutely wrong.
 
But it explains why OP asks this question -- this is exactly the problem.
--SA
Sergey Alexandrovich Kryukov at 3-Dec-12 13:22pm
   
So, I answered, please see.
--SA
marchest_21 at 3-Dec-12 13:28pm
   
thanks
Sergey Alexandrovich Kryukov at 3-Dec-12 13:53pm
   
You are welcome. It will work for you, for sure. Please consider accepting the answer formally (green button) -- thanks.
--SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Please see comments to the questions — by Matt T Heffron and mine. This is the problem.
 
And here is the solution: instead of pointless "100000" use int.MaxValue (and, to find the maximum, start with int.MinValue):
http://msdn.microsoft.com/en-us/library/system.int32.minvalue.aspx[^],
http://msdn.microsoft.com/en-us/library/system.int32.maxvalue.aspx[^].
 
All other integer types have similar property.
 
With floating-point types, things are way more interesting. You should use NegativeInfinity and PositiveInfinity:
http://msdn.microsoft.com/en-us/library/system.double.negativeinfinity.aspx[^],
http://msdn.microsoft.com/en-us/library/system.double.positiveinfinity.aspx[^].
 
The fundamentally important and amazing feature is: infinity values correctly compare with non-infinite values with operators '>', '<', '>=', '<=' and '=='!
 
—SA
  Permalink  
v4
Comments
Marcus Kramer at 3-Dec-12 16:55pm
   
+5. Well described.
Sergey Alexandrovich Kryukov at 3-Dec-12 17:12pm
   
Thank you, Marcus.
--SA
CIDev at 3-Dec-12 17:32pm
   
Nice answer, +5
Sergey Alexandrovich Kryukov at 3-Dec-12 18:00pm
   
Thank you.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 350
1 Zoltán Zörgő 248
2 Peter Leow 235
3 CPallini 195
4 OriginalGriff 180
0 Sergey Alexandrovich Kryukov 8,963
1 OriginalGriff 6,706
2 Peter Leow 4,167
3 Zoltán Zörgő 3,714
4 Kornfeld Eliyahu Peter 2,525


Advertise | Privacy | Mobile
Web03 | 2.8.150123.1 | Last Updated 3 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100