6 Replies - 791 Views - Last Post: 03 March 2013 - 08:46 AM Rate Topic: -----

#1 spooky_dd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-March 13

Restarting a switch statement, need help

Posted 03 March 2013 - 06:47 AM

class Program
    {
        private static int check (int cs)
        {
            switch (cs)
            {
                case 3:
                    return 3;
                case 4:
                    return 4;
                case 9:
                    return 9;
                default:
                    return 0;
            }
        }

        private static int checkCase ()
        {
            Console.WriteLine("Choose denominator: 3, 4 or 9");
            int cs = int.Parse(Console.ReadLine());

            int d = check(cs);

            if (d == 0)
            {
                Console.WriteLine("Error");
                checkCase(); // <--- BUG
            }
            return d;
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Enter beginning of interval");
            int a = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter end of interval");
            int n = int.Parse(Console.ReadLine());
                        
            int d = checkCase();

            for (int i = a; i <= n; i++)
            {
                if (i % d == 0)
                    Console.WriteLine(i);
            }
        }
    }



This simple program checks which numbers from a given interval divide to 3, 4 or 9. I'm trying to make a Switch Case menu to choose from the denominators. I managed to do that but I wanted to make it so that the default case resets the whole switch statement so I made a seperate function for it, where it resets at line 28 if the default is selected.

But I get some kind of a bug (if i may call it that) when I choose the default it checks it for 0 then resets the function as expected and after I select one of the other cases it sets the variable "d" to that case as it should, and goes to return, here's the bug now, but instead of continuing to the main method it goes back to line 28 and resets the variable "d" back to 0 and the variable "cs" back to the default case and then goes to main method and sends an error because d=0.

I want to know why is that "bug" occuring. And If there's a simpler way to restart the switch statement I'd be happy if you share.

Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Restarting a switch statement, need help

#2 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3806
  • View blog
  • Posts: 13,488
  • Joined: 12-December 12

Re: Restarting a switch statement, need help

Posted 03 March 2013 - 07:27 AM

It is hard to follow the logic and flow of your code. However, when you call checkCase() again on line 28 you are not storing the result that it returns, so d will remain at its original value of 0. At least, that is the nearest I can follow your code to a solution :whistling:

I wouldn't call it a bug though..
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5674
  • View blog
  • Posts: 12,186
  • Joined: 02-June 10

Re: Restarting a switch statement, need help

Posted 03 March 2013 - 07:29 AM

Always put in the break; even if you are returning.

This would be a bit simpler

switch (cs)
{    
     case 3:
     case 4:
     case 9:
        return cs
        break;
     default:
        return 0;
        break;
}



But this is all just way convoluted and overly complex.
A simple while{} block will do what you need and throw out the switch method completely

int cs = 0; // starting point
while (cs !=3  && cs !=4 && cs != 9)
{
   Console.WriteLine("Choose denominator: 3, 4 or 9");
   cs = int.Parse(Console.ReadLine());
}

Was This Post Helpful? 1
  • +
  • -

#4 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3806
  • View blog
  • Posts: 13,488
  • Joined: 12-December 12

Re: Restarting a switch statement, need help

Posted 03 March 2013 - 07:30 AM

I would consider merging the two functions check() and checkCase(), using a loop to repeatedly ask for a number until they supply a valid number, then return this number.

Added: ..using the code just supplied by my esteemed colleague :)

This post has been edited by andrewsw: 03 March 2013 - 07:31 AM

Was This Post Helpful? 0
  • +
  • -

#5 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: Restarting a switch statement, need help

Posted 03 March 2013 - 08:06 AM

Breaking things up into more methods will help make your code clearer, and will make your job of implementing the code easier. For example, you could start with a framework that looks something like this:

Spoiler


Now you can focus on each making each method work in isolation, and that should keep you focused, and prevent you from getting overwhelmed with all the details.

Making good use of while loops and the int.TryParse() method should help you out too.

This post has been edited by CodingSup3rnatur@l-360: 03 March 2013 - 09:59 AM

Was This Post Helpful? 1
  • +
  • -

#6 spooky_dd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-March 13

Re: Restarting a switch statement, need help

Posted 03 March 2013 - 08:36 AM

Thank you all for the replys

@ tlhIn`toq

if i use break after the return i get "Unreachable Code" error.

but using a while loop li this

while (d == 0)
            
{
                Console.WriteLine("Грешка");
                Console.WriteLine("Изберете делител: 3, 4 или 9");
                cs = int.Parse(Console.ReadLine());
                d = check(cs);
            }
            return d;

Was This Post Helpful? 0
  • +
  • -

#7 spooky_dd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-March 13

Re: Restarting a switch statement, need help

Posted 03 March 2013 - 08:46 AM

Sorry for the double post. Where's the edit button on this forum?

Thank you all for the replies

@ tlhIn`toq

If i use break after the return i get "Unreachable Code" error.

but using a while loop like this did the job perfectly.

while (d == 0)
            
            {
                Console.WriteLine("Error");
                Console.WriteLine("Choose denominator: 3, 4 or 9");
                cs = int.Parse(Console.ReadLine());
                d = check(cs);
            }
            return d;

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1