Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Basics (Retired) Perfect Doubles

Liam Andersson
Liam Andersson
1,396 Points

Is this a bug? Or am I missing something?

(It worked with double once then it didnt work lol)

I removed "int" and replaced it with "doubles" instead so I could type decimals in the application! But I have a problem. I dont know why, but one function stopped working. My "You have entered" + minutes + "." stopped working and I cant figure out why. I dont get any error but the code just wont work the way I want it to.

Code: https://w.trhou.se/mtr2a64468

It doesnt work switching out doubles with int. What's the problem really?

Liam Andersson
Liam Andersson
1,396 Points

I dont know why it wont show the whole code, but I hope you can figure it out anyway! Thanks for any answer!

Steven Parker
Steven Parker
229,644 Points

When pasting code, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

But even better, you can make a snapshot of your workspace and post the link to it here. That will also make it much easier to test the solution.

Liam Andersson
Liam Andersson
1,396 Points

Ah like this? So I can send you this link and you will be linked to my snapshot?

https://w.trhou.se/mtr2a64468

1 Answer

Steven Parker
Steven Parker
229,644 Points

Using the snapshot, I tried the program and noticed that it only prints out the total if the number you enter is greater than 60. I found it difficult to see the program flow looking at the source because of how it is indented. But here's the "try" section re-indented to show nesting levels:

    double minutes = double.Parse(entry);

    if (minutes <=10)
    {
        System.Console.WriteLine("Better than nothing right?");
    }
    else if (minutes <= 30)
    {
        System.Console.WriteLine("Great job! Give yourself a pat on the back!");                     
    }
    else if (minutes <= 60)
    {
        System.Console.WriteLine("Are you a ninja warrior or what?!");
    }
    else
    {
        System.Console.WriteLine("I think I've found the new superman!");
        if (minutes <=0)
        {
            System.Console.WriteLine(minutes + " isn't an acceptable value, please try again!");
            continue;       
        }

        //Add minutes exercised total
        runningTotal = runningTotal + minutes;

        // Display total minutes exercised to the screen
        System.Console.WriteLine("You have entered: " + runningTotal + " minutes");

        // Repeat until user quits
    }

This makes it easier to see that the code in the final "else" only runs when the input is more than 60, and the test for "minutes <= 0" will never be true .

I expect that you will want to make some changes to the program flow so that the total can be accumulated (and output) for values below 60. Perhaps the whole issue is caused by a closing brace in the wrong place.

Liam Andersson
Liam Andersson
1,396 Points

Sadly I don't get it.. I even double checked the instructors notes. How come that only when it is over 60 the " You have entered" code will run?

Steven Parker
Steven Parker
229,644 Points

You have an "if...else if" chain that checks for different input values and the final "else" only runs when the value is more than 60. The code that outputs that message is inside that "else" block (but probably should not be).

Try moving the close brace that is currently on line 56 up between lines 41 and 42.

Liam Andersson
Liam Andersson
1,396 Points

Nevermind I fixed the error. I took a closer look at the "else" and saw that I missed some brackets there and here. I dont know how it happened though! Thanks for your help anyway!