PDA

View Full Version : Solved: Visual C++: Loops Question



CreganTur
11-25-2008, 01:28 PM
When I try to debug the code below I get the following error message:
"error C2061: syntax error : identifier 'intCounter'"
The error points to the line of code with the while keyword.

What am I doing wrong with this do...while loop structure?


private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
Int16 intCounter = 0;
Int16 intArray[] = {1,2,3,4,5};
do
{
String^ strMsg = gcnew String(intArray[intCounter].ToString());
MessageBox::Show(strMsg);
}while intCounter < 5;

}

Tommy
11-25-2008, 02:55 PM
As stated in an earlier post my C++ is very rusty. Are you incrementing the intCounter?
while (intCounter < 5);

CreganTur
11-26-2008, 06:52 AM
Are you incrementing the intCounter?
Yes, I am... I just neglected to include it.

I can't believe the solution was as simple as encasing the parameter of the 'while' keyword in parentheses!

Here's the working code:


private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
Int16 intCounter = 0;
Int16 intArray[] = {1,2,3,4,5};
do
{
String^ strMsg = gcnew String(intArray[intCounter].ToString());
MessageBox::Show(strMsg);
intCounter ++;
}while (intCounter < 5);
}

malik641
12-05-2008, 03:50 PM
...I can't believe the solution was as simple as encasing the parameter of the 'while' keyword in parentheses!...
Yeah, get used to that. C++ is not as flexible as VB/VBA. IF statements, method calls, and anything else where you think should have parenthesis, should have parenthesis.

CreganTur
12-08-2008, 07:47 PM
Yeah, get used to that. C++ is not as flexible as VB/VBA. IF statements, method calls, and anything else where you think should have parenthesis, should have parenthesis.

Yeah, I'm finding this out the hard way. I dl'ed the C++ for Beginners book that Microsoft is hosting on MSDN, so I'm learning about the Visual C++ Command Line and CPP files. I just wish that they had the same intellisense as Visual C++

malik641
12-08-2008, 08:06 PM
Can I ask why you are developing in VC++'s command prompt?

I'm assuming you type in "edit newFile.cpp" and you create your code in there.

CreganTur
12-10-2008, 07:39 AM
yes, I do. Then I use "cl /EHsc [filename].cpp" to compile it.

I'm only doing this because the book I mentioned from MSDN is C++, not Visual C++, so all of its examples are .cpp files.

I've got another book that is specifically for Visual C++, but I'm holding back on it until I get a better grasp of regular old C++

malik641
12-10-2008, 08:00 AM
I see. If it helps, you can program cpp files in Visual C++. Just open VC++ and choose new project --> win32 --> Windows Console App --> In the application settings be sure to select "Empty Project" --> Finish. Then Add new Item --> Code --> Cpp file.

From there you can create C++ code (the visual part is basically reserved for userforms) that has a cpp file with the code in it. You can still compile the cpp file using the VC++ Command Line if you want, but at least you can create the code and have Intellisense to help you.

Hope this helps!

CreganTur
12-11-2008, 06:31 AM
I do that already- write the code in Visual C++ and use the command line to compile. It just seems like the intellisense for CPP files isn't as robust as it is for UserForms.