0

i am trying to get a working Fibonacci calculator but i am having some issues. as far as i can tell my code is working well, although it crashes when i input a word. i'm unsure how i can get this to work for me so it will only accept numbers. Thanks in advance :D

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }


    private void Button_Click1(object sender, RoutedEventArgs e)
    {

        //Fibonacci

        string output = String.Empty;



        double result;
        double z = 0;
        double x = 1;
        double y = double.Parse(FibonacciAsText.Text);
        if (double.TryParse(FibonacciAsText.Text, out result))
        {
            if (y == 1)
                output = 1.ToString();
        }
        else if (y == 0)
        {
            output = 0.ToString();
        }
        for (double w = 0; w < y - 1; w++)
        {
            result = z;
            z = x;
            x = result + x;
            output = x.ToString();
        }
        Fibonacci.Text = output;
    }
  • `double.Parse(FibonacciAsText.Text)` cannot parse word – Sergey Berezovskiy Jun 15 '17 at 15:42
  • 1
    `double y = double.Parse(FibonacciAsText.Text);` is the problem. Use tryparse here too. – Chetan Jun 15 '17 at 15:43
  • @ChetanRanpariya when i do that i get the following error "No overload for method 'Tryparse' takes 1 arguments" –  Jun 15 '17 at 15:47
  • See the answer by Cory. Let us know if that doesn't work. – Chetan Jun 15 '17 at 15:52
  • See this for wpf : https://stackoverflow.com/questions/1268552/how-do-i-get-a-textbox-to-only-accept-numeric-input-in-wpf or this for winforms : https://stackoverflow.com/questions/8590501/restricting-users-to-input-only-numbers-in-c-sharp-windows-application – PaulF Jun 15 '17 at 16:07

2 Answers2

0

This will exit the method and not allow the code to finish:

double y;

if (!double.TryParse(FibonacciAsText.Text, out y))
{
    Fibonacci.Text = "N/A";
    return;
}

Basically, it's checking to see if you have a valid number. If so, it will set that as y (similar to Parse). If it doesn't succeed though, it will exit the method at "return".

Cory
  • 1,794
  • 12
  • 21
  • although now if i input a word after a past number it will still output the past number –  Jun 15 '17 at 15:58
  • Well, it depends on how your code reads. I can't see the updated code. It shouldn't though as long as the "word" is in the FibonacciAsText textbox – Cory Jun 15 '17 at 15:59
  • `private void Button_Click1(object sender, RoutedEventArgs e) { //Fibonacci string output = String.Empty; double result; double z = 0; double x = 1; double y; if (!double.TryParse(FibonacciAsText.Text, out y)) return; if (y == 1) output = 1.ToString();` –  Jun 15 '17 at 16:02
  • http://imgur.com/RZMFUHU i put in 9 it gave me the output tried fish and it still shows the past output –  Jun 15 '17 at 16:06
  • Ah, it's because you aren't clearing the past output. After "return" it's not doing anything. Including clearing that textbox. If you want to clear the output, you need to explicitly tell it to. See my update. – Cory Jun 15 '17 at 16:08
  • That's much better thanks so much :D ty for the help –  Jun 15 '17 at 16:11
0

You need validate the input first.If you are using a textbox..there should be a setting to accept only numbers.
Or
In the backend..you can check if the input is a number by using the code below

int n;
bool isNumeric = int.TryParse(FibonacciAsText.Text, out n);

if(isNumeric)
{
Your calculator code
}
raj'sCubicle
  • 350
  • 2
  • 13