none
C# fibonacci with input validation RRS feed

  • Question

  • Hello, 

    I am new to C# and am having hard time figuring out how to fix my code below to not have to ask for input twice if the first user input is in fact an integer. Do I need to declare some kind of static first in order to call it again from within the else statement? Should I use the threading class instead of what I have here to be able to pass user input to multiple threads? Thank you for any advice.


    //C#  Program to Generate Fibonacci Series

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace fibonaci
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.Write("Enter the number of elements: ");
                string input = Console.ReadLine();
                int num = -1;
                if (!int.TryParse(input, out num))
                {
                    Console.WriteLine("Not an integer");
                }
                else
                Console.Write("Enter the number of elements: ");
                int n1 = 0, n2 = 1, n3, i, number;
                number = int.Parse(Console.ReadLine());
                Console.Write(n1 + " " + n2 + " "); //printing 0 and 1
                for (i = 2; i < number; ++i) //loop starts from 2 because 0 and 1 are already printed
                {
                    n3 = n1 + n2;
                    Console.Write(n3 + " ");
                    n1 = n2;
                    n2 = n3;
                }
                Console.ReadLine();

            }
        }
    }

    Sunday, July 21, 2019 1:00 PM

All replies


  • I am new to C# and am having hard time figuring out how to fix my code below to not have to ask for input twice if the first user input is in fact an integer. 

    There are numerous ways one can repeat a request for input. Consider this
    example of one approach.

    static void Main(string[] args)
    {
        int num = -1;
        string input;            
        while(true)
        {
            Console.Write("Enter the number of elements: ");
            input = Console.ReadLine();
            if (!int.TryParse(input, out num))
            {
                Console.WriteLine("Not an integer");
            }
            else break;
        }
    
        Console.WriteLine("You entered {0}", num);
        Console.ReadLine();
    }
    

    - Wayne

    Sunday, July 21, 2019 3:25 PM
  • Hi jgbka,

    Thank you for posting here.

    For your question, you want to fix the code you provided so that user doesn't have to input twice if the value is an integer.

    I modified the code, you could have a look.

     Console.Write("Enter the number of elements: ");
                string input = Console.ReadLine();
                int num = -1;
                if (!int.TryParse(input, out num))
                {
                    Console.WriteLine("Not an integer");
                }
                else
                {
                    int n1 = 0, n2 = 1, n3, i, number;
                    number = int.Parse(input);
                    Console.Write(n1 + " " + n2 + " "); //printing 0 and 1
                    for (i = 2; i < number; ++i) //loop starts from 2 because 0 and 1 are already printed
                    {
                        n3 = n1 + n2;
                        Console.Write(n3 + " ");
                        n1 = n2;
                        n2 = n3;
                    }
                }
    
                Console.ReadLine();

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 22, 2019 2:04 AM
    Moderator