Search code examples
c#loopsmonodevelop

Calculating remaining calories output


I have most of the code done but the last part has been confusing for about 2 hours now. I am trying to display the remaining calories to the user by subtracting the calories Allowed by the calories of the meal/snack they ate and loop is they say YES to continue. I get an error every time. Any help is greatly appreciated!

using System;

namespace CSE1101Unit2Lab
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            string name;
            double height, weight;
            int userAge;
            string gender;
            double exerciseFactor;
            double BMR = 0;
            double DailyCI = 0;

            Console.Write ("Enter your name: ");
            name = Console.ReadLine ();
            Console.Write ("Enter your height in inches: ");
            height = Convert.ToDouble (Console.ReadLine ());
            Console.Write ("Enter your weight in pounds: ");
            weight = Convert.ToDouble (Console.ReadLine ());
            Console.Write ("Enter your age: ");
            userAge = Convert.ToInt32 (Console.ReadLine ());
            Console.Write ("Enter your gender as M or F: ");
            gender = Console.ReadLine ();
            gender = gender.ToUpper ();
            Console.Write ("Please select your level of activity: ");

            Console.WriteLine ("1. You don't exercise");
            Console.WriteLine ("2. You engage in light exercise one to three days a week");
            Console.WriteLine ("3. You exercise moderately three to five times a week"); 
            Console.WriteLine ("4. You exercise intensely six to seven days a week"); 
            Console.WriteLine ("5. you exercise intensely six to seven days a week and have a physically active job"); 
            Console.WriteLine ("Activity Level: ");

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

            if (gender == "F") {
                BMR = 655 + (4.35 * weight) + (4.7 * height) - (4.7 * userAge);

                if (exerciseFactor == 1) {
                    DailyCI = BMR * 1.2;
                }
                if (exerciseFactor == 2) {
                    DailyCI = BMR * 1.375;
                }
                if (exerciseFactor == 3) {
                    DailyCI = BMR * 1.55;
                }
                if (exerciseFactor == 4) {
                    DailyCI = BMR * 1.725;
                }
                if (exerciseFactor == 5) {
                    DailyCI = BMR * 1.9;

                }
            } else if (gender == "M") {
                BMR = 66 + (6.23 * weight) + (12.7 * height) - (6.8 * userAge);

                if (exerciseFactor == 1) {
                    DailyCI = BMR * 1.2;
                }
                if (exerciseFactor == 2) {
                    DailyCI = BMR * 1.375;

                }
                if (exerciseFactor == 3) {
                    DailyCI = BMR * 1.55;

                }
                if (exerciseFactor == 4) {
                    DailyCI = BMR * 1.725;

                }
                if (exerciseFactor == 5) {
                    DailyCI = BMR * 1.9;


                }
                Console.WriteLine (name + " you entered: \nHeight: " + height + "\nWeight: " + weight + "\nAge: " + userAge + "\nGender: " + gender);
                Console.WriteLine ("Your BMR is " + BMR);
                Console.WriteLine ("Your daily calories allowed is " + DailyCI);


                {

                }
                double caloriesAllowed = BMR * exerciseFactor;
                string response = "YES";
                while (response == "YES");
                var calconsumed = 0;
                {
                    Console.WriteLine ("Enter the amount of calories consumed: ");
                    calconsumed = Convert.ToInt32 (Console.ReadLine ());
                    Console.WriteLine ("Your remaining calories are:" + caloriesAllowed - calconsumed);
                    Console.WriteLine("Do you want to continue? (YES / NO)");
                    response = Console.ReadLine().ToUpper();
                        response = response.ToUpper();
                    }
                }
        }
    }
}

Now it's not looping if the user types YES to continue. How do I get this code to loop?

            {
            double caloriesAllowed = BMR * exerciseFactor;
            var calconsumed = 0;
            string response = "YES";
                do {

                    Console.WriteLine ("Enter the amount of calories consumed: ");
                    Console.WriteLine ("Your remaining calories are:" + (caloriesAllowed - calconsumed));
                    calconsumed = Convert.ToInt32 (Console.ReadLine();
                    Console.WriteLine ("Do you want to continue? YES or NO: ");
                    Console.ReadLine ();
                    response = Console.ReadLine().ToUpper();

                } while (response == "YES");

Solution

  • You build an empty infinity loop because you use an semicolon after the while condition instead the braces.

    this should work

            string response = "YES";
              var calconsumed = 0;
            while (response == "YES")
            {
    
                Console.WriteLine("Enter the amount of calories consumed: ");
                calconsumed = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Your remaining calories are:" + (caloriesAllowed - calconsumed));
                Console.WriteLine("Do you want to continue? (YES / NO)");
                response = Console.ReadLine().ToUpper();
                response = response.ToUpper();
            }