473,406 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

how to fix stack over flow Errors?

hi. a blind it student using the jaws for windows screen reader from http://www.freedomscientific.com and doing certificate iv programming from http://www.upskilled.edu.au. doing a simple calculator. and have run into a stack over flow. have tried looking on the net, googled, and did read about recursive functions, and also stack over flow, but could only find examples, and fixes for c # console, or windows forms, nothing for universal apps or xaml. so can any one help me out, point me to where i can find the answers, and just fix this problem. this is the only problem. and points the exception to line 266. can any one help me. any one in australia, or south east asia, as similar or same time zone. marvin.
Ps:: pasting below.

View Detail
System.StackOverflowException {The runtime refused to evaluate the expression at th
System.StackOverflowException was unhandled
HResult=-2147023895
InnerException:

Expand|Select|Wrap|Line Numbers
  1. <Page
  2.     x:Class="Calculator.MainPage"
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5.     xmlns:local="using:Calculator"
  6.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8.     mc:Ignorable="d">
  9.   <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
  10.     <TextBlock HorizontalAlignment="Left" Margin="120,50,0,0" TextWrapping="Wrap" Text="Simple Calculator"
  11.  VerticalAlignment="Top"/>
  12.     <TextBox x:Name="Result" IsReadOnly="true" Margin="10,75,0,0"
  13. TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="340"/>
  14.     <Button Content="1" HorizontalAlignment="Left" VerticalAlignment="Top"
  15. Margin="78,189,0,0" Width="42" x:Name="btn1" Click="btn1_Click"/>
  16.     <Button Content="2" HorizontalAlignment="Left" VerticalAlignment="Top"
  17. Margin="125,189,0,0" Width="42" x:Name="btn2" Click="btn2_Click"/>
  18.     <Button Content="3" HorizontalAlignment="Left" VerticalAlignment="Top"
  19. Margin="172,189,0,0" Width="42" x:Name="btn3" Click="btn3_Click"/>
  20.     <Button Content="4" HorizontalAlignment="Left" VerticalAlignment="Top"
  21. Margin="78,152,0,0" Width="42" x:Name="btn4" Click="btn4_Click"/>
  22.     <Button Content="5" HorizontalAlignment="Left" VerticalAlignment="Top"
  23. Margin="125,152,0,0" Width="42" x:Name="btn5" Click="btn5_Click"/>
  24.     <Button Content="6" HorizontalAlignment="Left" VerticalAlignment="Top"
  25. Margin="172,152,0,0" Width="42" x:Name="btn6" Click="btn6_Click"/>
  26.     <Button Content="7" HorizontalAlignment="Left" VerticalAlignment="Top"
  27. Margin="78,115,0,0" Width="42" x:Name="btn7" Click="btn7_Click"/>
  28.     <Button Content="8" HorizontalAlignment="Left" VerticalAlignment="Top"
  29. Margin="125,115,0,0" Width="42" x:Name="btn8" Click="btn8_Click"/>
  30.     <Button Content="9" HorizontalAlignment="Left" VerticalAlignment="Top"
  31. Margin="172,115,0,0" Width="42" x:Name="btn9" Click="btn9_Click"/>
  32.     <Button Content="0" HorizontalAlignment="Left" VerticalAlignment="Top"
  33. Margin="125,226,0,0" Width="42" x:Name="btn0" Click="btn0_Click"/>
  34.     <Button Content="+" HorizontalAlignment="Left" VerticalAlignment="Top"
  35. Margin="219,226,0,0" Width="42" x:Name="btnPlus" Click="btnPlus_Click"/>
  36.     <Button Content="-" HorizontalAlignment="Left" VerticalAlignment="Top"
  37. Margin="219,189,0,0" Width="42" x:Name="btnMinus" Click="btnMinus_Click"/>
  38.     <Button Content="*" HorizontalAlignment="Left" VerticalAlignment="Top"
  39. Margin="219,152,0,0" Width="42" x:Name="btnTimes" Click="btnTimes_Click"/>
  40.     <Button Content="/" HorizontalAlignment="Left" VerticalAlignment="Top"
  41. Margin="219,115,0,0" Width="42" x:Name="btnDiv" Click="btnDiv_Click"/>
  42.     <Button Content="=" HorizontalAlignment="Left" VerticalAlignment="Top"
  43. Margin="78,226,0,0" Width="42" x:Name="btnEqual" Click="btnEqual_Click"/>
  44.     <Button Content="C" HorizontalAlignment="Left" VerticalAlignment="Top"
  45. Margin="172,226,0,0" Width="42" x:Name="btnClear" Click="btnClear_Click"/>
  46.   </Grid>
  47. </Page>
  48. using System;
  49. using System.Collections.Generic;
  50. using System.IO;
  51. using System.Linq;
  52. using System.Runtime.InteropServices.WindowsRuntime;
  53. using Windows.Foundation;
  54. using Windows.Foundation.Collections;
  55. using Windows.UI.Xaml;
  56. using Windows.UI.Xaml.Controls;
  57. using Windows.UI.Xaml.Controls.Primitives;
  58. using Windows.UI.Xaml.Data;
  59. using Windows.UI.Xaml.Input;
  60. using Windows.UI.Xaml.Media;
  61. using Windows.UI.Xaml.Navigation;
  62.  
  63. // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
  64.  
  65. namespace Calculator
  66. {
  67.   /// <summary>
  68.   /// An empty page that can be used on its own or navigated to within a Frame.
  69.   /// </summary>
  70.   public sealed partial class MainPage : Page
  71.   {
  72.     public MainPage()
  73.     {
  74.       this.InitializeComponent();
  75.  
  76.       Result.Text = 0.ToString();
  77.     }
  78.  
  79.     // Function to add numbers and check for a empty char and empty string.
  80.  
  81.     private void AddNumberToResult(double number)
  82.     {
  83.       if (char.IsNumber(Result.Text.Last()))
  84.       {
  85.         if (Result.Text.Length == 1 && Result.Text == "0")
  86.         {
  87.           Result.Text = string.Empty;
  88.         }
  89.         Result.Text += number;
  90.       }
  91.       else
  92.       {
  93.         if (number != 0)
  94.         {
  95.           Result.Text += number;
  96.         }
  97.       }
  98.     }
  99.  
  100.     // Enum to add the different mathematical operations for this application.
  101.  
  102.     enum Operation { MINUS = 1, PLUS = 2, DIV = 3, TIMES = 4, NUMBER = 5 }
  103.  
  104.     // Function to add the operation and the Result to this application.
  105.     // Tadd a substring to the Result of the text in this application.
  106.  
  107.     private void AddOperationToResult(Operation operation)
  108.     {
  109.       if (Result.Text.Length == 1 && Result.Text == "0")
  110.         return;
  111.  
  112.       if (!char.IsNumber(Result.Text.Last()))
  113.       {
  114.         Result.Text = Result.Text.Substring(0, Result.Text.Length - 1);
  115.       }
  116.  
  117.       // Switch statement to choose the different  mathematical operations for this application.
  118.  
  119.       switch (operation)
  120.       {
  121.         case Operation.MINUS:
  122.           Result.Text += "-";
  123.           break;
  124.         case Operation.PLUS:
  125.           Result.Text += "+";
  126.           break;
  127.         case Operation.DIV:
  128.           Result.Text += "/";
  129.           break;
  130.         case Operation.TIMES:
  131.           Result.Text += "*";
  132.           break;
  133.       }
  134.     }
  135.  
  136.     // Add all the number functions to the text box and the mathematical operations.
  137.     // Then assign the Result to using the mathematical operators, to display the Result in the text box.
  138.     // Call the Add Number To Result function in the Number click events for this application.
  139.  
  140.     private void btn1_Click(object sender, RoutedEventArgs e)
  141.     {
  142.       AddNumberToResult(1);
  143.     }
  144.  
  145.     private void btn2_Click(object sender, RoutedEventArgs e)
  146.     {
  147.       AddNumberToResult(2);
  148.     }
  149.  
  150.     private void btn3_Click(object sender, RoutedEventArgs e)
  151.     {
  152.       AddNumberToResult(3);
  153.     }
  154.  
  155.     private void btn4_Click(object sender, RoutedEventArgs e)
  156.     {
  157.       AddNumberToResult(4);
  158.     }
  159.  
  160.     private void btn5_Click(object sender, RoutedEventArgs e)
  161.     {
  162.       AddNumberToResult(5);
  163.     }
  164.  
  165.     private void btn6_Click(object sender, RoutedEventArgs e)
  166.     {
  167.       AddNumberToResult(6);
  168.     }
  169.  
  170.     private void btn7_Click(object sender, RoutedEventArgs e)
  171.     {
  172.       AddNumberToResult(7);
  173.     }
  174.  
  175.     private void btn8_Click(object sender, RoutedEventArgs e)
  176.     {
  177.       AddNumberToResult(8);
  178.     }
  179.  
  180.     private void btn9_Click(object sender, RoutedEventArgs e)
  181.     {
  182.       AddNumberToResult(9);
  183.     }
  184.  
  185.     private void btn0_Click(object sender, RoutedEventArgs e)
  186.     {
  187.       AddNumberToResult(0);
  188.     }
  189.  
  190.     private void btnPlus_Click(object sender, RoutedEventArgs e)
  191.     {
  192.       AddOperationToResult(Operation.PLUS);
  193.     }
  194.  
  195.     private void btnMinus_Click(object sender, RoutedEventArgs e)
  196.     {
  197.       AddOperationToResult(Operation.MINUS);
  198.     }
  199.  
  200.     private void btnTimes_Click(object sender, RoutedEventArgs e)
  201.     {
  202.       AddOperationToResult(Operation.TIMES);
  203.     }
  204.  
  205.     private void btnDiv_Click(object sender, RoutedEventArgs e)
  206.     {
  207.       AddOperationToResult(Operation.DIV);
  208.     }
  209.  
  210.     // Function to use the Equals operators and mathematical operators for this application.
  211.  
  212.     #region Equal
  213.     private class Operand
  214.     {
  215.       public Operation operation = Operation.NUMBER; // Default constructor
  216.       public double value = 0;
  217.  
  218.       public Operand left = null;
  219.       public Operand right = null;
  220.     }
  221.  
  222.     //Get an expression from Result.Text and build  a   
  223.  // tree function.
  224.  
  225.       private Operand BuildTreeOperand()
  226.     {
  227.       Operand tree = null;
  228.  
  229.       string expression = Result.Text;
  230.       if (!char.IsNumber(expression.Last()))
  231.       {
  232.         expression = expression.Substring(0, expression.Length - 1);
  233.       }
  234.  
  235.       string numberStr = string.Empty;
  236.       foreach (char c in expression.ToCharArray())
  237.       {
  238.         if (char.IsNumber(c) || c == '.' || numberStr == string.Empty && c ==
  239.           '-')
  240.         {
  241.           numberStr += c;
  242.         }
  243.         else
  244.         {
  245.           AddOperandToTree(ref tree, new Operand()
  246.           {
  247.             value = double.Parse(numberStr)
  248.           });
  249.           numberStr = string.Empty;
  250.  
  251.           // Switch statement to show the different calculator operations for this application.
  252.  
  253.                         Operation op = Operation.MINUS; // Default condition.
  254.  
  255.             switch (c)
  256.           {
  257.             case '-':
  258.               op = Operation.MINUS;
  259.               break;
  260.             case '+':
  261.               op = Operation.PLUS;
  262.               break;
  263.             case '/':
  264.               op = Operation.DIV;
  265.               break;
  266.             case '*':
  267.               op = Operation.TIMES;
  268.               break;
  269.           }
  270.           AddOperandToTree(ref tree, new Operand()
  271.           {
  272.             operation = op
  273.           });
  274.         }
  275.       }
  276.  
  277.       // Last number.
  278.  
  279.  
  280.         AddOperandToTree(ref tree, new Operand()
  281.         {
  282.           value = double.Parse(numberStr)
  283.         });
  284.  
  285.       return tree;
  286.     }
  287.  
  288.     // This function will add the Operand tree and all associated objects for this application.
  289.  
  290.     private void AddOperandToTree(ref Operand tree, Operand elem)
  291.     {
  292.       if (tree == null)
  293.       {
  294.         tree = elem;
  295.       }
  296.       else
  297.       {
  298.         if (elem.operation < tree.operation)
  299.         {
  300.           Operand auxTree = tree;
  301.           tree = elem;
  302.           elem.left = tree;
  303.         }
  304.         else
  305.         {
  306.           AddOperandToTree(ref tree.right, elem); // Recursive function for the Add Operand To Tree structure.//
  307.                     }
  308.       }
  309.     }
  310.  
  311.     // Function to calculate the Operand tree and all objects for this application.
  312.  
  313.     private double Calc(Operand tree)
  314.     {
  315.       if (tree.left == null && tree.right == null) // Selects the number.
  316.           {
  317.         return tree.value;
  318.       }
  319.       else// Select an operation.
  320.  {
  321.         double subResult = 0;
  322.         // Switch statement  to choose the different operations for this application.
  323.           switch (tree.operation)
  324.         {
  325.           case Operation.MINUS:
  326.             subResult = Calc(tree.left) - Calc(tree.right);
  327.             break; // Recursive function.
  328.               case Operation.PLUS:
  329.             subResult = Calc(tree.left) + Calc(tree.right);
  330.             break;
  331.           case Operation.DIV:
  332.             subResult = Calc(tree.left) / Calc(tree.right);
  333.             break;
  334.           case Operation.TIMES:
  335.             subResult = Calc(tree.left) * Calc(tree.right);
  336.             break;
  337.         }
  338.         return subResult;
  339.       }
  340.     }
  341.  
  342.     // Function to have the Equals button, set empty string and then set up the calculations and the Results in the text box.
  343.  
  344. private void btnEqual_Click(object sender, RoutedEventArgs e)
  345.     {
  346.       if (string.IsNullOrEmpty(Result.Text))
  347.         return;
  348.  
  349.       // Build a new string tree from the Result.Text TextBox.
  350.  
  351.         Operand tree = BuildTreeOperand();
  352.  
  353.       // Evaluate tree  to calculate final Result from the TextBox.
  354.  
  355.       double value = Calc(tree);
  356.  
  357.       Result.Text = value.ToString();
  358.     }
  359.  
  360.     // Function to clear the text box and reset all values to 0 or null.
  361.  
  362.     #endregion Equal
  363.  
  364.     private void btnClear_Click(object sender, RoutedEventArgs e)
  365.     {
  366.       Result.Text = 0.ToString();
  367.     }
  368.   }
  369. }
Dec 26 '15 #1
1 2921
Luuk
1,047 Expert 1GB
Without looking at your code, you should know what a 'stackoverflowexception' is.

A simple example can be found here:
http://www.dotnetperls.com/stackoverflowexception

Now, ( you know your code !) do you anything like that in your code?
Jan 1 '16 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Aaron W. LaFramboise | last post by:
I'm seeking a solution to my C++-life long dilemma of how to deal with errors when exceptions aren't appropriate. Below I highlight two error cases, which mainly occur when trying to handle...
19
by: Jim | last post by:
I have spent the past few weeks designing a database for my company. The problem is I have started running into what I believe are stack overflow problems. There are two tab controls on the form...
5
by: Miyra | last post by:
Hi. I'm working with an app that uses exceptions for control flow. These are code blocks where exceptions are thrown/caught regularly. A couple hundred exceptions occur per hour and they're caught...
2
by: Brett | last post by:
Hi, I'm compiling an old project under the 'new' visual studio 7.1.3088. I changed the line: #include <iostream.h> to #include <iostream> and now I get a stack of errors, some of which...
2
by: Ali | last post by:
Hi, I got stack overflow errors while using an unmanaged dll from managed c# application. When i use editbin.exe to increase stack size of app , it works. Is there a way to increase stack size of...
3
by: clintonb | last post by:
Some programmers, and even Microsoft documents, say you should only throw exceptions for exceptional situations. So how are others handling all the other unexceptional errors? How are you...
5
by: superjacent | last post by:
I'm getting 'Out of Stack Space' error messages. The process involves two forms. Form A opens Form B in dialog mode and passes through OpenArgs. Form B contains a 'Tab' control having two...
7
by: DevNull | last post by:
Hello everyone, I decided to pick c++ back up after not really having used it much in 10 years. Just as a point of refference, there was no standard C++ last time I used regularly. Anyways...
4
by: szimek | last post by:
Hi, I've already posted an email with this problem, but this time I think I got a bit more info. The app I'm currently working on works like this: when user clicks on a clickable element, it...
8
by: Ruben | last post by:
Hello I'm struggling with the throw and catch exception system. As I understood it, when you throw an error, that the stack is searched for a matching catch, but I'm having difficult finding...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.