0

I would like my program to fill an array with user input, but with an numeric input (then program will make specific calculations with that numbers, but it's not important for now).

If no input is done, program should stop reading numbers and print it. I have a couple of errors, especially in case of parsing, because I have tried a couple of solutions, and I have no idea in which part of code and maybe what way, numbers in an array should be parsed to avoid receiving an "cannot implicitly convert type string to int" or "cannot implicitly convert type int[] to int".

This how my code looks like:

public static void Main (string[] args)
    {
        int[] userInput = new int[100];
        int xuserInput = int.Parse (userInput);

        for (int i = 0; i<userInput.Length; i++)
        {
            userInput[i] = Console.ReadLine ();

            if (userInput == "")
                break;
        }
        Console.WriteLine (userInput);
    }
2
  • Looks like you're after int.Parse... Commented Aug 6, 2013 at 8:47
  • This is not like Ruby/Python. Type conversions aren't done magically for you. Commented Aug 6, 2013 at 8:47

6 Answers 6

2

you should take the input to a string and try to parse it to integer:

  public static void Main(string[] args)
  {
     int[] userInput = new int[100];
     int counter = 0;

     for (counter = 0; counter < userInput.Length; counter++)
     {
        string input = Console.ReadLine();

        if (input == "")
           break;
        else
           int.TryParse(input, out userInput[counter]);
     }

     for (int i = 0; i < counter; i++)
     {
        Console.WriteLine(userInput[i]);            
     }

     Console.ReadLine();
  }

try parse will not throw exception like parse will.

if you decide to use parse, catch exceptions

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

2 Comments

it doesn't throw any errors for now, but after reading user input, it doesn't write numbers read in a final WriteLine statement, but "System.Int32[]"
@ArturAntonczyk fixed that, even though you might want to use List<int> instead of array
2

Try this:

int[] userInputs = new int[100];
int parsedInput;
int inputs = 0;
bool stop = false;

while (inputs < 100 && !stop)
{

    string userInput = Console.ReadLine();

    if (userInput == "")
    {
        stop = true;
    }
    else if (Int32.TryParse(userInput, out parsedInput))
    {
        userInputs[i] = parsedInput;
        inputs++;
    }
    else
    {
        Console.WriteLine("Please enter a number only!");
    } 
}

for each (int number in userInputs)
{
    Console.WrietLine(number.ToString());
}

This code does a few things.

First, a while loop is used to ensure the user inputs 100 numbers or doesn't enter any data.

Next, it gets the input from the user. If it's an empty input, it sets the stop flag to true, which will exit the loop.

If the input wasn't empty, it uses TryParse to determine if the input is a number. If it is, it returns true and the converted input is added to the array and the counter incremented.

If the parse fails, the user is prompted to enter a number.

Once the array is filled, it loops through the array and prints out each input.

3 Comments

Thanks for the edit, @Heslacher - I missed that when I posted it :)
My edit had been wrong. I have tried to update it 3 or 4 times, but somehow it hadn`t gone through. You need to change int[] userInput = new int[100]; to int[] input = new int[100]; and userInput[i] = parsedInput; to input[i] = parsedInput; Sorry about this.
@Heslacher - Thanks - I changed userInput to userInputs; should fix the issue :)
0

The problem is that you are parsing userInput which is an array with int.Parse instead of parsing the input you got by Console.ReadLine()

int xuserInput = int.Parse (userInput); // Remove this statement.

For parsing user input you need to parse like this

string input = Console.ReadLine ();
if (input == "")
    break;
else
    int.TryParse(input, out userInput[i]);

Comments

0

try this.

public static void Main (string[] args)
{
    int[] userInput = new int[100];
    int xuserInput = int.Parse (userInput);

    for (int i = 0; i<userInput.Length; i++)
    {
        int temp = int.Parse(Console.ReadLine());
        userInput[i] = temp;

        if (userInput == "")
            break;
    }
    Console.WriteLine (userInput);
}

Comments

0

You just might want to use this

    static void Main(string[] args)
    {
      int[] userInput = new int[100];
      string recievedInput = "";  
    for (int i = 0; i<userInput.Length; i++)
    {
        recievedInput = Console.ReadLine();
        int.TryParse(recievedInput, out userInput[i]);
        if (recievedInput == "")
            break;
    }
    Console.WriteLine (userInput); //this will only print the type name of Userinput not all element  
    }

Comments

0

The following program reads numbers from user.

If user enters an invalid number, then it reports with the message: Not a valid number.

If user enters nothing, then the program prints all the numbers entered by the user.

class Program
{
    static void Main(string[] args)
    {
        int[] userInput = new int[10];
        for(int count = 0; count <= 9; count++)
        {
            int number;
            string input = Console.ReadLine();
            bool result = Int32.TryParse(input, out number);
            if (result)
            {
                userInput[count] = number;
            }
            else if (!result)
            {
                if (input != string.Empty)
                    Console.WriteLine("Not a valid number.");
                else if (input.Equals(string.Empty))
                {
                    foreach (var item in userInput)
                    {
                        Console.WriteLine(item.ToString());
                    }
                    Console.WriteLine("Press any key to continue...");
                    Console.ReadKey(true);
                    return;
                }
            }
        }
    }
}

Please let me know, if this is okay to you.

Comments

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.