c# console get array value with index from user input












1















I'm in an intro to programming class. Is it possible to output an array value when a user selects its index? Its not much but this is my code so far:



double cisTuition = new double { 0.00, 1.00, 1.50, 2.00, 2.50 };
Console.WriteLine("Please choose the semester");









share|improve this question























  • Hi Jade - Are you asking to simply print out (ala, WriteLine or similar) one of the values in your array? Akin to Console.WriteLine(cisTuition[0]) printing out 0.00?

    – gravitymixes
    Nov 15 '18 at 16:26











  • Hint: you can access the value of an element of an array by its index. Ex. cisTuition[n], when n is the index number. Remember that arrays are indexed starting at 0.

    – Michael Roy
    Nov 15 '18 at 16:26
















1















I'm in an intro to programming class. Is it possible to output an array value when a user selects its index? Its not much but this is my code so far:



double cisTuition = new double { 0.00, 1.00, 1.50, 2.00, 2.50 };
Console.WriteLine("Please choose the semester");









share|improve this question























  • Hi Jade - Are you asking to simply print out (ala, WriteLine or similar) one of the values in your array? Akin to Console.WriteLine(cisTuition[0]) printing out 0.00?

    – gravitymixes
    Nov 15 '18 at 16:26











  • Hint: you can access the value of an element of an array by its index. Ex. cisTuition[n], when n is the index number. Remember that arrays are indexed starting at 0.

    – Michael Roy
    Nov 15 '18 at 16:26














1












1








1








I'm in an intro to programming class. Is it possible to output an array value when a user selects its index? Its not much but this is my code so far:



double cisTuition = new double { 0.00, 1.00, 1.50, 2.00, 2.50 };
Console.WriteLine("Please choose the semester");









share|improve this question














I'm in an intro to programming class. Is it possible to output an array value when a user selects its index? Its not much but this is my code so far:



double cisTuition = new double { 0.00, 1.00, 1.50, 2.00, 2.50 };
Console.WriteLine("Please choose the semester");






c# console-application






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 16:22









JadeJade

84




84













  • Hi Jade - Are you asking to simply print out (ala, WriteLine or similar) one of the values in your array? Akin to Console.WriteLine(cisTuition[0]) printing out 0.00?

    – gravitymixes
    Nov 15 '18 at 16:26











  • Hint: you can access the value of an element of an array by its index. Ex. cisTuition[n], when n is the index number. Remember that arrays are indexed starting at 0.

    – Michael Roy
    Nov 15 '18 at 16:26



















  • Hi Jade - Are you asking to simply print out (ala, WriteLine or similar) one of the values in your array? Akin to Console.WriteLine(cisTuition[0]) printing out 0.00?

    – gravitymixes
    Nov 15 '18 at 16:26











  • Hint: you can access the value of an element of an array by its index. Ex. cisTuition[n], when n is the index number. Remember that arrays are indexed starting at 0.

    – Michael Roy
    Nov 15 '18 at 16:26

















Hi Jade - Are you asking to simply print out (ala, WriteLine or similar) one of the values in your array? Akin to Console.WriteLine(cisTuition[0]) printing out 0.00?

– gravitymixes
Nov 15 '18 at 16:26





Hi Jade - Are you asking to simply print out (ala, WriteLine or similar) one of the values in your array? Akin to Console.WriteLine(cisTuition[0]) printing out 0.00?

– gravitymixes
Nov 15 '18 at 16:26













Hint: you can access the value of an element of an array by its index. Ex. cisTuition[n], when n is the index number. Remember that arrays are indexed starting at 0.

– Michael Roy
Nov 15 '18 at 16:26





Hint: you can access the value of an element of an array by its index. Ex. cisTuition[n], when n is the index number. Remember that arrays are indexed starting at 0.

– Michael Roy
Nov 15 '18 at 16:26












6 Answers
6






active

oldest

votes


















2














You need to access using index of the array



Console.WriteLine(cisTuition[index]);


For example, if you need to get the index from the user entered input value,



int input = int.Parse(Console.ReadLine());
Console.WriteLine("value is: " + cisTuition[input]);





