Tubby McChubby
Tubby McChubby

Reputation: 11

Why is Invalid Expression Term "String" showing up?

I think the error has to do with the If statement, but i'v tried searching for the error, and most of the problems are caused by syntax errors, which doesn't seem to be the case for me. Thank you in advance for the help.

using System;

namespace FirstConsoleProjectSolution
{

  class MainClass
  {

    public static void Main (string[] args) // this is a method called "Main". It is called when the program starts.
    {
        string square;
        string cylinder;

        Console.WriteLine ("Please enter a shape");

        if (string == square) { 

            double length;
            double width;
            double height;

            Console.WriteLine ("Please enter length");
            Console.ReadLine (Convert.ToDouble ());

            Console.WriteLine ("Please enter width");
            Console.ReadLine (Convert.ToDouble ());

            Console.WriteLine ("Please enter height");
            Console.ReadLine (Convert.ToDouble ());

            Console.WriteLine ("Your total volume is" + length * width * height);
        }

        if (string == cylinder) {

            double areaOfBase;
            double height;

            Console.WriteLine ("Please enter area of base");
            Console.ReadLine (Convert.ToDouble ());

            Console.WriteLine ("Please enter height");
            Console.ReadLine (Convert.ToDouble ());

            Console.WriteLine ("Your total volume is" + areaOfBase * height);

        }
    }

  }

}   

Upvotes: 1

Views: 1489

Answers (3)

user4426213
user4426213

Reputation:

You have not assigned your string variables

        string square;
        string cylinder;

You have not captured the input from the user

Solution

string square = "square";
string cylinder = "cylinder";
string input;

Console.WriteLine ("Please enter a shape");
input = Console.ReadLine();

if (input == square) { 

    // Do stuff

}

You are getting error because you are comparing primitive type declaration 'string' to an instance of type string cylinder

if (string == square) {

Upvotes: 0

Bryan Crosby
Bryan Crosby

Reputation: 6554

You have no variable named string. It is also illegal to use string because it is a keyword in the language.

If you are content on using the variable name string (I would probably avoid it as it is not very descriptive in this case), you can escape keywords by adding the @ symbol to the beginning of the variable name.

Your code has multiple problems. The first is that you are not actually asking for any input in the beginning. If your intent is to get input from the user, you should consider assigning the value of Console.ReadLine() to a variable. You should consider something such as:

Console.WriteLine("Please enter a shape");
string shapeType = Console.ReadLine();

if (shape == "square")
{
    //do something
}

If you insist on naming your variable string, you will have to say

string @string = Console.ReadLine();

Upvotes: 0

Guffa
Guffa

Reputation: 700152

That's because of this statement:

if (string == square) {

The string keyword represents a data type, and it's not possible to compare a data type and a string.

The message that you print out suggests that you are trying to input something, but there is no input. I think that you are trying to do something like:

Console.WriteLine ("Please enter a shape");
string shape = Console.ReadLine();
if (shape == "square") {
  ...

Later on in the code, when you try to input numbers, you would use code like this to parse the string and put it in a variable:

length = Convert.ToDouble(Console.ReadLine());

Upvotes: 3

Related Questions