Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Suggestions of control statement needed

Tags:

c#

I'm a student and I got a homework i need some minor help with =)

Here is my task:

Write an application that prompts the user to enter the size of a square and display a square of asterisks with the sides equal with entered integer. Your application works for side’s size from 2 to 16. If the user enters a number less than 2 or greater then 16, your application should display a square of size 2 or 16, respectively, and an error message.

This is how far I've come:

        start:
        int x;
        string input;
        Console.Write("Enter a number between 2-16: ");
        input = Console.ReadLine();
        x = Int32.Parse(input);
        Console.WriteLine("\n");
        if (x <= 16 & x >= 2)
        {
          control statement
          code
          code
          code
        }
        else
        {
            Console.WriteLine("You must enter a number between 2 and 16");
            goto start;
        }

I need help with...

... what control statment(if, for, while, do-while, case, boolean) to use inside the "if" control.

My ideas are like...

do I write a code that writes out the boxes for every type of number entered? That's a lot of code...

..there must be a code containing some "variable++" that could do the task for me, but then what control statement suits the task best?

But if I use a "variable++" how am I supposed to write the spaces in the output, because after all, it has to be a SQUARE?!?! =)

I'd love some suggestions on what type of statements to use, or maybe just a hint, of course not the whole solution as I am a student!

like image 666
fierflash Avatar asked Sep 05 '10 23:09

fierflash


3 Answers

It's not the answer you're looking for, but I do have a few suggestions for clean code:

  1. Your use of Int32.Parse is a potential exception that can crash the application. Look into Int32.TryParse (or just int.TryParse, which I personally think looks cleaner) instead. You'll pass it what it's parsing and an "out" parameter of the variable into which the value should be placed (in this case, x).
  2. Try not to declare your variables until you actually use them. Getting into the habit of declaring them all up front (especially without instantiated values) can later lead to difficult to follow code. For my first suggestions, x will need to be declared ahead of time (look into default in C# for default instantiation... it's, well, by default, but it's good information to understand), but the string doesn't need to be.
  3. Try to avoid using goto when programming :) For this code, it would be better to break out the code which handles the value and returns what needs to be drawn into a separate method and have the main method just sit around and wait for input. Watch for hard infinite loops, though.

It's never too early to write clean and maintainable code, even if it's just for a homework assignment that will never need to be maintained :)

like image 198
David Avatar answered Sep 28 '22 06:09

David


You do not have to write code for every type of number entered. Instead, you have to use loops (for keyword).

Probably I must stop here and let you do the work, but I would just give a hint: you may want to do it with two loops, one embedded in another.

I have also noted some things I want to comment in your code:

  1. Int32.Parse: do not use Int32, but int. It will not change the meaning of your code. I will not explain why you must use int instead: it is quite difficult to explain, and you would understand it later for sure.
  2. Avoid using goto statement, except if you were told to use it in the current case by your teacher.
  3. Console.WriteLine("\n");: avoid "\n". It is platform dependent (here, Linux/Unix; on Windows it's "\r\n", and on MacOS - "\n\r"). Use Environment.NewLine instead.
  4. x <= 16 & x >= 2: why & and not ||?
  5. You can write string input = Console.ReadLine(); instead of string input; followed by input = Console.ReadLine();.
like image 29
Arseni Mourzenko Avatar answered Sep 28 '22 05:09

Arseni Mourzenko


Since it's homework, we can't give you the answer. But here are some hints (assuming solid *'s, not white space in-between):

  1. You're going to want to iterate from 1 to N. See for (int...
  2. There's a String constructor that will allow you to avoid the second loop. Look at all of the various constructors.

Your current error checking does not meet the specifications. Read the spec again.

You're going to throw an exception if somebody enters a non-parsable integer.

goto's went out of style before bell-bottoms. You actually don't need any outer control for the spec you were given, because it's "one shot and go". Normally, you would write a simple console app like this to look for a special value (e.g., -1) and exit when you see that value. In that case you would use while (!<end of input>) as the outer control flow.

like image 30
Rob Avatar answered Sep 28 '22 05:09

Rob