1

I'm pretty new to coding -- C# is the first language I've attempted to learn -- and I'm having trouble with one of my codes. I'm using Visual Studio, and the gist of this code is that I'm trying to pull numbers from three text boxes, pass them to a method that returns the largest number, and then display the result in another text box. I've trawled through other threads on this site looking for a solution, but with no luck.

Visual Studio doesn't show me any errors in my code, and the program executes fine. But when I enter the three numbers, I'm not able to get the largest to display in the answer box. I don't think it's an issue with getting the numbers from the boxes, as I can print those numbers individually if I choose to. However, I also think I've written the method correctly (think being the operative word here).

Side note: I know there's a Math.Max() method I can use instead of the if/else statements, but I'm trying to understand the basics before I start using built-in methods like that.

private double max(double firstNum, double secNum, double thirdNum)
{
    double maxNum = 0;
    if (firstNum > secNum && firstNum > thirdNum)
    {
        maxNum = firstNum;
    }
    else if (secNum > firstNum && secNum > thirdNum)
    {
        maxNum = secNum;
    }
   else 
    {
        maxNum = thirdNum;
    }

    return maxNum;
}

private void retBtn_Click(object sender, EventArgs e)
{
    double num1, num2, num3;
    num1 = double.Parse(num1Box.Text);
    num2 = double.Parse(num2Box.Text);
    num3 = double.Parse(num3Box.Text);
    double biggestNum = max(num1, num2, num3);
    ansBox.Text = biggestNum.ToString();
}

Here's the code I currently have. Any help would be greatly appreciated!

EDIT: The other thread that has been linked does not answer the same question I'm asking. It shows how to write the method that finds the biggest number, which I have already done. The issue I'm having is that when I press the retBtn, nothing appears in the textbox. I think the issue is in my the "double biggestNum = max(num1, num2, num3)" or in the return statement of my method.

Caroline
  • 43
  • 7
  • Possible duplicate of [In c# is there a method to find the max of 3 numbers?](https://stackoverflow.com/questions/6800838/in-c-sharp-is-there-a-method-to-find-the-max-of-3-numbers) – Sinatr Jun 30 '17 at 13:33
  • Does anything display in the textbox? – PaulF Jun 30 '17 at 13:34
  • what is the result you are getting? – Fabiano Jun 30 '17 at 13:34
  • So if you replace with ansBox.Text = num1.ToString() it works? – ccalboni Jun 30 '17 at 13:35
  • 4
    A good time to learn how the debugger can step through the code and allow you to look at what's happening. Start the code using F11 and fill in the textboxes. When you click the button, you will be returned to the code and can step through it using F11. This will allow you to see how your code is executing against what you've entered and you can hover over variables to see what their actual value is at that time. – Charles May Jun 30 '17 at 13:36
  • 2
    On a side note, try to step through in your head the following input: `firstNum = 1; secondNum = 1; thirdNum = 0`. Are you getting the expected result? – InBetween Jun 30 '17 at 13:37
  • It looks like you need a [rubber_duck](https://en.wikipedia.org/wiki/Rubber_duck_debugging) – ΦXocę 웃 Пepeúpa ツ Jun 30 '17 at 13:38
  • What do you get as output? – Aleksa Ristic Jun 30 '17 at 13:44
  • I'm curious much more than I have to be – ccalboni Jun 30 '17 at 13:47
  • One other thing that could be happening is that you don't have your button set to use that method for its Click event. Click on the button and click the lightening bolt in the properties window. Ensure that Click has retBtn_Click selected as the method to run for that event. – Charles May Jun 30 '17 at 13:49
  • The logic of the max function needs improvement.. Try a few test cases, you'll see where it fails. Keep learning! – Aditya Banerjee Jun 30 '17 at 13:59
  • I know you're learning, but another option is `new [] { firstNum, secNum, thirdNum }.Max()`. – Enigmativity Jul 03 '17 at 01:49

3 Answers3

0

Your method is defined as

max(double A, double B, double C)

but you are not covering the whole combinations... lets break it down with this counter-example:

according to your logic give this input:

double biggestNum = max(10, 10, 1);

then

 //this condition will fail since is a short circuit and firstNum > secNum is false
 if (firstNum > secNum && firstNum > thirdNum)
{
    maxNum = firstNum;
}
//this condition will fail too since for the same reason
else if (secNum > firstNum && secNum > thirdNum)
{
    maxNum = secNum;
}
//you are inferring then that C=1 is the biggest.. BOOM!!!
else 
{
    maxNum = thirdNum;
}

do take a pen and a paper a cover all the possible combinations before you jsut skip and discard the max candidates....

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • The question is about getting/showing data, the logic behind it is meaningless in this case – ccalboni Jun 30 '17 at 13:52
  • how do you understand the phrase: ***I'm not able to get the largest to display in the answer box.***??? – ΦXocę 웃 Пepeúpa ツ Jun 30 '17 at 13:55
  • Ok, so after taking what you (@ΦXocę 웃 Пepeúpa ツ)and @Charles May suggested, I've made some progress. I corrected the logical error and figured out how to step through the program. My issue now, though, is that the value of maxNum never changes from the 0 value I initialized it with. As I step through the code, it shows the value of maxNum as 0 even in the assigning statements (like "maxNum = num1"), when the value should change to that of the appropriate number, assuming the conditions were met. Any hints on this issue? I really appreciate you helping out a newbie! – Caroline Jul 03 '17 at 00:30
0

I'm assuming that based on your comment "I can print those number individually" you have no issue in hooking-up the button click to read those textboxes (and presumably no issue in rendering the highest number value too).

I suppose you were trying to understand how if/else statement work and confused that the highest number wasn't returned in your method. As "ΦXocę 웃 Пepeúpa ツ" mentioned, there is a flaw in your logic and it resulted in the default 'else' statement --> try using >= instead of >

hsoesanto
  • 513
  • 2
  • 9
0

Thanks to everyone who answered; I got it working! Shout out to @ΦXocę 웃 Пepeúpa ツ for pointing out my logical error and @Charles May for telling me about the debugging tool. If anyone's interested, here's the fixed code:

namespace Passing_Data
{
public partial class Form1 : Form
{
    private double maxNum;

    public Form1()
    {
        InitializeComponent();
    }


    private double max(double num1, double num2, double num3)
    {
        if (num1 >= num2 && num1 >= num3)
        {
            maxNum = num1;
        }
        else if (num2 >= num1 && num2 >= num3)
        {
            maxNum = num2;
        }
       else if (num3 >= num1 && num3 >= num2)
        {
            maxNum = num3;
        }
        return maxNum;
    }


    private void retBtn_Click(object sender, EventArgs e)
    {
        double num1, num2, num3;
        num1 = double.Parse(num1Box.Text);
        num2 = double.Parse(num2Box.Text);
        num3 = double.Parse(num3Box.Text);
        maxNum = max(num1, num2, num3);
        ansBox.Text = maxNum.ToString();
    }
Caroline
  • 43
  • 7