0

I cannot spot where my mistake is on my IF/ELSE statement nested inside my for loop. I did try to use a while loop to get make my IF/ELSE statement work, but it didn't work as expected.

Even my Console.Writeline statement cannot capture the right price and size.

My code:

static void Main(string[] args)
{
    string[] size = { "S", "M", "L", "X"};
    decimal[] price = { 6.99M, 8.99M, 12.50M, 15.00M};

    Console.WriteLine("Please enter a pizza size : ");
    string pizzaSize = Console.ReadLine();

    int i;

    for (i = 0; i < size.Length; i++)
    {
        if (size[i] == pizzaSize)
        {
            Console.WriteLine("Your pizza size is " + pizzaSize + ". The price is " + price[i].ToString("C"));
        } else
        {
            Console.WriteLine("Please enter a valid pizza size");
            return;
        }
    }
    Console.ReadKey();
}
6
  • Did you run your code in the debugger? What happens, what do you observe? Note you can use the immediate window in Visual Studio to check for example what the value of size[i] is when you set a breakpoint at your if statement. Commented Apr 2, 2021 at 13:12
  • 2
    If you enter for instance M as selected pizza size, the first check will be if ("S" == "M") which is obviously false. Thus you will hit the else branch in the first iteration, which causes the function to return immediately. Which in Main means, the program terminates. Furthermore consider, that comparing Strings with == is discouraged, and the comparisons are also case sensitive, thus "M" == "m" will return false Commented Apr 2, 2021 at 13:13
  • @ADyson Yup sorry for that. If user input is not the same as size, the error statement will execute Commented Apr 2, 2021 at 13:13
  • 1
    return; in the else clause looks strange to me. Commented Apr 2, 2021 at 13:14
  • 1
    I was taught a very long time ago that a function should have only one exit point. It helps to structure the code and it can save a lot of headaches when trying to debug. Commented Apr 2, 2021 at 15:09

3 Answers 3

6

If the value of pizzaSize is "L" , in the for loop the first check will be if ("L" == "M") which is false then the else branch execute in the first iteration , which return; in the else clause causes the function to return immediately. so If you want to check if the pizzaSize value is valid, you can use the following solution:

static void Main(string[] args)
    {
        string[] size = { "S", "M", "L", "X"};
        decimal[] price = { 6.99M, 8.99M, 12.50M, 15.00M};

        Console.WriteLine("Please enter a pizza size : ");
        string pizzaSize = Console.ReadLine();

        bool isValid = false;

        while(isValid == false){
              for (int i = 0; i < size.Length; i++)
              {
                    if (size[i] == pizzaSize)
                    {
                          Console.WriteLine("Your pizza size is " + pizzaSize + ". The price is " + price[i].ToString("C"));
                          isValid = true;
                          break;
                     }
              }
        
              if (isValid == false){
                    Console.WriteLine("Please enter a valid pizza size");
                    pizzaSize = Console.ReadLine();
              }
        }

        Console.ReadKey();
    }
Sign up to request clarification or add additional context in comments.

Comments

2

The else block will execute every time the tested condition is false, which would be at least 3 times, even if the condition was true, that is if you didn't have the return statement, which will not allow for the cycle to continue, as soon as the code hits the else it will return immediately exiting the program.

The given answer already addresses these issues, what I would change is the comparison, you'd want your code to recognize lower and upper case for input, using Equals would be a more solid approach:

static void Main(string[] args)
{
    string[] size = { "S", "M", "L", "X" };
    decimal[] price = { 6.99M, 8.99M, 12.50M, 15.00M };

    Console.Write("Please enter a pizza size : ");
    string pizzaSize;
    var exists = false;

    while (!exists && (pizzaSize = Console.ReadLine()) != null)
    {
        for (int i = 0; i < size.Length; i++)
        {
            if (size[i].Equals(pizzaSize, StringComparison.OrdinalIgnoreCase))
            {
                Console.WriteLine("Your pizza size is " + size[i] + ". The price is " + price[i].ToString("C"));
                exists = true;
                break;
            }
        }
        if (!exists)
        {
            Console.Write("Please enter a valid pizza size: ");
        }
    }
    Console.ReadKey();
}

And a few minor tweaks to make your I/O more user friendly.

Comments

1

It seems, that you are returning in your else-branch on the first mismatch and therefore leaving the whole function.

There are several ways to fix your code. Without touching your code too much you could introduce a break statement after your WriteLine in the if-branch to leave the loop (you are already done, no need to check against the other sizes) and add an additional if-statement inside the else-branch, wrapping its content, to check whether the current loop iteration is the last one. If it is the last iteration and it is still not matched, it can only be an invalid size.

But actually, you would be better off using a simple switch-case instead of iterating manually.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.