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#

viktor Degerman
viktor Degerman
3,056 Points

Why does the break not work??? And !=????

using System;

namespace ProgramMain.Calculator {

class Calculator { public static void Main(){ var i = true; while(i){ //Preform var outpot = 0.0;

           //Question 4 a number
   Console.WriteLine("Welcome to my calculator and will repeat until you typ 'quit' and you can put your first number here: ");
   var a = Console.ReadLine();


    //Question 4 the operation
    Console.WriteLine("Put in your opertion +-*/");
    var c = Console.ReadLine();
    //Question 4 another number
    Console.WriteLine("And the last number");
    var e = Console.ReadLine();


     //quit

       if (a == "quit"){
        break;

      }

       //Get to double instead of String
       var b = double.Parse(a);
       var f = double.Parse(e);


       //Error Fixer
      if (b <= 0){
        Console.WriteLine("Not valid Number try again");

          continue;
      }
       if (c != "*"){
        Console.WriteLine("Valid number try again");
         continue;

      }

        if (c != "/"){
        Console.WriteLine("Valid number try again");
         continue;

      }

        if (c != "+"){
        Console.WriteLine("Valid number try again");
         continue;

      }
        if (c != "-"){
        Console.WriteLine("Valid number try again");
         continue;

      }



      try{  

     if (c == "*"){
        outpot = b * f;

      }


      else if (c == "/"){
       outpot =  b / f;
      }
       else if (c == "+"){
       outpot =  b + f;
      }

       else {
       outpot =  b - f;
      }


    //Print to screen
    Console.WriteLine(outpot);

    }
    catch (FormatException){
      Console.WriteLine ("Not valid Number try again");
      continue;

    }
   }
  }

} }

1 Answer

Steven Parker
Steven Parker
231,210 Points

:point_right: The break works just fine. But you don't test for "quit" right away.

In your code, you ask for the operation and then the last number before you check if the first number was "quit". If you want the "quit" to be effective immediately, you'll need to move the test before the other prompts and inputs.

Individually checking that the operation is not ("!=") each possible symbol creates a problem because no matter what symbol is input, 3 out of the 4 tests must fail. Since you check each one to perform it, you could just add a check for "-" and have the final else print the error and continue. Then you could remove all those preliminary (and contradictory) tests.