How do I solve the "unreachable code detected" error in "if" statement?

Seth Mathes

I am new to C# and programming and currently working on a project by the name of Marshals Revenue. I have an error when I run the program if someone can please help me understand the issue.

It tells me unreachable code detected in regards to the "if" statement (error CS0162) and it won't let me run the processing portion of the code. I'm not sure why I'm receiving the error because it looks like the correct syntax.

I also am told that they want to include the " CultureInfo.GetCultureInfo" method. and the correct format is " WriteLine("This is an example: {0}", value.ToString("C", CultureInfo.GetCultureInfo("en-US")));". I'm not sure if that has something to do with why it wont run my "if" statement and not sure where to put the get culture method statement.

Below is the the code I'm using.

Thanks in advance.

using System;
using System.Globalization;
class MarshallsRevenue
{
   static void Main()
   {
     const int INTERIORPRICE= 500;
     const int EXTERIORPRICE=750; 
     string entryString;
     int numberInterior;
     int numberExterior;
     int revenueInterior;
     int revenueExterior;
     int total;
      bool isInteriorGreater;

     // declare the required variables
     bool valid;
     valid=true;
     int Month;
     int monthInterPrice=INTERIORPRICE;
     int monthExterPrice=EXTERIORPRICE;

     // Prompt the user to Enter the month 
     Console.WriteLine("Enter the number of month being scheduled >>");

     // Read the input
     entryString = Console.ReadLine();

     // convert the input to an integer
     Month = Convert.ToInt32(entryString);

     Console.WriteLine("Enter number of interior murals being scheduled >>");
     entryString = Console.ReadLine();
     numberInterior = Convert.ToInt32(entryString);
     Console.WriteLine("Enter number of exterior murals scheduled >>");
     entryString = Console.ReadLine();
     numberExterior = Convert.ToInt32(entryString);

     //use a switch case to perform the aciton
     //as per the entered month 
     switch(Month) {
      //set the exterior murals
      //to zero for the month
      //December through February 
      case 1: 
      case 2:
      case 12:
      numberExterior=0;
      break; 

      //if the month is either 
      //one of April, May, September
      //or October, reduce the price 
      //of exterior murals.

      case 4:
      case 5:
      case 9:
      case 10:
      monthExterPrice = 699;
      break;
      //if the month is either 
      //July or August
      //or October, reduce the price 
      //of interior murals.

      case 7:
      case 8:
      monthInterPrice = 450;
      break;

      //Do nothing for the months 
      //of March June and November.

      case 3:
      case 6:
      case 11:
      break;

      //if the entered month is invalid, 
      //display an error message and 
      //set the is valid month to false. 

      default: 
      Console.WriteLine("The entered month is invalid.");
      valid=false;
      break; 

      //if the entered month is valid 
      //perform the calculations and display
      //the results. 

      if(valid) 
      {
        revenueInterior = numberInterior * monthInterPrice;
        revenueExterior = numberExterior * monthExterPrice;
        total = revenueExterior + revenueInterior;
        isInteriorGreater = numberInterior > numberExterior;
        Console.WriteLine("{0} interior murals are scheduled at {1} each for a total of {2}", numberInterior, monthInterPrice.ToString("C"), revenueInterior.ToString("C"));
        Console.WriteLine("{0} exterior murals are scheduled at {1} each for a total of {2}", numberExterior, monthExterPrice.ToString("C"), revenueExterior.ToString("C"));
        Console.WriteLine("Total revenue expected is {0}", total.ToString("C"));
        Console.WriteLine("It is {0} that there are more interior murals sceduled than exterior ones.", isInteriorGreater);

        



      }




     }




   }
}

Matthew Goulart

The if statement is unreachable because it is enclosed in the switch statement's braces but is is not a part of any of the cases.

I believe you need:

switch(Month) {
 //cases go here
}

if(valid) 
{
  //if stuff goes here
}

2 things you may want to consider.

  1. you can write bool valid = true;
  2. For the months where you do nothing, you can simply omit the cases.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

"Unreachable code detected" error in switch/case statement

From Dev

unreachable code detected on for loop

From Dev

Unreachable code detected in function

From Dev

unreachable code detected on for loop

From Dev

C# Unreachable Code Detected

From Dev

How do I solve MMUMapper error?

From Dev

How do i solve this XAML error?

From Dev

Unreachable code detected in C# Controller

From Dev

how do i solve ssl error in django collectstatic

From Dev

How do I solve this "Cannot read property 'appendChild' of null" error?

From Dev

How do I solve this Scala function parameter type erasure error?

From Dev

How do I solve an error in showing the result of a decorator?

From Dev

How do I solve a content type error in the Microsoft Bot Framework?

From Dev

How do I solve a content type error in the Microsoft Bot Framework?

From Dev

Error when using "sudo Nautilus", how do I solve it?

From Dev

How do I solve bower init EACCES error on ubuntu server?

From Dev

Shortening a piece of code gives an error; How do I solve this issue?

From Dev

How do I solve the error "execv: No such file or directory" from rsync?

From Dev

How do I solve this Nvidia driver download error?

From Dev

Error with if statement and for loop in method append how to solve?

From Dev

Trying to create a grads script and getting error " Expected end of statement " how can I solve it?

From Dev

How do I solve this StackOverflowError?

From Java

Why isn't unreachable code detected when an if condition is a constant?

From Dev

C# Getting Unreachable Code Detected when using StreamWriter

From Dev

How do I combine an IF statement with an IF(OR) statement?

From Dev

How can I solve this error in Flash game?

From Dev

How can i solve this java lang error?

From Dev

How to solve i++ loop error in Python?

From Dev

How to solve i++ loop error in Python?

Related Related

  1. 1

    "Unreachable code detected" error in switch/case statement

  2. 2

    unreachable code detected on for loop

  3. 3

    Unreachable code detected in function

  4. 4

    unreachable code detected on for loop

  5. 5

    C# Unreachable Code Detected

  6. 6

    How do I solve MMUMapper error?

  7. 7

    How do i solve this XAML error?

  8. 8

    Unreachable code detected in C# Controller

  9. 9

    how do i solve ssl error in django collectstatic

  10. 10

    How do I solve this "Cannot read property 'appendChild' of null" error?

  11. 11

    How do I solve this Scala function parameter type erasure error?

  12. 12

    How do I solve an error in showing the result of a decorator?

  13. 13

    How do I solve a content type error in the Microsoft Bot Framework?

  14. 14

    How do I solve a content type error in the Microsoft Bot Framework?

  15. 15

    Error when using "sudo Nautilus", how do I solve it?

  16. 16

    How do I solve bower init EACCES error on ubuntu server?

  17. 17

    Shortening a piece of code gives an error; How do I solve this issue?

  18. 18

    How do I solve the error "execv: No such file or directory" from rsync?

  19. 19

    How do I solve this Nvidia driver download error?

  20. 20

    Error with if statement and for loop in method append how to solve?

  21. 21

    Trying to create a grads script and getting error " Expected end of statement " how can I solve it?

  22. 22

    How do I solve this StackOverflowError?

  23. 23

    Why isn't unreachable code detected when an if condition is a constant?

  24. 24

    C# Getting Unreachable Code Detected when using StreamWriter

  25. 25

    How do I combine an IF statement with an IF(OR) statement?

  26. 26

    How can I solve this error in Flash game?

  27. 27

    How can i solve this java lang error?

  28. 28

    How to solve i++ loop error in Python?

  29. 29

    How to solve i++ loop error in Python?

HotTag

Archive