ltantonov
ltantonov

Reputation: 143

How can I fill an array with user input in case of numbers

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);
    }

Upvotes: 0

Views: 6342

Answers (6)

Tim
Tim

Reputation: 28530

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.

Upvotes: 2

Ashok kumar
Ashok kumar

Reputation: 1621

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.

Upvotes: 0

No Idea For Name
No Idea For Name

Reputation: 11597

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

Upvotes: 2

virusrocks
virusrocks

Reputation: 871

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  
    }

Upvotes: 0

Vishweshwar Kapse
Vishweshwar Kapse

Reputation: 939

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);
}

Upvotes: 0

Adil
Adil

Reputation: 148180

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]);

Upvotes: 0

Related Questions