1,105,578 Community Members

implicit convert, current context issues

Member Avatar
techlawsam
Junior Poster in Training
56 posts since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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,
                minutes,
                hoursSpent,
                minutesSpent;
             double caloriesBurned;
             hoursSpent = GetHours();
             minutesSpent = GetMinutes();

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

            Console.ReadKey();
        } // main ends 

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

        } // 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,
            minutes;
            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.Clear();
            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

errors:

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.

Member Avatar
arunkumars
Posting Whiz in Training
204 posts since Jul 2009
Reputation Points: 16 [?]
Q&As Helped to Solve: 22 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: