none
declare/initialize int?

    Question

  • Hello,

    I wish to have an integer inumber to use as the index in

    myArray[inumber]

    How do I declare/initialize this value?

    Also can I have something similar to the C# code of

    private string[] results;
    results = new string[myArray.Length];

    Thanks

    Geoff
    Wednesday, August 17, 2005 2:41 PM

Answers

  • Geoff: it is probaly my fault but it is still not clear to me exactly what you are asking. I don't know if it is of any help but I would translate the above C# code as follows:

    // data members
    private:
       array<String^>^ myQuestion;
       array<String^>^ results;
       int qnumber;

    // constructor
    MyClass::MyClass()
    {
       myQuestion = gcnew array<String^>(2);
       myQuestion[0] = "question 1";
       myQuestion[1] = "question 2";

       results = gcnew array<String^>(myQuestion->Length);
       qnumber = 0;
    }

    // initialize label
    this->label->Text = myQuestion[qnumber];

    // record answer and update label
    results[qnumber] = trackBar->Value->ToString();
    ++qnumber;
    this->label->Text = myQuestion[qnumber];

    Wednesday, August 17, 2005 6:26 PM

All replies

  • Hi Geoff: I'm not sure I 100% understand your question.

    In C++ (and C) there is no built-in way in the language to get the size of an array; you either need to use the size that the array was declared with or use one of the many language "tricks" that will give you the size of an array.

    Given an array declaration like:

    const int SIZE = 10;

    int myArray[SIZE];


    I would iterate through the elements of the array using one of the following snippets:

    for (int k = 0; k < SIZE; ++k)
    {
       myArray[k] = ...
    }

    for (int k = 0; k < sizeof(myArray)/sizeof(myArray[0]); ++k)
    {
       myArray[k] = ...
    }

    #include <stdlib.h>

    for (int k = 0; k < _countof(array); ++k)
    {
       myArray[k] = ...
    }


    _countof is a macro that gives you the number of elements in an array. It is defined in stdlib.h

    Wednesday, August 17, 2005 3:18 PM
  • Jonathan,

    I may be asking the wrong question!

    In my app I am aiming to put the trackbar position value into an array each time the user clicks on  button to move to the next question where he/she will be asked to position the trackbar again as a response to the question.

    In Visual C# I have

    private string[] myQuestion;
    private string[] results;
    private int qnumber = 0;

    myQuestion = new string[]{"question 1","question 2"};
    results = new string[myQuestion.Length];

    Then I position the first question

    this.label.Text = myQuestion[qnumber];

    When the button is clicked the trackbar value is put into the results array, qnumber is increased by 1 and a new question is display.

    results[qnumber] = trackBar.Value.ToString();
    ++qnumber;
    this.label.Text = myQuestion[qnumber];

    after the last question all the members of the results array are written to a file.

    Is this any clearer?!

    Cheers

    Geoff


    Wednesday, August 17, 2005 3:43 PM
  • Geoff: it is probaly my fault but it is still not clear to me exactly what you are asking. I don't know if it is of any help but I would translate the above C# code as follows:

    // data members
    private:
       array<String^>^ myQuestion;
       array<String^>^ results;
       int qnumber;

    // constructor
    MyClass::MyClass()
    {
       myQuestion = gcnew array<String^>(2);
       myQuestion[0] = "question 1";
       myQuestion[1] = "question 2";

       results = gcnew array<String^>(myQuestion->Length);
       qnumber = 0;
    }

    // initialize label
    this->label->Text = myQuestion[qnumber];

    // record answer and update label
    results[qnumber] = trackBar->Value->ToString();
    ++qnumber;
    this->label->Text = myQuestion[qnumber];

    Wednesday, August 17, 2005 6:26 PM
  • Jonathan,

    Many thanks for your suggestions - I will come back to them in a minute as although I have got that part of the code to work I am not sure why so will look carefully at your code  ...

    I am being held up at the moment as not sure how to write to a file. In C# I would use

    TextWriter tw = new StreamWriter("d:\\path\\file.txt");

    tw.WriteLine("fred");

    tw.Close;

    What is the C++ equivalent for Visual C++ ?

    heers

    Geoff

    Wednesday, August 17, 2005 7:15 PM
  • When it comes to writing managed code with C++ the code is remarkably similar to the equivalent C# code. One of the biggest differences is that C++ keeps the syntactic difference in referring to an object on the stack and an onject on the heap.

    So the code above would be translated to:

    TextWriter^ tw = gcnew StreamWriter("D:\\path\\file.txt");

    tw->Write("barney");
    tw->Close();


    As you can see the code is remarkably similar: the major difference is the use of ^ and -> and these are used because the StreamWriter object is created on the GC heap (via gcnew).
    Wednesday, August 17, 2005 11:13 PM
  • Jonathan,

    Thanks for the TextWriter code. I must be nearly there but getting a couple of error messages.

    line 155   error C2227 left of '->ToString' must point to class/struct/union/generic type
    line 181   error C2228 left of '.Close' must have class/struct/union

    I have following for initialization which I think is OK.

    private: static array<String^>^ LHSquestions = gcnew array<String^> {"question 1", "question 2"};

    private: static array<String^>^ RHSquestions = gcnew array<String^> {"question 1", "question 2"};

    private: static int qnumber = 0;

    private: static array<String^>^ results = gcnew array<String^> [LHSquestions->Length];



    and for the first error message

    this->results[qnumber] = trackBar1->Value->ToString(); // line 155



    and for the second error message

    TextWriter^ outfile = gcnew StreamWriter("h:\\a-temp1\\data.txt");

    for (int i=0; i < LHSquestions->Length; i++) {

     outfile->WriteLine(resultsIdea);

    }

    outfile.Close();  // line 188


    If this not clear I can post the whole code!

    Cheers

    Geoff

    Thursday, August 18, 2005 6:47 AM
  • Jonathan,

    re my message with the 2 errors - have found the cause of the 2nd one - I had outfile.Close() instead of outfile->Close()

    also found error in the other code but still get 2 similar error messages

    results[qnumber] = trackBar1_Scroll->Value->ToString();

    (I had trackBar1 not trackBar1_Scroll)

    error messages re above

    error C2227: left of '->Value' must point to class/struct/union/generic type
    error C2227: left of '->ToString' must point to class/struct/union/generic type

    thought?

    Cheers

    Geoff

    Thursday, August 18, 2005 7:47 AM
  • Geoff: this looks like you have a value-type instead of a reference type: in this case you need to use '.' to access the members instead of '->'
    Thursday, August 18, 2005 1:57 PM
  • Jonathan,

    I'm going round in circles with this one! When I look at the Events properties for the Track Bar I see that the name to the right of the Scroll behaviour property is trackBar1_Scroll so if I just wish to print out this value, forgetting putting it into an array for the moment, what would I use?

    Is there an equivalent to the C# MessageBox.Show? Or how do I print to the Console?

    Cheers

    Geoff




    Thursday, August 18, 2005 2:35 PM
  • Jonathan,

    having gone back a step and just used Console::WriteLine("fred"); to ceck the correct way to print out anything (!) I am now getting an error from earlier in the code.

    I have

    private: static array<String^>^ LHSquestions = gcnew array<String^> {"question 1", "question 2"};

    private: static array<String^>^ RHSquestions = gcnew array<String^> {"question 1", "question 2"};

    private: static int qnumber = 0;

    private: static array<String^>^ results = gcnew array<String^> [LHSquestions->Length];

    and the last line is intended to create the results array with the same number of elements as in LHSquestions - this seems to be wrong. Am I able to do this?

    Cheers

    Geoff

    Thursday, August 18, 2005 2:43 PM
  • Yes: you are able to do this but the last line should be:

    private: static array<String^>^ results = gcnew array<String^> (LHSquestions->Length);

    Thursday, August 18, 2005 5:49 PM
  • Jonathan,

    I got there just before your message - largely by trying anything I could think of!

    Thanks

    Geoff

    Thursday, August 18, 2005 5:55 PM