2

I am trying to make a program that calculates some specific data from numbers given by a user. In this example, my program counts amount of numbers in range (10,103) that are divisible by 2, and amount of numbers that are in range (15,50) divisible by 3 within numbers given by user. On this stage, my program gives the results, when 10 numbers are given (as I specified it in the loop). How can I make my program stop reading numbers and give the results when user imputs an empty line no matter if he, entered 5 or 100 numbers before?

Here is my code, as it looks for now:

using System;

namespace Program1
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            int input10_103_div_2 = 0;
            int input15_50_div_3 = 0;

            for (int i = 0; i < 10; i++)
            {
                string input = Console.ReadLine ();
                double xinput = double.Parse (input);

                if (xinput > 10 && xinput <= 103 && (xinput % 2) == 0)
                {
                    input10_103_div_2++;
                }
                if (xinput > 15 && xinput < 50 && (xinput % 3) == 0) 
                {
                    input15_50_div_3++;
                }
            }
            Console.WriteLine ("Amount of numbers in range (10,103) divisible by 2: " + input10_103_div_2);
            Console.WriteLine ("Amount of numbers in range (15,50) divisible by 3: " + input15_50_div_3);
        }
    }
}

3 Answers 3

5

instead of for, do:

string input = Console.ReadLine();
while(input != String.Empty)
{
     //do things
     input = Console.ReadLine();
}

if you're trying to allow any number of inputs. Or

if(input == "")
    break;

if you want the for loop

Sign up to request clarification or add additional context in comments.

Comments

2

Change your loop to go forever and break out of the loop when the string is empty:

for (;;)
{
    string input = Console.ReadLine ();
    if (String.IsNullOrEmpty(input))
    {
        break;
    }

    // rest of code inside loop goes here
}

Comments

0

If you want to restructure the loop, you can use a do while loop:

string input;
do{
    input = Console.ReadLine();
    //stuff
} while(!string.IsNullOrEmpty(input));

If you just want to be able to break early:

string input = Console.ReadLine ();
if(string.IsNullOrEmpty(str))
  break;
double xinput = double.Parse (input);   

12 Comments

in this scenario he will be doing //stuff against an empty string. He'll need another if for validation
@Jonesy Right, that is fine. Do while conveys his logic the best and I was shooting for his requirement of "no matter if he entered 5 or 100".
@Jonesy Which in my opinion is no worse than your answer using two Console.ReadLine.
Yeah I suppose its just personal choice
@AustinHenley Why would you think that?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.