0
int n;
 int[] ar = new int[50];
 Console.Write("Enter the size of array= ");
 n = int.Parse(Console.ReadLine());
 for (int i = 0; i < n; i++)
 {
 ar[i] = int.Parse(Console.ReadLine());
 }
 for (int i = 0; i < n; i++)
 {
 Console.WriteLine("AR["+i+"]="+ar[i]);
 }
 Console.Read();

enter image description here

As here you can see that when m entering the 09 or 08 its going to remove it and print the 9 and 8. And when same run on the c++ compiler then its print the 0 and 9 on different indices, why the compilers of both language doing behave like this? Why they do not reading it one digit?

Nathan
6,2244 gold badges38 silver badges66 bronze badges
asked Jun 16, 2011 at 6:58
4
  • It's hard to know what's happening when you haven't shown any code... and it's hard to see what the compiler has to do with this when the numbers are being entered at execution time. Commented Jun 16, 2011 at 7:00
  • What data type does your array have? How do you read from the command line? Seeing some code would be helpful. Commented Jun 16, 2011 at 7:00
  • i edited the question and give my code sample.... Commented Jun 16, 2011 at 7:01
  • 1
    well you did say parser sow it ignores the first 0 because ther is no integer that starts whit 0 Commented Jun 16, 2011 at 7:20

5 Answers 5

10

The behavior of int.Parse(..) and other string to numeric parsing functions is to strip out any leading characters (zeros in this case) which are irrelevant to the numeric value.

If you wish to keep the leading zero then change the data type of your array to string.

Now that you've posted some code, here's a few suggestions (comments inline)

 int n;
 bool validNumber;
 do {
 validNumber = true;
 Console.Write("Enter the size of array:");
 if(!int.TryParse(Console.ReadLine(), out n)) // use TryParse instead of Parse to avoid formatting exceptions
 {
 Console.WriteLine("Not a number, please try again");
 validNumber = false;
 }
 } while(!validNumber); // re-prompt if an invalid number has been entered
 int[] ar = new int[n]; // declare the array after the size is entered - your code will break if the user enters a number greater than 50
 // change to "string[] ar = new string[n];" if you want to keep the full value entered
 for (int i = 0; i < n; i++)
 {
 bool valid;
 do {
 valid = true;
 int num;
 string value = Console.ReadLine();
 if(int.TryParse(value, out num)) // again - use tryparse.
 ar[i] = num; // change to "ar[i] = value;" if you're using a string array. 
 else {
 Console.WriteLine("Please enter that again - not a number");
 valid = false;
 }
 } while(!valid);
 }
 for (int i = 0; i < n; i++)
 {
 Console.WriteLine("AR["+i+"]="+ar[i]);
 }
 Console.Read();
answered Jun 16, 2011 at 7:01
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this nice example and answer. Can you explain about c++ stackoverflow.com/questions/6323885/array-initialization-in-c thanks?
5

Your array is storing integers, and since 01 and 1 have the same number value, we don't distinguish them.

You'll want to save them as strings, if you need to keep the prefix zeros.

answered Jun 16, 2011 at 7:02

3 Comments

Ok sir i assume that c# compiler have the same value as 01 and 1 but what about the c++ why it print it on different indices? check my this question stackoverflow.com/questions/6323885/array-initialization-in-c thanks.
I don't know much about C++'s standard behavior, but there you're putting strings into an integer array again, except without parsing them as integers first. Probably something with char's and their integer values you're hitting there.
A good advice would be to always know the datatype of your variables. In the C++ case, check what stdin gives you and make sure you handle the cast to whatever you're assigning that output to correctly.
4

An integer is an integer, not a string.

So 09 is the same as 9, is the same as 000000009. They are all internally represented by the bit pattern 00000101 00000000 00000000 00000000 (at least on x86).

answered Jun 16, 2011 at 7:04

Comments

0

i think its better if you convert your array to string array and then perfrom the operation. Since when it comes to integer 09 and 9 has no difference the numeric values is what that matters. but when its a string its different so use string array to do the operation and if u have to just want to display it then string is better anyways. if u want integer then convert it into int, there are API available.

answered Jun 16, 2011 at 7:13

Comments

-1

Console.Read*Line*() is different from cin>> . so

answered Jun 16, 2011 at 8:00

1 Comment

-1: This does not answer the question. cin is C++, not C#.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.