By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,051 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

I am getting eror like use of unassigned variable totalhourrate.

P: 1
Expand|Select|Wrap|Line Numbers
  1. using static System.Console;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace MoveEstimator
  8. {
  9.     class MoveEstimator
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             const  int baserate=200;
  14.             const int rateperhour = 150;
  15.             const int ratepermile = 20;
  16.  
  17.             int totalhourrate, totalmiles;
  18.             for (int numhours=1, nummiles = 1;  numhours <= 24 && nummiles <= 100; numhours++,nummiles++)
  19.             {
  20.                 WriteLine("Enter the number of hours worked", numhours);
  21.                 ReadKey();
  22.                 totalhourrate = numhours * rateperhour;
  23.                 WriteLine("\nTotal rate of hours worked: " + totalhourrate);
  24.  
  25.  
  26.  
  27.                 WriteLine("\n Enter the number of miles", nummiles);
  28.                 ReadKey();
  29.                 totalmiles = nummiles * ratepermile;
  30.                 WriteLine("\n Total miles covered: " +totalmiles);
  31.  
  32.  
  33.  
  34.             }
  35.             ReadLine();
  36.             int totalfee = baserate + totalhourrate + totalmiles;
  37.             WriteLine("\n The total fee is: " +totalfee);
  38.             ReadKey();
  39.  
  40.  
  41.         }
  42.     }
  43. }
Mar 20 '19 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 1,026
You should move line #17 ("int totalhourrate, totalmiles;")
to just before line #11 ("static void Main(string[] args)")
and add 'private static ' in front of it.

Or you could initialize those vars at line #17, changing that line to:
Expand|Select|Wrap|Line Numbers
  1. int totalhourrate = 0, totalmiles = 0;
The explanation about this is here: CS0165
The C# compiler does not allow the use of uninitialized variables. If the compiler detects the use of a variable that might not have been initialized, it generates compiler error CS0165. ...
1 Week Ago #2

Post your reply

Sign in to post your reply or Sign up for a free account.