Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying that when i input 10 random numbers on console then message will be display
these numbers are less than 10

e..g

if i input these random numbers
10,9,12,45,120,2,3,1,9,14
then
message display
9,2,3,1,9 are less than 10

i try this

What I have tried:

C#
public void p1()
{
   try
   {
       Console.WriteLine("Enter numbers");

       int num = Convert.ToInt32(Console.ReadLine());

       if (num < 10)
       {
           Console.WriteLine("less num",num);
           Console.ReadLine();

       }
       else
       {
           Console.WriteLine("Not less than num", num);
           Console.ReadLine();
       }
   }
   catch(Exception ex)
   {
       throw new Exception();
   }


}


but this work like that when i enter only 1 e.g. if i enter 9 number then message display less number and when i enter 12 then message display Not less than num

where as i want
i input 10 numbers and from that program identifies which numbers are less than 10

how i do that

CHECK THIS GIF
http://i.imgur.com/xmfiApI.gifv[^]
Posted
Updated 10-Oct-16 3:42am
v4
Comments
Prabu_Anand22 10-Oct-16 3:20am    
try getting the user inputs into an array of type int, then using a for loop, loop through the content checking for the values smaller or larger if so display the message as desired.
super_user 10-Oct-16 3:41am    
http://i.imgur.com/xmfiApI.gifv

C#
int num = Convert.ToInt32(Console.ReadLine());

This line will only convert a single number, since that is what you have asked it to do. You should deal with multiple numbers by something like:
C#
string[] numbers = Console.ReadLine().split(" ");
foreach (string num in numbers)
{
    int num = Convert.ToInt32(num);
    // test for less or not
}

Note it is better to use TryParse rather than Convert to allow for the case when the user types invalid characters.
 
Share this answer
 
Comments
super_user 10-Oct-16 5:24am    
this show error
Error 1 The best overloaded method match for 'string.Split(params char[])' has some invalid arguments
Error 2 Argument 1: cannot convert from 'string' to 'char[]'
Richard MacCutchan 10-Oct-16 5:35am    
Then now would be a good time to go to the MSDN documentation and learn the String.split method(s).
Quote:
but this work like that when i enter only 1 e.g. if i enter 9 number then message display less number and when i enter 12 then message display Not less than num

where as i want
i input 10 numbers and from that program identifies which numbers are less than 10
Your code is doing exactly what you requested.
How is it possible that you don't understand what the code is doing ? Is it your code ?

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
super_user 10-Oct-16 3:30am    
no... when i enter 10 numbers in horizontal and enter then error display An unhandled exception of type 'System.Exception' occurred in practise.exe
where as when i try to enter number in vertical i.e when i input number 12 then press enter then message display that not less than 10 ...where as i want to input 10 numbers then want to display message
super_user 10-Oct-16 3:41am    
check this GIF
http://i.imgur.com/xmfiApI.gifv
Patrice T 10-Oct-16 4:35am    
GIF do not exist
super_user 10-Oct-16 5:19am    
check again .. this is exist
Patrice T 10-Oct-16 5:27am    
did you make it available to public ?
I would have solved it this way:

C#
namespace check_Numbers
{
    class Program
    {
        static void Main(string[] args)
        {    
            string[] seperators = {",", " ", ";", "."};
            int num2;
            Console.WriteLine("Input numbers over 10:");

            string[] splitArray = Console.ReadLine().Split(seperators, StringSplitOptions.RemoveEmptyEntries);
            foreach (string num in splitArray)
            {

            If(Int32.TryParse(num, out num2) == true)
            {
                if (num2 < 10)
                {
                    Console.WriteLine(num + " is smaller than 10");
                }
            }
            else
            {
                Console.WriteLine(num + " is not a f***ing number...");
            }
            }
        }
    }
}
 
Share this answer
 
v7
Comments
[no name] 10-Oct-16 6:52am    
Why do you convert a string to a string? Why are you using Convert.ToInt32? What happens when the user types in 1 2 3 a b c?
Member 12747274 10-Oct-16 7:59am    
Thank you for the feedback. First of all im a beginner in C# and I think the convertion of string into a string is a leftover from my troubleshooting of some strange problem i had with the ".Split". i couldn't integrate just a simple char (",") and i made a workaround with the string array. Yes, its absoultely unnecessary!

But for the Convert.ToInt: I did that to check if the number is smaller than 10?
I think it's needed or am I wrong? But yes you're right, i should "try and catch" this!
[no name] 10-Oct-16 8:27am    
No you shouldn't use Convert at all. You will get an exception if someone types in anything other than a number. Should us int.TryParse which will not throw an exception if the conversion fails.
Member 12747274 10-Oct-16 9:22am    
I updated my Code now, thanks for the advice. But now there is the Problem, that every character is seen as "under 10". Wouldn't be an exception + try and catch better, to return a failure message? Or is there a possibility to "ignore" character inputs?
[no name] 10-Oct-16 9:34am    
Since TryParse returns a bool indicating success or failure, your error message would be shown in the else part of your if. Exceptions can be expensive in terms of processing so I try to avoid them as much as possible. You *could* catch the exception here but there is a better way to do it.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900