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 Final

C # BASICS

I dont fully understand this please help. What i wanted to do was get the amount of times to say yay by reading what the user enters. if they entered 0, break would cause the end of the program. parsing the yay amount variable yayTotal to total as type double would give me a numeric value that i could use for calculations. if the number is 0 or less, the user is prompted to enter a different value. if the numeric value is less then the yayTotal variable then total would receive 1 and run thru the loop again until total was equal to yayTotal.

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()        
        {
            var runningTotal = 0;

            while(true)
            {

            Console.Write("Enter the number of times to print \"Yay!\": ");
            var yayTimes = Console.ReadLine();

            if (yayTimes == 0)
            {
            }
            break;
            {
            try
            {
            var times = double.Parse(yayTimes);
            if (times <= 0)
            {
                Console.WriteLine(times + "is not an exceptable value");
                continue;
            }
                else (yayTimes < times)
                {
                    ConsoleWriteLine( yayTimes += times )
                }
             runningTotal += times;
            }
                catch (FormatException)
                {
                    Console.WriteLine("That is not a vaild input");
                    Console.ReadLine();
                }
}
Kyle Jensen
Kyle Jensen
Courses Plus Student 6,293 Points

Good morning! I should preface by saying that I'm coming from javascript and starting c# today so I'm not sure if this is correct, but here is something I see.

Your break is outside of the first if statement essentially you have asked the compiler to do nothing if yayTimes = 0 and then I would gather you are just ending the loop there since the break is part of the main loop (outside any conditional) So all you've accomplished is writing the question, collecting the input, checking for zero, and then ending the program.

As well, and again not sure if you can do this in c# but could you save yourself some memory allocation by negating the times variable and just parsing the input from the start? yayTimes = double.Parse (Console.Readline () ). But why make it a floating point? Again, here I'm assuming double means double floating point int..so I apologize if I'm incorrect. You have a 0 conditional twice. I'd look for a way to make one that makes the most sense. One I mention above and the other you should take a look at your concatenation. Make sure to add a space inside your quote marks or times will be crammed against the string.

Also, what is the purpose of runningTotal? I don't see a need for it. You only use it to add the times, but times never changes.

yayTimes is equal to times...only its as a double floating point int. How will it ever be less than times? You cannot enter the else that you've created if it will never be less than... furthermore, can you even place an argument in an else in c#? I would assume that like most other languages you should get an error there, had you fixed everything else up to this point. Else is your default statement. If all the above is not true else { do this } You might want to rethink your direction here and start from scratch. I appreciate you're checking for errors but maybe go about this whole thing in a new way. Write it in psuedo code and cover all your bases. While x is true do these things. You will need input, you will need a countdown that will be equal to 1 or greater than 0 from the users number and with each count print yay! and consider how to terminate the loop when it's done printing the requested number of iteration. Again, I like that you're checking for errors and trust me, I still have many days where find myself banging my head trying to understand why something won't work the way I want it to. Sometimes it can be as simple as order or a semicolon and others more complicated. Keep going it will get easier! Soon you will find yourself doing things you couldn't imagine I hope this helps!

1 Answer

Steven Parker
Steven Parker
231,096 Points

Too much of the program is inside the loop.

The program should ask for and accept input just one time, before the loop. Then, if the value is good it will print some "Yay!" messages, and otherwise it will print an error message. Either way, it should then end.

Also be sure you use the exact error messages as given in the challenge instructions.

Kyle Jensen
Kyle Jensen
Courses Plus Student 6,293 Points

I completely spaced on mentioning that. Agreed and good catch!

Kyle Jensen
Kyle Jensen
Courses Plus Student 6,293 Points

@stevenParker since you're here and I know you're also on the js side. Is c# similar, in that a method or function is considered first class and can be passed as an arg to another function or method? Also, may I ask how you'd compare learning c# to learning js and things I should explicitly look for as different(probably aside from classes)? I know this is off topic to the op's question and if you'd like I can post it separately, but if there's a somewhat simple answer it would be appreciated either way.

Steven Parker
Steven Parker
231,096 Points

One big difference is that in C# inheritance is through classes (a common principle to many languages) and in JavaScript it is through prototypes. The new ES6 additions include "class" and other keywords to make it look more like other languages, but it still works differently "under the hood". Another biggie is that C# is "strongly typed". I can't really cover everything in a short answer but all of it will come to you with time and practice.

Kyle Jensen
Kyle Jensen
Courses Plus Student 6,293 Points

I can understand that. Either way, thank you for the info you did supply. Have a nice holiday weekend!