This Code

  • Thread starter Thread starter WELCOME ###
  • Start date Start date
W

WELCOME ###

This is code done by (Type:C++, and CLR console template).
The code built well, but NO output.
Please can any body help:
The code is:
#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)

{

array<int>^ datta = {10};

int i;

for(i=0; i<10; i++)

datta=2*i;

Console::Write(L"{0,5}",datta);

Console::WriteLine();

return 0;

}

=======================================
 
Hello Welcome,

You code is a bit novice. Is this a homework assignment?

WELCOME ### said:
This is code done by (Type:C++, and CLR console template).
The code built well, but NO output.
Please can any body help:
The code is:
#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)

{

array<int>^ datta = {10};

you have declared a zero-based integer array. the valid index values are
zero through nine.
int i;

for(i=0; i<10; i++)

datta=2*i;


here, you travel from zero through nine, assigning values to the
cooresponding locations in the array. The loop index variable (i) starts at
zero and increments by one. When the loop index (i) reaches 10, the loop
stops. At the end of the process, the index variable is holding the value
(10).
Console::Write(L"{0,5}",datta);


you attempt to write the 10th value of the array. There IS NO 10th value in
the array. An error occurs. No output is produced.
Console::WriteLine();

You never get this far. During default error handling, the application
terminates at the previous Write statement with "index out of bounds".
return 0;

}

=======================================

Did you learn from that? I hope so.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
 
Hey. Programming is my hobby. So how can I fix this
to find the output?
++++++++++++++++++++++++++++++
Nick Malik said:
Hello Welcome,

You code is a bit novice. Is this a homework assignment?

WELCOME ### said:
This is code done by (Type:C++, and CLR console template).
The code built well, but NO output.
Please can any body help:
The code is:
#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)

{

array<int>^ datta = {10};

you have declared a zero-based integer array. the valid index values are
zero through nine.
int i;

for(i=0; i<10; i++)

datta=2*i;


here, you travel from zero through nine, assigning values to the
cooresponding locations in the array. The loop index variable (i) starts
at zero and increments by one. When the loop index (i) reaches 10, the
loop stops. At the end of the process, the index variable is holding the
value (10).
Console::Write(L"{0,5}",datta);


you attempt to write the 10th value of the array. There IS NO 10th value
in the array. An error occurs. No output is produced.
Console::WriteLine();

You never get this far. During default error handling, the application
terminates at the previous Write statement with "index out of bounds".
return 0;

}

=======================================

Did you learn from that? I hope so.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
 
Console::Write(L"{0,5}",datta);

I'm not sure why this builds, the "L" token is a mystery to me. There are
several components to Composite formatting, the first of which is generally
a string. It can be an IFormatProvider instance, but I don't believe the
token "L" is such, unless you've defined it as such elsewhere. The string to
be formatted may contain multiple format items. A format item consists of
opening and closing curly brackets containing at least an index, with an
optional alignment component (in your case, the integer 5), which specifies
the preferred width of the string value of the object which the index
represents. The alignment integer is positive, the alignment is right; if
negative, the alignment is left. There is also an additional colon (":") in
the format item, which would be followed by a format string. All of these
would be contained within the string. The "L" token preceding the string is
therefore meaningless. As to why it compiled, I have no idea. It should not
have, but it is possible to confuse the compiler on rare occasions.

--
HTH,

Kevin Spencer
Microsoft MVP
Bit Player
http://unclechutney.blogspot.com

Where there's a Will, there's a William.
 
Thanks Larry,

I've been a bit busy. As you may have guessed, I don't use C++ much these
days.

If the OP is still around, I believeI see the problem. See below:
array<int>^ datta = {10};

int i;

for(i=0; i<10; i++)

datta=2*i;

Console::Write(L"{0,5}",datta);

Console::WriteLine();


He started by declaring a managed array, which is equivalent to the managed
System.Array data type. He also initialized it without declaring a length.
If I understand correctly, this resulted in an array of one element. This
would make the size of the array immutable. So, again, I can't see why this
didn't throw any exceptions, but again, I am not very sharp with C++ these
days.

--
HTH,

Kevin Spencer
Microsoft MVP
Bit Player
http://unclechutney.blogspot.com

Where there's a Will, there's a William.


Larry Lard said:
Kevin said:
Console::Write(L"{0,5}",datta);


I'm not sure why this builds, the "L" token is a mystery to me.


It's the C++ way of indicating that a string literal is made up of wide
characters.



--
Larry Lard
(e-mail address removed)
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
 
Back
Top