Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Error in my Calculator in unity

Discussion in '2D' started by Netherknight666, Jan 18, 2020.

  1. Netherknight666

    Netherknight666

    Joined:
    Jun 18, 2019
    Posts:
    4
    I trying to create calculator here, however it can only calculate 1 digit number. When I try 2 digits number it's calculate wrong. For example, I tried 11+1 but the answer is 2.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using UnityEngine.EventSystems;
    7.  
    8. public class Calculator : MonoBehaviour
    9. {
    10.     [SerializeField]
    11.     Text inputField;
    12.  
    13.     string inputString;
    14.     int[] number = new int[2];
    15.     string operatorSymbol;
    16.     int i = 0;
    17.     int result;
    18.     bool displayerResults = false;
    19.     public void ButtonPressed()
    20.     {
    21.         if(displayerResults == true)
    22.         {
    23.             inputField.text = "";
    24.             inputString = "";
    25.             displayerResults = false;
    26.         }
    27.         string buttonValue = EventSystem.current.currentSelectedGameObject.name;
    28.         inputString += buttonValue;
    29.  
    30.         int arg;
    31.         if (int.TryParse(buttonValue, out arg))
    32.         {
    33.             if (i > 1) i = 0;
    34.             number[i] = arg;
    35.             i = i + 1;
    36.         }
    37.         else
    38.         {
    39.             switch (buttonValue)
    40.             {
    41.             case "+":
    42.                 operatorSymbol = buttonValue;
    43.                 break;
    44.             case "-":
    45.                 operatorSymbol = buttonValue;
    46.                 break;
    47.             case "x":
    48.                 operatorSymbol = buttonValue;
    49.                 break;
    50.             case "÷":
    51.                 operatorSymbol = buttonValue;
    52.                 break;
    53.             case "=":
    54.                 switch (operatorSymbol)
    55.                 {
    56.                 case "+":
    57.                     result = number[0] + number[1];
    58.                     break;
    59.                 case "-":
    60.                     result = number[0] - number[1];
    61.                     break;
    62.                 case "x":
    63.                     result = number[0] * number[1];
    64.                     break;
    65.                 case "÷":
    66.                     result = number[0] / number[1];
    67.                     break;
    68.                 }
    69.                 displayerResults = true;
    70.                 inputString = result.ToString();
    71.                 number = new int[2];
    72.                 break;
    73.             }
    74.         }
    75.         inputField.text = inputString;
    76.     }
    77. }
    78.  
    79. }
     

    Attached Files:

    Last edited: Jan 18, 2020
  2. Agent003

    Agent003

    Joined:
    Sep 7, 2018
    Posts:
    55
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.EventSystems;
    6.  
    7. public class Calculator : MonoBehaviour
    8. {
    9.     [SerializeField]
    10.     Text inputField;
    11.  
    12.     string inputString;
    13.     int[] number = new int[2];
    14.     string operatorSymbol;
    15.     int i = 0;
    16.     int result;
    17.     bool displayerResults = false;
    18.     public void ButtonPressed()
    19.     {
    20.         if(displayerResults == true)
    21.         {
    22.             inputField.text = "";
    23.             inputString = "";
    24.             displayerResults = false;
    25.         }
    26.         string buttonValue = EventSystem.current.currentSelectedGameObject.name;
    27.         inputString += buttonValue;
    28.  
    29.         int arg;
    30.         if (int.TryParse(buttonValue, out arg))
    31.         {
    32.             if (i > 1) i = 0;
    33.             number = arg;
    34.             i = i + 1;
    35.         }
    36.         else
    37.         {
    38.             switch (buttonValue)
    39.             {
    40.             case "+":
    41.                 operatorSymbol = buttonValue;
    42.                 break;
    43.             case "-":
    44.                 operatorSymbol = buttonValue;
    45.                 break;
    46.             case "x":
    47.                 operatorSymbol = buttonValue;
    48.                 break;
    49.             case "÷":
    50.                 operatorSymbol = buttonValue;
    51.                 break;
    52.             case "=":
    53.                 switch (operatorSymbol)
    54.                 {
    55.                 case "+":
    56.                     result = number[0] + number[1];
    57.                     break;
    58.                 case "-":
    59.                     result = number[0] - number[1];
    60.                     break;
    61.                 case "x":
    62.                     result = number[0] * number[1];
    63.                     break;
    64.                 case "÷":
    65.                     result = number[0] / number[1];
    66.                     break;
    67.                 }
    68.                 displayerResults = true;
    69.                 inputString = result.ToString();
    70.                 number = new int[2];
    71.                 break;
    72.             }
    73.         }
    74.         inputField.text = inputString;
    75.     }
    76. }
     
  3. Netherknight666

    Netherknight666

    Joined:
    Jun 18, 2019
    Posts:
    4
    Oh it's still doesn't work, all I want is to make 10+1= 11. and other more than 2 digits number working correctly in what it should be.
     
  4. Agent003

    Agent003

    Joined:
    Sep 7, 2018
    Posts:
    55
    Actually i didn't do any thing, but i made the first reply to correct the formatting of the code so i can read it and help you.
    your problem is that the code does not allow you to store two digits integer, why????
    Because every time you press on a digit you immediately store it in the array (in line 32 to line 34), so if you want to type 11, will press 1 and your code will store that 1 in the first index in the array, then you press 1 again and the code will store it in second index of the array.
    that will end up having an array like this:

    Code (CSharp):
    1. number[0] = 1;
    2. number[1] = 1;
    that's why you get answer equals to 2.

    To solve this, you should only store the integer after you press on operator symbol.
    declare the number array as public
    public int[] number;
    so you can see it in the inspector.
     
    Last edited: Jan 18, 2020
  5. Netherknight666

    Netherknight666

    Joined:
    Jun 18, 2019
    Posts:
    4
    Now everytime I press it is equal to zero so which correct line I should declare the integer (line from my old code)

    below my new code with new error.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.EventSystems;
    6. public class Calculator : MonoBehaviour
    7. {
    8.     [SerializeField]
    9.     Text inputField;
    10.     string inputString;
    11.     int[] number = new int[2];
    12.     string operatorSymbol;
    13.     int i = 0;
    14.     int result;
    15.     bool displayerResults = false;
    16.     public void ButtonPressed()
    17.     {
    18.         if(displayerResults == true)
    19.         {
    20.             inputField.text = "";
    21.             inputString = "";
    22.             displayerResults = false;
    23.         }
    24.         string buttonValue = EventSystem.current.currentSelectedGameObject.name;
    25.         inputString += buttonValue;
    26.         int arg;
    27.         if (int.TryParse(buttonValue, out arg))
    28.         {
    29.            
    30.         }
    31.         else
    32.         {
    33.             switch (buttonValue)
    34.             {
    35.             case "+":
    36.                 operatorSymbol = buttonValue;
    37.                     if (i > 1) i = 0;
    38.                     number[i] = arg;
    39.                     i = i + 1;
    40.                     break;
    41.             case "-":
    42.                 operatorSymbol = buttonValue;
    43.                     if (i > 1) i = 0;
    44.                     number[i] = arg;
    45.                     i = i + 1;
    46.                     break;
    47.             case "x":
    48.                 operatorSymbol = buttonValue;
    49.                     if (i > 1) i = 0;
    50.                     number[i] = arg;
    51.                     i = i + 1;
    52.                     break;
    53.             case "÷":
    54.                 operatorSymbol = buttonValue;
    55.                     if (i > 1) i = 0;
    56.                     number[i] = arg;
    57.                     i = i + 1;
    58.                     break;
    59.             case "=":
    60.                     switch (operatorSymbol)
    61.                 {
    62.                 case "+":
    63.                     result = number[0] + number[1];
    64.                     break;
    65.                 case "-":
    66.                     result = number[0] - number[1];
    67.                     break;
    68.                 case "x":
    69.                     result = number[0] * number[1];
    70.                     break;
    71.                 case "÷":
    72.                     result = number[0] / number[1];
    73.                     break;
    74.                 }
    75.                 displayerResults = true;
    76.                 inputString = result.ToString();
    77.                 number = new int[2];
    78.                 break;
    79.             }
    80.         }
    81.         inputField.text = inputString;
    82.     }
    83. }
    84.  
    Sorry I am new here I don't know much, please help me