0

hello please i am having issues getting the values of a system generated variables. here is the code for the getting the values from user;

public void DetailsRate()
{
 begin1:
        Console.WriteLine("\n \t Rate the Acting on a scale of 0 to 5");
        RateActing = int.Parse(Console.ReadLine());
        switch (RateActing)
        {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
                Console.WriteLine("\n you have rated the action of the movie {0}", RateActing);
                break;
            default:
                Console.WriteLine("you have selected the wrong choice {0}", RateActing);
                goto begin1;
        }

        begin2:
        Console.WriteLine("\n \t Rate the music of the movie on a scale of 0 to 5");
        RateMusic = int.Parse(Console.ReadLine());
        switch (RateMusic)
        {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
                Console.WriteLine("you have rated the music of the movie {0}", RateMusic);
                break;
            default:
                Console.WriteLine("you have selected the wrong choice {0}", RateMusic);
                goto begin2;
        }
}

I called the inputed values into this piece of code

public double getoverallRate(double rateact, double ratemus)
    {
        double totrate = 0;

         rateact = RateActing * 0.25;
         ratemus = RateMusic * 0.15;


        totrate = (rateact + ratemus);
        return totrate;
    }

and here is the main method

static void Main(string[] args)
    {

        MovieRating MR = new MovieRating();
        MR.DetailsRate();

        MovieRating MT = new MovieRating();
        double totrate = MT.getoverallRate(1, 2);
        Console.WriteLine("total rate is {0}", totrate);

        Console.ReadKey();
    }

Please what Im i missing the value of totrate is just giving me 0. please help me.

Jeremiah
  • 324
  • 3
  • 13
  • 1
    getoverallRate takes two parameters, you are passing 5. How is it even compiling? And is that the famed "goto" statement in there :o? – Taleeb Aug 21 '15 at 03:20
  • that should be a mistake i did not copy the whole parameters. Its actually 5 parameters. and i do not understand the meaning of famed 'goto'. – Jeremiah Aug 21 '15 at 03:29
  • Where's your `DetailRate()`? What's the usage of `MR` and `MT` and why don't you just use one instead of two? And where are `RateActing` and `RateMusic` stored? Are they global variables? I'm sorry but you may need to rewrite your whole program.... following the OO way. – J.C Aug 21 '15 at 03:30

2 Answers2

0

First get rid of the goto statements. At a quick glance you could write this:

    static void Main(string[] args)
    {
        double RateActing = -1;
        double RateMusic = -1;

        RateActing = GetMovieRating(RateActing);

        RateMusic = GetMovieMusicRating(RateMusic);

        double totrate = getoverallRate(RateActing, RateMusic);

        Console.WriteLine("total rate is {0}", totrate);

        Console.ReadKey();
    }

    private static double GetMovieRating(double RateActing)
    {
        do
        {
            Console.WriteLine("\n \t Rate the Acting on a scale of 0 to 5");
            double.TryParse(Console.ReadLine(), out RateActing);
        }
        while (RateActing < 0 || RateActing > 5);
        Console.WriteLine("\n you have rated the action of the movie {0}", RateActing);
        return RateActing;
    }

    private static double GetMovieMusicRating(double RateMusic)
    {
        do
        {
            Console.WriteLine("\n \t Rate the music of the movie on a scale of 0 to 5");
            double.TryParse(Console.ReadLine(), out RateMusic);
        }
        while (RateMusic < 0 || RateMusic > 5);
        Console.WriteLine("\n you have rated the music of the movie {0}", RateMusic);
        return RateMusic;
    }

    public static double getoverallRate(double rateact, double ratemus)
    {
        rateact *= 0.25;
        ratemus *= 0.15;

        return rateact + ratemus;
    }
thewisegod
  • 1,524
  • 1
  • 10
  • 11
0

There are lots of problems here - almost enough to start over!

First: Never use goto - there are better ways to construct your program flow.

Second: Your method getoverallRate takes less parameters (2) than what you're passing in (5) so this shouldn't even build.

Third: You are referencing three additional variables in getoverallRate that look like they should be local variables but they are not defined anywhere. Should these be passed in like the usage implies in Main.

Fourth: you are passing in values in variables rateact and ratemus but you are overwriting them immediately with your calculations.

Fifth: It would make more sense to me if you were passing in the input values from the user to this method along with any others you need. You should not be using Global variables from user input in any method that calculates values and returns a result. You should always pass in whatever is needed by the calculation.

Sixth: What is the point of the MR declaration and what does DetailsRate do?

joehanna
  • 1,471
  • 1
  • 11
  • 22
  • "Never use goto" - I strongly disagree. There are valid uses for `goto`. 'Try to avoid goto' is more appropriate – Rob Aug 21 '15 at 03:48
  • 2
    I think the fifth rule is wrong. When an Integer multiply with a Double, the result is Double. Type casting is not necessary, please take a try by yourself. – J.C Aug 21 '15 at 03:53
  • 1
    Thanks @J.C and @Rob. I have removed the incorrect point. @Rob, I respect your opinion regarding `goto`. I still believe there are better constructs available that make code more readable and maintainable. Having said that, this is an age-old philosophical argument. It has been done elsewhere and is not worth having again here. Here is just one of them... http://stackoverflow.com/questions/6545720/does-anyone-still-use-goto-in-c-sharp-and-if-so-why – joehanna Aug 21 '15 at 04:05
  • Thanks @ all. Its a great privilege learning from experience programmers. I really appreciate your contributions. – Jeremiah Aug 21 '15 at 05:06