2

I have been searching all night for a fix for a storage system for users inputs from a loop up to the limmit of 3 loop. i belive i have found it but as the title says i get the error Cannot implicitly Convert Type 'int' to 'int[]', on the console.readline for the user input? any suggestions on how i can solve this ?

Thank you In advnace.

            //Array For Ticket prices, sales and user input
            int[] TicketChoices = new int[3];

            //Ticket Types
            //ChildT = £1.50 = Child;
            //AdultT = £2.35 = Adult;
            //StudentT = £1.99 = Student;

            //Film      Certificate     Seats   Screen
            //Jaws          12A          15       1
            //The Exorcist  18           33       2

            cw("Hello Current tickets are:");

            for (int I = 0; I < 3; I++)

                {
                    cw("ID (1) Child, £1.50");
                    cw("ID:(2) Adult, £2,35");
                    cw("ID:(3) Student £1.99");
                    cw("");
                    cw("Please Select Which ticket you would like to input By Entering it's id Number");
                    cw("input Must be between 1-3 for it to be vaild.");
                    TicketChoices = int.Parse(Console.ReadLine());

                }

3 Answers 3

1

Here's what I think you are trying to do:

    static void Main()
    {
        //Array For Ticket prices, sales and user input
        var ticketChoices = new int[3];

        //Ticket Types
        //ChildT = £1.50 = Child;
        //AdultT = £2.35 = Adult;
        //StudentT = £1.99 = Student;

        //Film      Certificate     Seats   Screen
        //Jaws          12A          15       1
        //The Exorcist  18           33       2

        Console.WriteLine("Hello Current tickets are:");

        for (var i = 0; i < 3; i++)

        {
            Console.WriteLine("ID (1) Child, £1.50");
            Console.WriteLine("ID:(2) Adult, £2,35");
            Console.WriteLine("ID:(3) Student £1.99");
            Console.WriteLine("");
            Console.WriteLine("Please Select Which ticket you would like to input By Entering it's id Number");
            Console.WriteLine("input Must be between 1-3 for it to be vaild.");
            var valid = false;
            while (!valid)
            {
                var input = Console.ReadLine();
                if (int.TryParse(input, out var ticketNumber))
                {
                    if (ticketNumber >= 0 && ticketNumber <= 3)
                    {
                        valid = true; 
                    }
                }

                if (valid)
                {
                    ticketChoices[i] = ticketNumber;
                }
                else
                {
                    Console.WriteLine("Please enter a value between 1 and 3");
                }
            }
        }

        // Print the results 
        Console.WriteLine("You entered:");
        foreach (var ticketChoice in ticketChoices)
        {
            Console.WriteLine(ticketChoice);
        }

        Console.ReadLine();
    }
Sign up to request clarification or add additional context in comments.

Comments

1
int[] TicketChoices = new int[3];

TicketChoices is not an int its an array of int

TicketChoices = int.Parse(Console.ReadLine());

Maybe something like this instead

var choice = int.Parse(Console.ReadLine());

Also if you take input form a user don't, trust them to get it right

Use TryParse Instead

Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded.

Comments

0

int.Parse returns a single integer. So you are trying to set TicketChoices (an array) to a single integer. That won't work.

You can set the first integer in your array to the output of int.Parse, if that's what you want:

TicketChoices[0] = int.Parse(Console.ReadLine());

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.