Search code examples
c#if-statementtemperatureboolean-operations

Temperature Advice Application


First question. Any advice helps.

This is for a class though I am trying to understand on my own. I'm having some trouble with syntax errors in my coding. The goal of this console application is for the user to be able to enter a temperature and have a recommendation as far as what clothes are necessary (ie "Put on a light jacket").

I've done the temperature conversion application prior to this and added my code into the advice application. I've looked at other examples and haven't found any concise examples for if...else statements quite like this.

I thought the error was because the variable was not boolean, but I have no idea how to convert it to boolean for only the if else statements.

This is what I have so far:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleF_to_C_App
{
    class Program
    {
        static void Main(string[] args)
        {
            //declare a char variable to store the degree symbol
            char chrDegree = (char)176;

            //display program info
            Console.WriteLine("Temperature Conversions with Advice (v.1) Sept 17, 2015");
            Console.WriteLine("-------------------------------------------------------\n\n");
            //prompt user to enter the temperature in F
            Console.Write("Enter today's temperature in {0} F (eg 60): ", chrDegree);

            //read in the user input
            string strF = Console.ReadLine();

            //declare two doubles to store F and C temperature
            double dblF, dblC;

            //convert input from string to double
            dblF = Convert.ToDouble(strF);

            //calculate celsius using fahrenheit
            dblC = (dblF - 32) * 5 / 9;

            Console.WriteLine("\n\nToday's Temperature: {0:F2}{1} F = {2:F2}{1} C \n\n",
                dblF, chrDegree, dblC);

            double temp = double.Parse(Console.ReadLine());

            //if the user enters < 40
                if (temp < 40)
            {
                Console.WriteLine("\n\nIt is very cold. Put on a heavy coat.");
            }

            else if
            {
                 (temp > 40 || temp < 60)
                Console.WriteLine("\n\nIt is cold. Put on a coat.");
            }
            else if
            {
                 (temp >= 60 || temp < 70)
                Console.WriteLine("\n\nThe temperature is cool. Put on a light jacket.");
            }
            else if
            {
                 (temp >= 70 || temp < 80)
                Console.WriteLine("\n\nThe temperature is pleasant. Wear anything you like.");
            }
            else if
            {
                  (temp >= 80 || temp < 90)
                Console.WriteLine("\n\nThe temperature is warm. Wear short sleeves.");
            }
            else if
            {
                 (temp >= 90)
                Console.WriteLine("\n\nIt is hot. Wear shorts today.");
            }

            Console.WriteLine("Thank you for using the Temperature Conversion Application.\n\n");
            //ask if the user wants to continue
            Console.Write("Do you want to continue Y/N ? ");
            //reads in the user input
            strContinue = Console.ReadLine();
            Console.WriteLine("\n\n");

            //if the user enters N or n
            if (strContinue == "N" || strContinue == "n")
            {
            //set the bool variable to false
            boolContinue = false;
            }
            //otherwise
            else
            {
            //set the boolean variable to true
            boolContinue = true;
            }

            Console.ReadKey();

        }
    }
}

Solution

  • You get syntax error at these points.

    else if
    {
        (temp > 40 || temp < 60)
        Console.WriteLine("\n\nIt is cold. Put on a coat.");
    }
    

    The syntax is if( expression) { /* ... */ }, so the ( have to go directlyafter the if. This is correct:

    else if (temp > 40 || temp < 60)
    {
        Console.WriteLine("\n\nIt is cold. Put on a coat.");
    }
    

    Also, you forgot to declare this variable as a string.

    strContinue = Console.ReadLine();
    

    And you do correctly set this boolean value to true or false, so you'll just need to move the declaration of the bool boolContinue = true; to the beginning of the Main() function, the wrap all your existing code in a while(boolContinue) expression.