Click here to Skip to main content
15,909,242 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
every time i try to run the program it says A local variable named `wm' cannot be declared in this scope because it would give a different meaning to `wm', which is already used in a `parent or current' scope to denote something else
ii tried using booleans but couldnt do it
what should i change?
this is the code:
C#
using System;

class MainClass {
  public static void Main (string[] args) {
  int wd = 1;
	int wm = 1;
	int wy = 1;
    Console.WriteLine ("enter digit between 0-4");
    int md = int.Parse(Console.ReadLine());
   //md=menu digit
    if (md==0)
{
    Console.WriteLine("Bye Bye");
}
else if (md==1)
{
	Console.Write("enter month number-");
	int month = int.Parse(Console.ReadLine());
	Console.Write("enter a day-");
	int day = int.Parse(Console.ReadLine());
	Console.Write("enter a year-");
	int year = int.Parse(Console.ReadLine());
	if (month<0 || month>12){
	  Console.WriteLine("wrong month");
	  int wm = 0;
	}
	else if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12 && day>31 || day<0){
	  Console.WriteLine("wrong day");
	  int wd = 0;
	}
	else if (month==4 || month==6 || month==9 || month==11){
	  if (day>30 || day<0){
	  Console.WriteLine("wrong day");
	  int wd = 0;
	}
}
if(month==2)
{
  if (year%400==0 || year%4==0)
  {
    if (day>29 || day<0){
      Console.WriteLine("wrong day");
      int wd = 0;
    }
  }
    else {
      if (day<0 || day>28){
      Console.WriteLine("wrong day");
      int wd = 0;
    }
  }
  if (year<0 || year>9999){
    Console.WriteLine("wrong year");
    int wy = 0;
  }
  
}
}
}
}                                         


What I have tried:

i tried to replace the numbers with booleans and searching on google
Posted
Updated 22-Mar-18 12:19pm
Comments
RedDk 22-Mar-18 18:13pm    
I'm not sure I see what pm is refering to beneath because he's refering to 'wd' which does get declared and asigned and all ... but with respect to 'wm', which is what you propose is the problem ... it has no scope at all anywhere anyway ... my compiler would throw up a message to the effect that "wm was declared but never used". So what IS the problem again?

When you put int before a variable name, it is a declaration, and you can declare a variable only 1 time in a procedure.
Solution: remove the int in front of every wd but the first one. you may have to do the same for other variables.
-----
Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C#
using System;

class MainClass {
  public static void Main (string[] args) {
    int wd = 1;
    int wm = 1;
    int wy = 1;
    Console.WriteLine ("enter digit between 0-4");
    int md = int.Parse(Console.ReadLine());
    //md=menu digit
    if (md==0)
    {
      Console.WriteLine("Bye Bye");
    }
    else if (md==1)
    {
      Console.Write("enter month number-");
      int month = int.Parse(Console.ReadLine());
      Console.Write("enter a day-");
      int day = int.Parse(Console.ReadLine());
      Console.Write("enter a year-");
      int year = int.Parse(Console.ReadLine());
      if (month<0 || month>12){
        Console.WriteLine("wrong month");
        int wm = 0;
      }
      else if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12 && day>31 || day<0){
        Console.WriteLine("wrong day");
        int wd = 0;
      }
      else if (month==4 || month==6 || month==9 || month==11){
        if (day>30 || day<0){
          Console.WriteLine("wrong day");
          int wd = 0;
        }
      }
      if(month==2)
      {
        if (year%400==0 || year%4==0)
        {
          if (day>29 || day<0){
            Console.WriteLine("wrong day");
            int wd = 0;
          }
        }
        else {
          if (day<0 || day>28){
            Console.WriteLine("wrong day");
            int wd = 0;
          }
        }
        if (year<0 || year>9999){
          Console.WriteLine("wrong year");
          int wy = 0;
        }

      }
    }
  }
}

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
 
Share this answer
 
Why to force doors wide open? You should work with proper data type - a DateTime. See:

C#
CultureInfo provider = CultureInfo.CreateSpecificCulture("en-US");
string format = "MM/dd/yyyy";
string stringDate;
DateTime myDate;

Console.Write("Enter date in the following format: '{0}'", format);
stringDate = Console.ReadLine();
try
{
	myDate = DateTime.ParseExact(stringDate, format, provider);
	Console.WriteLine("Correct date!");
}
catch (FormatException)
{
	Console.WriteLine();
	Console.WriteLine("'{0}' is NOT in the correct format.", stringDate);
}


For further details, please see: DateTime.ParseExact Method (String, String, IFormatProvider) (System)[^]
 
Share this answer
 
Your issue is because you are re-declaring your variables.

C#
// Declare a new variable named 'myInt'
int myInt;
// Populate the variable
myInt = 22;
// Repopulate the variable
myInt = 33;

Once you declare a variable it is available throughout the scope, which is determined where you declare it.
For instance, if you declare it outside of a method, it is available throughout the entire class.
If you declare it inside a method, it is available throughout the method
If you declare it inside of an if or using statement then it is available throughout if or using statement but not outside of it.
Additional Examples below
C#
using System;
// declare a variable that can be accessed throughout any method within this class
private int ClassAccessibleInt = 12;

public static void Main(string[] args)
{
// declare a variable that can be accessed within the Main method
int MethodAccessibleInt = 10;
// declare another variable that can be accessed within the Main by adding 2 others
int AnotherMethodAccessibleInt = ClassAccessibleInt + MethodAccessibleInt;
// create an if statement
if(DateTime.Today.Month > 2)
{
    // declare another variable that can be accessed only within the If Statement
    int IfAccessibleInt = DateTime.Today.Month + ClassAccessibleInt;
    // add value to AnotherAccessibleInt
    AnotherMethodAccessibleInt = AnotherMethodAccessibleInt + IfAccessibleInt;
    // IfAccessibleInt = 3 + 12 = 15
    // ClassAccessibleInt = 12
    // MethodAccessibleInt = 10
    // AnotherMethodAccessibleInt = 10 + 12 + 3 + 12 = 37
    // close if statement
}
// IfAccessibleInt no longer available
// ClassAccessibleInt = 12
// MethodAccessibleInt = 10
// AnotherMethodAccessibleInt = 37
// end Main Method
}
// ClassAccessibleInt = 12
// MethodAccessibleInt no longer available
// AnotherMethodAccessibleInt no longer available


Kind Regards
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900