Ok I think im getting better at these but not quite still have these implicit convert from int to string problems and the usual "does not exist in the current context" dilemma.

 * */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Calories
    class Calories
        static void Main()
            string hours,
             double caloriesBurned;
             hoursSpent = GetHours();
             minutesSpent = GetMinutes();

            Console.WriteLine("\nData Entry");
            hours = GetHours();
            minutes = GetMinutes();
            caloriesBurned = 3.9 * minutes; 
            Console.WriteLine("\nPress any key to continue");
            DisplayResults(hours, minutes, caloriesBurned);

        } // main ends 

        public static void DisplayInstructions()
            Console.WriteLine("This program will calculate how many calories burned on a treadmill");
            Console.WriteLine(" ");

        } // end of DisplayInstructions Method

        public static int GetHours()

            string userHours;
             string hours;
            Console.WriteLine("Please enter in the number of hours spent on treadmill: ");
            userHours = Console.ReadLine();
             hoursSpent = int.Parse(userHours);
            return hoursSpent;

        } // end of GetHours method

        public static string GetMinutes()
            string userMinutes,
            Console.WriteLine("Please enter the number of minutes spent on treadmill: ");
            userMinutes = Console.ReadLine();
             minutesSpent = int.Parse(userMinutes);
            return minutesSpent;
        } //end of GetMinutes method

        public static double caloriesBurned(int minutes)
            return (3.9 * minutes);
        } // end of caloriesBurned method

        public static void DisplayResults(string GetMinutes, string GetHours, double caloriesBurned)
            Console.WriteLine("Minutes:\t\t {0:N1}" +
                              "Hours: \t\t {1:N1}" +
                              "Calories Burned: \t\t {2:N1}", GetMinutes,
                              GetHours, caloriesBurned);
        } //ends display results method
    } // end class
} // end namespace


Error 4 The name 'hoursSpent' does not exist in the current context 54 14 Calories
Error 6 The name 'minutesSpent' does not exist in the current context 65 14 Calories
Error 5 The name 'hoursSpent' does not exist in the current context 55 20 Calories
Error 7 The name 'minutesSpent' does not exist in the current context 66 20 Calories
Error 2 Cannot implicitly convert type 'int' to 'string' 28 21 Calories
Error 1 Cannot implicitly convert type 'int' to 'string' 19 27 Calories
Error 3 Operator '*' cannot be applied to operands of type 'double' and 'string' 30 30 Calories

Thanks again for the help

Also it would be awesome if you can tell me how to avoid "does not exist in current context errrors" and " cannot implicitly convert type int to string" errors as well.

Edited by techlawsam: n/a

5 Years
Discussion Span
Last Post by arunkumars

The scope of a variable declared inside a method is alive only inside that method, it will not be accessible outside the method other than the place where it was declared and initialised.
hourspent has been declared in Main() and GetHours() knows only if it was declared inside GetHours() method or the respective class.
there are 2 ways to overcome this error
a. declare your variable as a class level variable or
b. pass the variable as a parameter to the method. (if is not assigned, u can still return it back with the data type).

Error 2 and 1 : you have declared hourspent as string and you are trying to pass a int value, you can use hourspent = Gethours().ToString();
this should get you out of those errors.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.