I have been asked this questions before but this time i want to know the difference between the compiler of C++
and C#
.
C# coding of array
static void Main(string[] args)
{
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();
}
Output
C++ coding of array
int main()
{
clrscr();
int x[10];
int n;
cout << "Enter the array size= ";
cin >> n;
cout << "Enter the elements for array= ";
for(int i = 0; i < n; i++)
{
cin >> x[i];
}
for(int i = 0; i < n; i++)
{
cout << "x[" << i <<开发者_JAVA技巧 "]=" << x[i] << "\n";
}
getch();
return 0;
}
Output
Now here my question is this when m giving the same input for the C# then its asking for 4 elements to input and leave the 0 before any digit. But when m going with same input in C++ then its considering the 0 with any digit a separate input even m giving it with a digit and took less input then i entered.Even both languages follow the OOP approach. So what are the difference between both compiler. Why these are taking a lots of different input and generating different output.
One more that bother me that why the C++ compiler not reading 0 for last element and print the 7 but my input is 07 so according to its above output it should be 0 not 7.
This is a complete guess but the Borland C++ is trying to interpret a number with a leading 0 as octal (which is a standard convention like the '0x' prefix for hexadecimal). Since 08 isn't a valid octal number (only 0-7 are valid digit it octal) it is splitting it up into two inputs.
Try entering '010' and if the program prints out 8 you'll know it is interpreting anything with a leading zero as octal.
You could also trying to force cin to interpret your input as decimal by changing your input line to:
cin >> dec >> x[i];
First, it shouldn't be any surprise that using two different functions in two different languages that are meant to do the same thing will give you subtly different results. If you want to know what each of them does, look at the documentation.
More specifically, cin >> i
in C++ certainly does not act the same way as int.Parse(Console.ReadLine())
in C#. For example, the C++ version is able to read multiple numbers from the same line (delimited by spaces), while the C# version isn't.
But for your specific example, this looks either like a bug in the compiler you use, or it could be that the specification is unclear. If I compile your C++ code in Visual Studio, it behaves the same way as the C# version.
Also, I'm not sure what exactly did you mean, but your C++ code certainly does not “follow OOP approach”. (I'm not saying it should, just that it doesn't.)
精彩评论