简体   繁体   中英

C# index is out of bounds with array

I am making a "game" where you enter all 17 digits of pi using Math.PI. My idea was that you start off with typing in the first 3 digits of Pi (3.14) and then continue.

using System;

public class Program
{
   static void Main(string[] args)
   {
        double Pi = Math.PI;
        string Pi_s = Pi.ToString();
        char[] Pi_c = Pi_s.ToCharArray();

        Console.WriteLine("Write the first 3 digits of pi");

        string f = Console.ReadLine();
        char[] e = f.ToCharArray();
        int ind = 3;

        while (ind < 16)
        {

            if(e[ind] == Pi_c[ind]) //here I get "Index was out of bounds with array"
            {
                Console.WriteLine("Congrats you got it right!");
                ind = ind ++;
                Console.WriteLine($"Now, type the first {ind} letters of pi");
            }
            else
            {
                Console.WriteLine("You got something wrong :(");
                break;
            }
        }

        
   }
   
}

I tried to change the value of ind or to try to add it to a different value once you get it right but it did not work. I think that the problem is that you increment the value of ind but I'm not sure.

Replace your Main() body with this:

        double Pi = Math.PI;
        string Pi_s = Pi.ToString();
        char[] Pi_c = Pi_s.ToCharArray();  //<<<< Fixed this.
        string f = "";

        while (f != "3.14")
        {
            Console.WriteLine("Write the first 3 digits of Pi.");
            f = Console.ReadLine();
            if (f != "3.14")
            {
                Console.WriteLine("You got something wrong :(");
                Console.WriteLine("You must enter the first 3 digits as in 3.14");
            }
            else
            {
                Console.WriteLine("Congrats you got it right!");
            }
        }

        int ind = 4;
        while (ind < 16)
        {
            Console.WriteLine($"Now, type the {ind}th digit of Pi.");
            if (Console.ReadKey().KeyChar == Pi_c[ind])
            {
                Console.Write("\n");
                Console.WriteLine("Congrats you got it right!");
                ind++; //ind = ind++;  <<<<<<< Recommend using the shorthand here.
            }
            else
            {
                Console.Write("\n");
                Console.WriteLine("You got something wrong :(");
            }
        }
        Console.WriteLine("Congrats you WIN!");

This fixes the P2i bug and does the following:

  1. Loops until receiving the initial "3.14" input.
  2. Adds input reception in the form of Console.ReadKey() to continually get the next character in the loop.
  3. Adds a final message to the program.

I trust this answers your question. ;-)

Because array's index is starting 0. And if you include 3 number as this "3.1" array's max index become 2. But you started "ind" from 3. And array's third element is empty. Result of it "ERROR". Thats a True Version:

 static void Main(string[] args)
    {
        string Pi_s = Math.PI.ToString();
        char[] Pi_c = Pi_s.ToCharArray();
        Console.WriteLine("Write the first digit of pi");
        char[] array = new char[16];
        int ind = 0;        
        while (ind < 16)
        {
            array[ind] = Convert.ToChar(Console.ReadLine());
            if (array[ind] == Pi_c[ind]) 
            {
                Console.WriteLine("Congrats you got it right!");
                ind++;
                Console.WriteLine($"Now, type the first {ind+1} letters of pi");
            }
            else
            {
                Console.WriteLine("You got something wrong :(");
                break;
            }
        }
        Console.ReadKey();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM