Using the sizeof function

I am getting an error dealing with the namespace

Page 1 of 1

6 Replies - 1006 Views - Last Post: 20 January 2010 - 09:12 PM Rate Topic: -----

#1 Hawkeye28   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 12-November 09

Using the sizeof function

Post icon  Posted 20 January 2010 - 06:47 PM

I am getting this error "The type of namespace name "year" could not be found (are you missing a using directive
or an assembly reference". I am completly new to C# and am not sure what is going on. I only have one namespace and program titled like this. I want to validate the length of the year as input and return the each digit one line after another. Why am I getting this?


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

namespace A1Q3
{
	class Program3
	{
		static void Main(string[] args)
		{
			int year;

			// Get year input from user.
			Console.Write("Please type a year (between 0 and 2009): ");
			year = Convert.ToInt32(Console.In.ReadLine() );

			/* Validate the year input. If the the year is valid
			   output the year one digit per line */

			if (sizeof(year) == 4)
			{
				Console.WriteLine(year / 1000);
				Console.WriteLine(year / 10000);
				Console.WriteLine(year / 10000);
				Console.WriteLine(year / 10000);
				Console.WriteLine("All Done.");
			}
			else Console.Write("Please enter a valid 4 digit year. Example \"2000\"\n");

		}
	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: Using the sizeof function

#2 zombie_chan51   User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 327
  • Joined: 16-March 08

Re: Using the sizeof function

Posted 20 January 2010 - 07:00 PM

you have to be in unsafe mode to use sizeof

http://msdn.microsof...zb8(VS.71).aspx

This post has been edited by zombie_chan51: 20 January 2010 - 07:01 PM

Was This Post Helpful? 0
  • +
  • -

#3 zombie_chan51   User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 327
  • Joined: 16-March 08

Re: Using the sizeof function

Posted 20 January 2010 - 07:25 PM

Also SizeOf isn't for variable, only data types

example

int sizeofInt=sizeof(int);

This post has been edited by zombie_chan51: 20 January 2010 - 07:27 PM

Was This Post Helpful? 0
  • +
  • -

#4 crepitus   User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: Using the sizeof function

Posted 20 January 2010 - 07:29 PM

The sizeof operator tells you how much memory is used to store the type that you pass to it. It's the wrong thing to be using here. Make sure you look up the things you are unsure of in the MSDN library (just press f1 in the IDE).

You should probably read the line from the user into a string variable, and then check that:

- The length of the string is 4
- All the characters in the string are numeric.

Then you could write characters from the string to the console to get each digit.
Was This Post Helpful? 0
  • +
  • -

#5 Hawkeye28   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 12-November 09

Re: Using the sizeof function

Posted 20 January 2010 - 08:44 PM

Thanks. I was thinking the sizeof could be used on an int like .Length on a string. I got ya. This is what I can up to validate it, minus the math to get the digits to print on each line. Had the assignment done reading it in as string and using substring, but it has to remain an integer. I will figure it out. Thanks.

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

namespace A1Q3_String
{
	class Program3_String
	{
		static void Main(string[] args)
		{
			String year;

			// Get year input from user.
			Console.Write("Please type a year (between 0 and 2009): ");
			year = Console.In.ReadLine();

			/* Validate the year input. If the the year is valid
			   output the year one digit per line */

			int i = 0;
			while(i < 1)
			{
				if (year.Length == 4)
				{
					int newYearVal = Convert.ToInt32(year);
					Console.WriteLine(newYearVal / 1000);
					Console.WriteLine(newYearVal / 1000);
					Console.WriteLine(newYearVal / 1000);
					Console.WriteLine(newYearVal / 1000);
					Console.WriteLine("All Done.");

					i++;
				}
				else Console.Write("Please enter a valid 4 digit year.\n (Ex: \"2000\"):");
				year = Console.In.ReadLine();
			}
		}
	}
}



Was This Post Helpful? 0
  • +
  • -

#6 MentalFloss   User is offline

  • .
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,590
  • Joined: 02-September 09

Re: Using the sizeof function

Posted 20 January 2010 - 09:03 PM

Quote

int i = 0;
			while(i < 1)
			{
				if (year.Length == 4)
				{
					int newYearVal = Convert.ToInt32(year);
					Console.WriteLine(newYearVal / 1000);
					Console.WriteLine(newYearVal / 1000);
					Console.WriteLine(newYearVal / 1000);
					Console.WriteLine(newYearVal / 1000);
					Console.WriteLine("All Done.");

					i++;
				}
				else Console.Write("Please enter a valid 4 digit year.\n (Ex: \"2000\"):");
				year = Console.In.ReadLine();
			}



I would say that for this piece you should create a boolean variable and set it to true on valid input. Here's the rewrite:

bool isValidInput = false;
			while(isValidInput == false)
			{
				if (year.Length == 4)
				{
					int newYearVal = Convert.ToInt32(year);
					Console.WriteLine(newYearVal / 1000);
					Console.WriteLine(newYearVal / 1000);
					Console.WriteLine(newYearVal / 1000);
					Console.WriteLine(newYearVal / 1000);
					Console.WriteLine("All Done.");

					isValidInput = true;
				}
				else Console.Write("Please enter a valid 4 digit year.\n (Ex: \"2000\"):");
				year = Console.In.ReadLine();
			}



Does that make sense? Does it seem clearer?
Was This Post Helpful? 0
  • +
  • -

#7 Hawkeye28   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 12-November 09

Re: Using the sizeof function

Posted 20 January 2010 - 09:12 PM

Yeah, it does. Much clearer, thanks I appriciate it!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1