# Architect Arithmetic Errors from the Switch Statement

I keep getting cannot convert void to bool errors from the Switch statments.
What am I doing wrong?

using System;

namespace ArchitectArithmetic
{
class Program
{
public static void Main(string args)
{
Console.WriteLine(“Hello, there! What monument would you like to work with? Please choose from one of the following: Teotihuacan, TajMahal, Mecca”);
string monument = Console.ReadLine();

``````  switch(monument)
{
case "Teotihuacan":
Console.WriteLine(CalculateTotalTeoCost());
break;
case "TajMahal":
Console.WriteLine(CalculateTotalTajCost());
break;
case "Mecca":
Console.WriteLine(CalculateTotalMeccaCost());
break;
default:
Console.WriteLine("It is important that you improve your reading skills.  Please choose from one of the following: Teotihuacan, TajMahal, Mecca");

}

}

public static double RectArea(double length, double width)
{
return length * width;
}
public static double CircleArea(double radius)
{
return Math.PI * Math.Pow(radius, 2);
}

public static double TriangleArea(double bottom, double height)
{
return 0.5 * bottom * height;
}

public static void CalculateTotalTeoCost()
{
double teoRect = RectArea(1500, 2500);
double teoCircle = CircleArea(375) * 0.5;
double teoTri = TriangleArea(500, 750);
double totalCost = Math.Round((teoRect + teoCircle + teoTri) * 180, 2);

Console.WriteLine(\$"Total construction of Teotihuacan is {totalCost} pesos.");
}
``````

public static void CalculateTotalTajCost()
{
double tajRect = RectArea(90.5, 90.5);
double tajTri = TriangleArea(24, 24);
double tajTotal = tajRect - (4 * tajTri);

``````  double totalCost = Math.Round(tajRect * 180, 2);

Console.WriteLine(\$"Total construction of Taj Mahal in Agra, India is {totalCost}.");
}
``````

public static void CalculateTotalMeccaCost()
{
double meccaBigRect = RectArea(284, 264);
double meccaSmallRect = RectArea(180, 106);
double meccaTri = TriangleArea(84, 264);
double totalCost = Math.Round((meccaBigRect + meccaSmallRect - meccaTri) * 180, 2);

``````  Console.WriteLine(\$"Total construction of Great Mosque of Mecca in Mecca, Saudi Arabia is {totalCost}.");
}
``````

}
}

1 Like

For proper formatting of code in forum posts, see the thread:

In your switch statement, you have:

``````Console.WriteLine(CalculateTotalTeoCost());
``````

The above statement makes a call to the method `CalculateTotalTeoCost` and then tries to pass the returned (returned by `CalculateTotalTeoCost`) value as an argument to the `Console.WriteLine` method. But, in the code you have posted `CalculateTotalTeoCost` doesn’t return any value because the return type is `void` in the method declaration:

``````public static void CalculateTotalTeoCost() {...
``````

`void` specifies that a method doesn’t return a value (see documentation for void). This causes a problem because the `WriteLine` method is expecting a value to be passed as the argument in the statement `Console.WriteLine(CalculateTotalTeoCost());`

How you solve this issue depends on how you want your program to behave.

• One way is to leave `CalculateTotalTeoCost` as it is and instead modify the statements in the switch:
``````switch(monument) {
case "Teotihuacan":
// Console.WriteLine(CalculateTotalTeoCost()); Delete this statement
CalculateTotalTeoCost();
break;
case ...
}

public static void CalculateTotalTeoCost() {
// ...
Console.WriteLine(\$"Total construction of Teotihuacan is {totalCost} pesos.");
}
``````

In this version, if the switch case matches, then the `CalculateTotalTeoCost` method is called and printing the output is left to the `CalculateTotalTeoCost` method.

• Another possible solution is to the leave the switch statements unchanged, but modify the `CalculateTotalTeoCost` method so that the method returns a string instead of printing the output:
``````switch(monument) {
case "Teotihuacan":
Console.WriteLine(CalculateTotalTeoCost());
break;
case ...
}

public static string CalculateTotalTeoCost() {
// ...
return \$"Total construction of Teotihuacan is {totalCost} pesos.";
}
``````

In this version, `CalculateTotalTeoCost` returns a string. Writing this returned sting to the console is left as the job of the switch statement.

Remarks:

• You will need to make similar changes by EITHER deleting/editing all the `Console.WriteLine` statements from all the other cases as well OR modifying the return types of all the methods `CalculateTotalTeoCost`, `CalculateTotalTajCost` and `CalculateTotalMeccaCost`

• Also, you will need to add the `break;` statement after the default case as well. In C/C++ and many other languages, the `break` would not be necessary if the default case is at the bottom of the switch statement. But in C#, you will get an error similar to:

Control cannot fall out of switch from final case label (‘default:’)

because in C#, you must explicitly transfer control (`break`, `return`, `goto`, `throw`).
`break;` isn’t necessary per se, rather the reason is: Four switch oddities [See the explanation for the second oddity titled “Case 2” in this link)

It requires that every switch section, including the last one , have an unreachable end point