share|improve this answer



















  • 1





    Thank you! This totally worked!

    – Jade
    Nov 15 '18 at 16:48



















2














Yes, you can get the value of the cisTuition array by their index: Check the snippet below



double cisTuition = new double { 0.00, 1.00, 1.50, 2.00, 2.50 };
Console.WriteLine("Please choose the semester");

int index = int.Parse(Console.ReadLine());
Console.WriteLine("value is: " + cisTuition[index]);


Of course, you can improve that snippet validating that the index is inside the bounds of the array, but that's the basic idea.






share|improve this answer


























  • Yes! Thank you!

    – Jade
    Nov 15 '18 at 16:50



















1














Yes, you can reach an value inside and array at a particular index position as below:



double cisTuition = new double { 0.00, 1.00, 1.50, 2.00, 2.50 };
Console.WriteLine(cisTuition[0]);


Ouput:



0.00


Then, as you requested to have an index provided by an input, I would use Console.ReadLine() to get the user choice and record it into a variable (index).



Lastly, I would use the variable as index with cisTuition[index].



See the full code below:



double cisTuition = new double { 0.00, 1.00, 1.50, 2.00, 2.50 };
Console.WriteLine("Enter input:"); // Prompt the question
string index = Console.ReadLine(); // Record the input
Console.WriteLine(cisTuition[index]); // Show the array value of this index





share|improve this answer


























  • Yes, but how would a user be able to select the index with like a Console.ReadLine()?

    – Jade
    Nov 15 '18 at 16:29











  • Yes totally, please look at the code.

    – Jonathan Gagne
    Nov 15 '18 at 16:56





















1














    bool Keeplooping = true; //Boolean to tell whether the loops continues
while (Keeplooping == true) //while the user hasn't chosen a valid index
{
Console.WriteLine("Select an index");
try //if this fails then the input is not an int or too big/small
{
int index = int.Parse(Console.ReadLine()); //receive input
Console.WriteLine(cisTuition[index].ToString()); //output the value
Keeplooping = false; //loop will end after this iteration
}
catch //alerts user that the input is bad and tries again
{
Console.WriteLine("Please select a valid index");
}
}


This should do the trick (I tested it already to be sure)






share|improve this answer

































    0














    Well first you need the users input, when you got this you can do your proper checks, like is it NaN, or if it's outside the arrays index. After this it should be easy for look out the index in the array and then print it. I dont program that much in C#, but i guess it could look something like:



    double cisTuition = new double { 0.00, 1.00, 1.50, 2.00, 2.50 };
    Console.WriteLine("Please choose the semester");



    var UserInput = getUserInput();



    if(UserInput == NaN || UserInput > cisTuition.length)



    print( cisTuition[UserInput] )



    I know this is definitely not the correct syntax, but the logic should work.
    Hope it helps.






    share|improve this answer



















    • 1





      Please format your code block.

      – Tau
      Nov 15 '18 at 17:08



















    0














    I think what is being missed here is how to read in a value, convert it to an int if possible, and then use that as the reference to the array index.



    So, if that's the case, you'd want something akin to this added to the end of your code:



    string strUserInput = Console.ReadLine();
    int? iConverted = null;
    int.TryParse(strUserInput, out iConverted);
    if ((iConverted != null) && (iConverted <= (cisTuition.Length - 1)) )
    {
    Console.WriteLine(cisTuition[iConverted]);
    }
    else
    {
    Console.WriteLine("Invalid value or index of the array");
    }


    Full explanation:



    ReadLine() pulls in the value the user provides as the index to the array



    iConverted is instantiated (as null) to start, to allow an integer to also be null we have to use the ? as well



    We TryParse the strUserInput into an Integer, and dump it into iConverted if successful (if not, iConverted remains null)



    Finally, we print the requested index array, or notify the user that their index was out of bounds, or not properly parsed.






    share|improve this answer





















    • 1





      AFAIK iConverted would have to be an int? for it to be able to store null

      – Tau
      Nov 15 '18 at 17:02











    • Fair point @Tau - What I get for writing off the cuff. :)

      – gravitymixes
      Nov 15 '18 at 17:27











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323768%2fc-sharp-console-get-array-value-with-index-from-user-input%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    6 Answers
    6






    active

    oldest

    votes








    6 Answers
    6






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    You need to access using index of the array



    Console.WriteLine(cisTuition[index]);


    For example, if you need to get the index from the user entered input value,



    int input = int.Parse(Console.ReadLine());
    Console.WriteLine("value is: " + cisTuition[input]);





    share|improve this answer



















    • 1





      Thank you! This totally worked!

      – Jade
      Nov 15 '18 at 16:48
















    2














    You need to access using index of the array



    Console.WriteLine(cisTuition[index]);


    For example, if you need to get the index from the user entered input value,



    int input = int.Parse(Console.ReadLine());
    Console.WriteLine("value is: " + cisTuition[input]);





    share|improve this answer



















    • 1





      Thank you! This totally worked!

      – Jade
      Nov 15 '18 at 16:48














    2












    2








    2







    You need to access using index of the array



    Console.WriteLine(cisTuition[index]);


    For example, if you need to get the index from the user entered input value,



    int input = int.Parse(Console.ReadLine());
    Console.WriteLine("value is: " + cisTuition[input]);





    share|improve this answer













    You need to access using index of the array



    Console.WriteLine(cisTuition[index]);


    For example, if you need to get the index from the user entered input value,



    int input = int.Parse(Console.ReadLine());
    Console.WriteLine("value is: " + cisTuition[input]);






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 15 '18 at 16:27









    SajeetharanSajeetharan

    124k30180243




    124k30180243








    • 1





      Thank you! This totally worked!

      – Jade
      Nov 15 '18 at 16:48














    • 1





      Thank you! This totally worked!

      – Jade
      Nov 15 '18 at 16:48








    1




    1





    Thank you! This totally worked!

    – Jade
    Nov 15 '18 at 16:48





    Thank you! This totally worked!

    – Jade
    Nov 15 '18 at 16:48













    2














    Yes, you can get the value of the cisTuition array by their index: Check the snippet below



    double cisTuition = new double { 0.00, 1.00, 1.50, 2.00, 2.50 };
    Console.WriteLine("Please choose the semester");

    int index = int.Parse(Console.ReadLine());
    Console.WriteLine("value is: " + cisTuition[index]);


    Of course, you can improve that snippet validating that the index is inside the bounds of the array, but that's the basic idea.






    share|improve this answer


























    • Yes! Thank you!

      – Jade
      Nov 15 '18 at 16:50
















    2














    Yes, you can get the value of the cisTuition array by their index: Check the snippet below



    double cisTuition = new double { 0.00, 1.00, 1.50, 2.00, 2.50 };
    Console.WriteLine("Please choose the semester");

    int index = int.Parse(Console.ReadLine());
    Console.WriteLine("value is: " + cisTuition[index]);


    Of course, you can improve that snippet validating that the index is inside the bounds of the array, but that's the basic idea.






    share|improve this answer


























    • Yes! Thank you!

      – Jade
      Nov 15 '18 at 16:50














    2












    2








    2







    Yes, you can get the value of the cisTuition array by their index: Check the snippet below



    double cisTuition = new double { 0.00, 1.00, 1.50, 2.00, 2.50 };
    Console.WriteLine("Please choose the semester");

    int index = int.Parse(Console.ReadLine());
    Console.WriteLine("value is: " + cisTuition[index]);


    Of course, you can improve that snippet validating that the index is inside the bounds of the array, but that's the basic idea.






    share|improve this answer















    Yes, you can get the value of the cisTuition array by their index: Check the snippet below



    double cisTuition = new double { 0.00, 1.00, 1.50, 2.00, 2.50 };
    Console.WriteLine("Please choose the semester");

    int index = int.Parse(Console.ReadLine());
    Console.WriteLine("value is: " + cisTuition[index]);


    Of course, you can improve that snippet validating that the index is inside the bounds of the array, but that's the basic idea.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 19 '18 at 21:37

























    answered Nov 15 '18 at 16:28









    Daniel CorzoDaniel Corzo

    1,46221527




    1,46221527













    • Yes! Thank you!

      – Jade
      Nov 15 '18 at 16:50



















    • Yes! Thank you!

      – Jade
      Nov 15 '18 at 16:50

















    Yes! Thank you!

    – Jade
    Nov 15 '18 at 16:50





    Yes! Thank you!

    – Jade
    Nov 15 '18 at 16:50











    1














    Yes, you can reach an value inside and array at a particular index position as below:



    double cisTuition = new double { 0.00, 1.00, 1.50, 2.00, 2.50 };
    Console.WriteLine(cisTuition[0]);


    Ouput:



    0.00


    Then, as you requested to have an index provided by an input, I would use Console.ReadLine() to get the user choice and record it into a variable (index).



    Lastly, I would use the variable as index with cisTuition[index].



    See the full code below:



    double cisTuition = new double { 0.00, 1.00, 1.50, 2.00, 2.50 };
    Console.WriteLine("Enter input:"); // Prompt the question
    string index = Console.ReadLine(); // Record the input
    Console.WriteLine(cisTuition[index]); // Show the array value of this index





    share|improve this answer


























    • Yes, but how would a user be able to select the index with like a Console.ReadLine()?

      – Jade
      Nov 15 '18 at 16:29











    • Yes totally, please look at the code.

      – Jonathan Gagne
      Nov 15 '18 at 16:56


















    1














    Yes, you can reach an value inside and array at a particular index position as below:



    double cisTuition = new double { 0.00, 1.00, 1.50, 2.00, 2.50 };
    Console.WriteLine(cisTuition[0]);


    Ouput:



    0.00


    Then, as you requested to have an index provided by an input, I would use Console.ReadLine() to get the user choice and record it into a variable (index).



    Lastly, I would use the variable as index with cisTuition[index].



    See the full code below:



    double cisTuition = new double { 0.00, 1.00, 1.50, 2.00, 2.50 };
    Console.WriteLine("Enter input:"); // Prompt the question
    string index = Console.ReadLine(); // Record the input
    Console.WriteLine(cisTuition[index]); // Show the array value of this index





    share|improve this answer


























    • Yes, but how would a user be able to select the index with like a Console.ReadLine()?

      – Jade
      Nov 15 '18 at 16:29











    • Yes totally, please look at the code.

      – Jonathan Gagne
      Nov 15 '18 at 16:56
















    1












    1








    1







    Yes, you can reach an value inside and array at a particular index position as below:



    double cisTuition = new double { 0.00, 1.00, 1.50, 2.00, 2.50 };
    Console.WriteLine(cisTuition[0]);


    Ouput:



    0.00


    Then, as you requested to have an index provided by an input, I would use Console.ReadLine() to get the user choice and record it into a variable (index).



    Lastly, I would use the variable as index with cisTuition[index].



    See the full code below:



    double cisTuition = new double { 0.00, 1.00, 1.50, 2.00, 2.50 };
    Console.WriteLine("Enter input:"); // Prompt the question
    string index = Console.ReadLine(); // Record the input
    Console.WriteLine(cisTuition[index]); // Show the array value of this index





    share|improve this answer















    Yes, you can reach an value inside and array at a particular index position as below:



    double cisTuition = new double { 0.00, 1.00, 1.50, 2.00, 2.50 };
    Console.WriteLine(cisTuition[0]);


    Ouput:



    0.00


    Then, as you requested to have an index provided by an input, I would use Console.ReadLine() to get the user choice and record it into a variable (index).



    Lastly, I would use the variable as index with cisTuition[index].



    See the full code below:



    double cisTuition = new double { 0.00, 1.00, 1.50, 2.00, 2.50 };
    Console.WriteLine("Enter input:"); // Prompt the question
    string index = Console.ReadLine(); // Record the input
    Console.WriteLine(cisTuition[index]); // Show the array value of this index






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 15 '18 at 17:01

























    answered Nov 15 '18 at 16:25









    Jonathan GagneJonathan Gagne

    2,1993722




    2,1993722













    • Yes, but how would a user be able to select the index with like a Console.ReadLine()?

      – Jade
      Nov 15 '18 at 16:29











    • Yes totally, please look at the code.

      – Jonathan Gagne
      Nov 15 '18 at 16:56





















    • Yes, but how would a user be able to select the index with like a Console.ReadLine()?

      – Jade
      Nov 15 '18 at 16:29











    • Yes totally, please look at the code.

      – Jonathan Gagne
      Nov 15 '18 at 16:56



















    Yes, but how would a user be able to select the index with like a Console.ReadLine()?

    – Jade
    Nov 15 '18 at 16:29





    Yes, but how would a user be able to select the index with like a Console.ReadLine()?

    – Jade
    Nov 15 '18 at 16:29













    Yes totally, please look at the code.

    – Jonathan Gagne
    Nov 15 '18 at 16:56







    Yes totally, please look at the code.

    – Jonathan Gagne
    Nov 15 '18 at 16:56













    1














        bool Keeplooping = true; //Boolean to tell whether the loops continues
    while (Keeplooping == true) //while the user hasn't chosen a valid index
    {
    Console.WriteLine("Select an index");
    try //if this fails then the input is not an int or too big/small
    {
    int index = int.Parse(Console.ReadLine()); //receive input
    Console.WriteLine(cisTuition[index].ToString()); //output the value
    Keeplooping = false; //loop will end after this iteration
    }
    catch //alerts user that the input is bad and tries again
    {
    Console.WriteLine("Please select a valid index");
    }
    }


    This should do the trick (I tested it already to be sure)






    share|improve this answer






























      1














          bool Keeplooping = true; //Boolean to tell whether the loops continues
      while (Keeplooping == true) //while the user hasn't chosen a valid index
      {
      Console.WriteLine("Select an index");
      try //if this fails then the input is not an int or too big/small
      {
      int index = int.Parse(Console.ReadLine()); //receive input
      Console.WriteLine(cisTuition[index].ToString()); //output the value
      Keeplooping = false; //loop will end after this iteration
      }
      catch //alerts user that the input is bad and tries again
      {
      Console.WriteLine("Please select a valid index");
      }
      }


      This should do the trick (I tested it already to be sure)






      share|improve this answer




























        1












        1








        1







            bool Keeplooping = true; //Boolean to tell whether the loops continues
        while (Keeplooping == true) //while the user hasn't chosen a valid index
        {
        Console.WriteLine("Select an index");
        try //if this fails then the input is not an int or too big/small
        {
        int index = int.Parse(Console.ReadLine()); //receive input
        Console.WriteLine(cisTuition[index].ToString()); //output the value
        Keeplooping = false; //loop will end after this iteration
        }
        catch //alerts user that the input is bad and tries again
        {
        Console.WriteLine("Please select a valid index");
        }
        }


        This should do the trick (I tested it already to be sure)






        share|improve this answer















            bool Keeplooping = true; //Boolean to tell whether the loops continues
        while (Keeplooping == true) //while the user hasn't chosen a valid index
        {
        Console.WriteLine("Select an index");
        try //if this fails then the input is not an int or too big/small
        {
        int index = int.Parse(Console.ReadLine()); //receive input
        Console.WriteLine(cisTuition[index].ToString()); //output the value
        Keeplooping = false; //loop will end after this iteration
        }
        catch //alerts user that the input is bad and tries again
        {
        Console.WriteLine("Please select a valid index");
        }
        }


        This should do the trick (I tested it already to be sure)







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 15 '18 at 18:10

























        answered Nov 15 '18 at 16:51









        DJ Spicy Deluxe-LeviDJ Spicy Deluxe-Levi

        1596




        1596























            0














            Well first you need the users input, when you got this you can do your proper checks, like is it NaN, or if it's outside the arrays index. After this it should be easy for look out the index in the array and then print it. I dont program that much in C#, but i guess it could look something like:



            double cisTuition = new double { 0.00, 1.00, 1.50, 2.00, 2.50 };
            Console.WriteLine("Please choose the semester");



            var UserInput = getUserInput();



            if(UserInput == NaN || UserInput > cisTuition.length)



            print( cisTuition[UserInput] )



            I know this is definitely not the correct syntax, but the logic should work.
            Hope it helps.






            share|improve this answer



















            • 1





              Please format your code block.

              – Tau
              Nov 15 '18 at 17:08
















            0














            Well first you need the users input, when you got this you can do your proper checks, like is it NaN, or if it's outside the arrays index. After this it should be easy for look out the index in the array and then print it. I dont program that much in C#, but i guess it could look something like:



            double cisTuition = new double { 0.00, 1.00, 1.50, 2.00, 2.50 };
            Console.WriteLine("Please choose the semester");



            var UserInput = getUserInput();



            if(UserInput == NaN || UserInput > cisTuition.length)



            print( cisTuition[UserInput] )



            I know this is definitely not the correct syntax, but the logic should work.
            Hope it helps.






            share|improve this answer



















            • 1





              Please format your code block.

              – Tau
              Nov 15 '18 at 17:08














            0












            0








            0







            Well first you need the users input, when you got this you can do your proper checks, like is it NaN, or if it's outside the arrays index. After this it should be easy for look out the index in the array and then print it. I dont program that much in C#, but i guess it could look something like:



            double cisTuition = new double { 0.00, 1.00, 1.50, 2.00, 2.50 };
            Console.WriteLine("Please choose the semester");



            var UserInput = getUserInput();



            if(UserInput == NaN || UserInput > cisTuition.length)



            print( cisTuition[UserInput] )



            I know this is definitely not the correct syntax, but the logic should work.
            Hope it helps.






            share|improve this answer













            Well first you need the users input, when you got this you can do your proper checks, like is it NaN, or if it's outside the arrays index. After this it should be easy for look out the index in the array and then print it. I dont program that much in C#, but i guess it could look something like:



            double cisTuition = new double { 0.00, 1.00, 1.50, 2.00, 2.50 };
            Console.WriteLine("Please choose the semester");



            var UserInput = getUserInput();



            if(UserInput == NaN || UserInput > cisTuition.length)



            print( cisTuition[UserInput] )



            I know this is definitely not the correct syntax, but the logic should work.
            Hope it helps.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 15 '18 at 16:29









            Daniel EDaniel E

            1




            1








            • 1





              Please format your code block.

              – Tau
              Nov 15 '18 at 17:08














            • 1





              Please format your code block.

              – Tau
              Nov 15 '18 at 17:08








            1




            1





            Please format your code block.

            – Tau
            Nov 15 '18 at 17:08





            Please format your code block.

            – Tau
            Nov 15 '18 at 17:08











            0














            I think what is being missed here is how to read in a value, convert it to an int if possible, and then use that as the reference to the array index.



            So, if that's the case, you'd want something akin to this added to the end of your code:



            string strUserInput = Console.ReadLine();
            int? iConverted = null;
            int.TryParse(strUserInput, out iConverted);
            if ((iConverted != null) && (iConverted <= (cisTuition.Length - 1)) )
            {
            Console.WriteLine(cisTuition[iConverted]);
            }
            else
            {
            Console.WriteLine("Invalid value or index of the array");
            }


            Full explanation:



            ReadLine() pulls in the value the user provides as the index to the array



            iConverted is instantiated (as null) to start, to allow an integer to also be null we have to use the ? as well



            We TryParse the strUserInput into an Integer, and dump it into iConverted if successful (if not, iConverted remains null)



            Finally, we print the requested index array, or notify the user that their index was out of bounds, or not properly parsed.






            share|improve this answer





















            • 1





              AFAIK iConverted would have to be an int? for it to be able to store null

              – Tau
              Nov 15 '18 at 17:02











            • Fair point @Tau - What I get for writing off the cuff. :)

              – gravitymixes
              Nov 15 '18 at 17:27
















            0














            I think what is being missed here is how to read in a value, convert it to an int if possible, and then use that as the reference to the array index.



            So, if that's the case, you'd want something akin to this added to the end of your code:



            string strUserInput = Console.ReadLine();
            int? iConverted = null;
            int.TryParse(strUserInput, out iConverted);
            if ((iConverted != null) && (iConverted <= (cisTuition.Length - 1)) )
            {
            Console.WriteLine(cisTuition[iConverted]);
            }
            else
            {
            Console.WriteLine("Invalid value or index of the array");
            }


            Full explanation:



            ReadLine() pulls in the value the user provides as the index to the array



            iConverted is instantiated (as null) to start, to allow an integer to also be null we have to use the ? as well



            We TryParse the strUserInput into an Integer, and dump it into iConverted if successful (if not, iConverted remains null)



            Finally, we print the requested index array, or notify the user that their index was out of bounds, or not properly parsed.






            share|improve this answer





















            • 1





              AFAIK iConverted would have to be an int? for it to be able to store null

              – Tau
              Nov 15 '18 at 17:02











            • Fair point @Tau - What I get for writing off the cuff. :)

              – gravitymixes
              Nov 15 '18 at 17:27














            0












            0








            0







            I think what is being missed here is how to read in a value, convert it to an int if possible, and then use that as the reference to the array index.



            So, if that's the case, you'd want something akin to this added to the end of your code:



            string strUserInput = Console.ReadLine();
            int? iConverted = null;
            int.TryParse(strUserInput, out iConverted);
            if ((iConverted != null) && (iConverted <= (cisTuition.Length - 1)) )
            {
            Console.WriteLine(cisTuition[iConverted]);
            }
            else
            {
            Console.WriteLine("Invalid value or index of the array");
            }


            Full explanation:



            ReadLine() pulls in the value the user provides as the index to the array



            iConverted is instantiated (as null) to start, to allow an integer to also be null we have to use the ? as well



            We TryParse the strUserInput into an Integer, and dump it into iConverted if successful (if not, iConverted remains null)



            Finally, we print the requested index array, or notify the user that their index was out of bounds, or not properly parsed.






            share|improve this answer















            I think what is being missed here is how to read in a value, convert it to an int if possible, and then use that as the reference to the array index.



            So, if that's the case, you'd want something akin to this added to the end of your code:



            string strUserInput = Console.ReadLine();
            int? iConverted = null;
            int.TryParse(strUserInput, out iConverted);
            if ((iConverted != null) && (iConverted <= (cisTuition.Length - 1)) )
            {
            Console.WriteLine(cisTuition[iConverted]);
            }
            else
            {
            Console.WriteLine("Invalid value or index of the array");
            }


            Full explanation:



            ReadLine() pulls in the value the user provides as the index to the array



            iConverted is instantiated (as null) to start, to allow an integer to also be null we have to use the ? as well



            We TryParse the strUserInput into an Integer, and dump it into iConverted if successful (if not, iConverted remains null)



            Finally, we print the requested index array, or notify the user that their index was out of bounds, or not properly parsed.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 15 '18 at 17:34

























            answered Nov 15 '18 at 16:31









            gravitymixesgravitymixes

            1,64911425




            1,64911425








            • 1





              AFAIK iConverted would have to be an int? for it to be able to store null

              – Tau
              Nov 15 '18 at 17:02











            • Fair point @Tau - What I get for writing off the cuff. :)

              – gravitymixes
              Nov 15 '18 at 17:27














            • 1





              AFAIK iConverted would have to be an int? for it to be able to store null

              – Tau
              Nov 15 '18 at 17:02











            • Fair point @Tau - What I get for writing off the cuff. :)

              – gravitymixes
              Nov 15 '18 at 17:27








            1




            1





            AFAIK iConverted would have to be an int? for it to be able to store null

            – Tau
            Nov 15 '18 at 17:02





            AFAIK iConverted would have to be an int? for it to be able to store null

            – Tau
            Nov 15 '18 at 17:02













            Fair point @Tau - What I get for writing off the cuff. :)

            – gravitymixes
            Nov 15 '18 at 17:27





            Fair point @Tau - What I get for writing off the cuff. :)

            – gravitymixes
            Nov 15 '18 at 17:27


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323768%2fc-sharp-console-get-array-value-with-index-from-user-input%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python