Jump to content

oqgmln1.png

 

if you enter any large number, then 0, then any large number after 0, that large number would go into the smallNum variable.

 

You could fix it by setting -999 as the initial smallNum variable (No need to change the STOP variable) then checking to see if smallNum is equal to the STOP variable

That's probably a good idea. 

Link to post
Share on other sites

Here's a quick possible example:

    class Program    {        static void Main(string[] args)        {            const string output = "You left the loop and ";            var inputs = new List<int>();            do            {                Console.WriteLine("Enter a value or -999 for exit:");                var input = Convert.ToInt32(Console.ReadLine());                if (input == -999)                {                    break;                }                inputs.Add(Convert.ToInt32(input));            } while (true);                        Console.WriteLine(inputs.Count > 0 ? string.Format("{0}the smallest value was: {1}", output, inputs.Min()) : string.Format("{0}there were no values entered.", output));            Console.ReadKey();        }    }

The single biggest problem in communication is the illusion that it has taken place.

Link to post
Share on other sites

I hate that condensed if statement. Drives me insane. So hard to read. Thank you though. 

 

That would be a ternary expression. I suspected that you'd have your own output mechanisms :P

 

Still not getting it to work. :(

 

So what's going wrong?

The single biggest problem in communication is the illusion that it has taken place.

Link to post
Share on other sites

That would be a ternary expression. I suspected that you'd have your own output mechanisms :P

 

 

So what's going wrong?

Most programmers do. I've never like ternary statements. Not in C++ and not in C#. I like readability over condensing code. Though I see it's uses don't get me wrong. I just can't read it efficiently and get confused with what exactly it's saying. Now as for what is going wrong. To be honest I don't know. I'm not 100% on where my logic SHOULD be that would make it work. I need to take the smallest input and output it. What I have is an if statement that says. 

 if (smallNum < tempNum && smallNum != 0 && tempNum != STOP) {       smallNum = tempNum;//small number is set to temp number }
Link to post
Share on other sites

My output is always 0 for the smallest number no matter if I input a 0 or not. Obviously it's because I have smallNum initialized to 0 in my declarations. What I can't seem to figure out is what I need to implement in order to make the smallest value input stored as smallNum no matter what it's value may be. It seems simple enough but I am stuck.

Link to post
Share on other sites

I got it half way working at one point but it was still incorrect. It would take any value. No matter what it was. If I input a 0 and then a 1 it would say the smallest value is 1. That's obviously not correct xD Oddly enough if I input 299 and 298 it would take the 298 as the smallest value. I don't quite get it but eh I've been looking at tthis same code for over 9 hours straight.

Link to post
Share on other sites

Here's the code that half worked. 

if ((smallNum == 0 || smallNum > tempNum) && tempNum != STOP)//if small number is equal to 0 or if small num is less than temp number and temp number is NOT equavalent to const STOP variable (-999){      smallNum = tempNum;//small number is set to temp number }
Link to post
Share on other sites

...I don't quite get it but eh I've been looking at tthis same code for over 9 hours straight.

 

The first priority is to calm down and not to start entering that spiral we both know so well. I too get stuck very easily when dealing with mathematical code as I have dyscalculia... though this could be argued to be more of a logic based problem.

 

With no refinements try this (I guess you can't use the list to do the heavy lifting for you):

    class Program    {        static void Main(string[] args)        {            var smallestNumber = int.MaxValue;            var hasInput = false;            do            {                Console.WriteLine("Enter a value or -999 for exit:");                var input = Convert.ToInt32(Console.ReadLine());                if (input == -999)                {                    break;                }                hasInput = true;                if (input < smallestNumber)                {                    smallestNumber = input;                }            } while (true);            if (!hasInput)            {                Console.WriteLine("You left the loop and there were no values entered.");            }            else            {                Console.WriteLine("You left the loop and the smallest value was: {0}", smallestNumber);            }                        Console.ReadKey();        }    }

I even left all of the if statements in for you  :lol:

The single biggest problem in communication is the illusion that it has taken place.

Link to post
Share on other sites

The first priority is to calm down and not to start entering that spiral we both know so well. I too get stuck very easily when dealing with mathematical code as I have dyscalculia... though this could be argued to be more of a logic based problem.

 

With no refinements try this:

    class Program    {        static void Main(string[] args)        {            var smallestNumber = int.MaxValue;            var hasInput = false;            do            {                Console.WriteLine("Enter a value or -999 for exit:");                var input = Convert.ToInt32(Console.ReadLine());                if (input == -999)                {                    break;                }                hasInput = true;                if (input < smallestNumber)                {                    smallestNumber = input;                }            } while (true);            if (!hasInput)            {                Console.WriteLine("You left the loop and there were no values entered.");            }            else            {                Console.WriteLine("You left the loop and the smallest value was: {0}", smallestNumber);            }                        Console.ReadKey();        }    }

I even left all of the if statements in for you  :lol:

Thank you. Much appreciated. Never heard of Dyscalculia before that's interesting. And yes it is a logical issue. 

Link to post
Share on other sites

Okie doke. That actually worked. Obviously I'm not gonna use the code you provided but you helped me solve the issue. It was the fact that I had intialized smallNum to 0 so that was sticking. AS I said I'm still learning C# and the .NET library so I wasn't aware of the int.MaxValue thing. That fixed it. Thank you so much :)

 

Here's what worked when I set smallNum initialized to int.MaxVal

 

 if (tempNum < smallNum && tempNum != STOP && smallNum != STOP)//if small number is equal to 0 or if small num is less than temp number and temp number is NOT equavalent to const STOP variable (-999){      smallNum = tempNum;//small number is set to temp number}
Link to post
Share on other sites

Well I still have a bunch of schoolwork to do and it's 3 am so I gotta go. Thanks again. I'm gonna add you if that's okay maybe we can bullshit about programming. I am really passionate about it :)

 

No problem, good luck and that would be great, I shall look forward to the bullshit  :D

The single biggest problem in communication is the illusion that it has taken place.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×