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);
}
}
}
}
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.
bool valid = true;
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments