Visual C+:+ .NET
(This tutorial is for .NET 2003 so the screen images will be different but the concepts will be the same)
You will be using a Microsoft product call Visual Studio .NET which contains a good C++ compiler in an IDE.
Microsoft Visual Studio's IDE (Integrated Development Environment) contains an editor (word processor for writing your programs, a C++ compiler to help you find errors in your source code, a linker to put together your code and built-in object files to make an executable file, a debugger to help you find errors during runtime, a help system to help you get help (!) and other tools.
Make sure you understand the concepts of compiler, linker and executable by the first test. That material will be covered here and in lectures.
You will be writing C++ source code - your programs.
In Visual Studio .NET, all programs must be part of a project.
A project is a folder that contains your source files and other files that .NET uses. Your .cpp files (and .h files if your are in cs1124) will be stored in a project.
Your first task whenever you want to write a new program in this course is to first create a new project for that program.
Each homework should have a separate project. Each lab recitation should have a separate project.Each lab test should have a separate project. You get the idea!
You should know how to find it by doing START|PROGRAMS... etc.
Do that now.
When you launch .NET, you should see the Start screen.
You did this in the previous Task so you should be looking at the Start screen now.
Select the New Project button.
Alternatively you can do File|New

You should see the New Project window.
In the lower left hand corner, if you see a More button, click it. If you see a Less button, go on.

Do NOT hit the OK button until all of the following are correct:

If all of these things are correct, hit the OK button.

You should see the Win32 Application Wizard rec01exam window. If you do not, get assistance from one of the lab workers.
Do NOT hit finish!!!!
Choose the Application Settings tab

Do NOT hit the OK button until all of the following are correct:
If all of these things are correct, hit the Finish button.
You should see the Start screen again. Notice that there are now entries in the Solution Explorer panel.

Now that you have created the project and the folders to place your program in, you need to open that project.
You may see the project you just created in the list of projects, if so you can click it to open it and skip over the next two steps.
The list should show all the projects that you had created and saved.
Opening a Project (an old one or one you just created above)
If you had previously created projects and you wanted to work on one of them some more, you would launch .NET again.
You would be looking at at the Start screen again
To open a previously created and saved project, following the directions from here.
If you see the project in the list, click it.
If not, click the Open Project button (you can also do File|Open|Project)
and choose the folder for your solution: rec01exam
You should see the Open Projects window again.
This time click the solution file: rec01exam.sln (sln for solution) and click Open button
If the Solution Explorer lists your project, then you have succeeded in open (or reopening) it.
Task 4: Describe the changes to the environment - Has the Solution Explorer changed?
You should now see your project name and see that the Solution Explorer - rec01exam panel now has Several listings under your solution name.
Notice that there are two tabs at the bottom of this panel: Solution Explorer and Class View.
Make sure the Solution Explorer tab is chosen by clicking it.
You will probably see Source Files, Header Files and Resource Files under your project folder.
(If you don't, click the + sign to the left of the folder with the project name.)

Task 5: What files should go in these folders and what files are in these folders right now?
You will probably see a + sign to the left of the folder icons labeled "Source Files", "Header Files", "Resource Files"
The + is Windows' way of saying a folder can be expanded to show the list of its current contents.
A - sign can be clicked to close an expanded folder.
(You can also double click the folder's icon to accomplish opening and closing.)
How many files are there? in these folders?
click on the '+'
None so far! --- Good.
In fact at this point the + sign probably disappeared meaning that there are no files in these directories.
Recall that in your project settings you chose and empty project so this is correct - empty.
(Why were there + signs to start with if the directories were empty? This is a Microsoft "feature." Get used to idiosyncrasies like this. There will be more to encounter!)
Task 6: Create a new file in which to write your program
Now we are ready to create out first C++ program.
You must create a source file for this.
All C++ programs are stored on disk as files. These are called source files. They are text files (also known as plain ASCII files) that you use a word processor to write and change. You could use Word to write source files as long as you were careful to always store them as text files. The language used to write your source code in is C++ and the name of C++ source files should have the extension .cpp. However you cannot run C++ source files. The computer does not understand C++. It understands only machine code. A piece of software called a compiler is built to translate or compile C++ into machine language. Word can't do this. Luckily for you, the Visual C++ .NET IDE's built in editor and a compiler that work together (and other things) to help you with programming. In the "olden days" you would have had to start working in an editor, save your work, close the editor, open a compiler, compile the file you saved, exit the compiler and then run the code!
If you are going back to work on a previously saved project that you have reopened, you can open the Source Files icon and double click on the .cpp file you want to edit. If this is what you are doing, skip to Task 7.
Projects hold source code.
You must ADD a file to your project.
Choose:
Project|Add New Item...

Do NOT hit the OK button until all of the following are correct:
Look at the Location: to make sure that it is correct and so that you can remember where you are placing this file.
If all of these things are correct, hit the Open button (yes, it should have been called Create!)

You can create C++ files outside of a project (by choosing File|New) but for this course do NOT do this.You can't compile files created this way. Be careful that you always make your source code part of your project by Project|Add New Item... or RIGHT-CLICKing on the project folder in the Solution Explorer window and choosing Add New Item... instead of File|New.
You should now be looking at the editor window with a tab named rec01exam.cpp that is blank. This is your source file.
It's empty so far. We'll be word processing in this window to create our first program shortly.
(If you do not see this, ask a lab worker for help.)
Look at the Solution Explorer - rec01exam panel.
The Source Files folder should be open and you should see your file's name there. (Open it if it isn't.)

Task 7: Write the C++ code to display the course designation on one line and your name on the next line
As you type notice some things.
--- The color of some of the words and symbols are different - this indicates that they have different meanings and uses in the C++ language.
--- When you hit the Enter key, the cursor will sometimes automatically go to a place that is indented on the next line. When you write C++ code you MUST correct thing of the style of the code. Indentation is important. Lucky for you .NET will often correctly indent your code. Look at the way our sample is indented. Your should be also.
For this Task, only type in the code. Don't do anything else yet.
Just get used to editing - word processing - something you'd better become good at quickly.
You don't have to know what all the following means yet either. Just type it in and wait.
#include <iostream>
using namespace std;
int main( )
{
cout << "cs1114" << endl;
cout << "Replace this with YOUR name!!" << endl;
return 0;
}
Note: the file name appears in the blue bar at the top of the .NET window. It has an * signifying that you have made changes to the file that have not yet been saved.
A hint: How good of a Windows' user and word processor are you? Did you copy and paste from the browser window to your project's editor window?
Choose File|Save and note that the * disappears from the top line.
You can also use the keyboard shortcut Control-S to save the file. This avoids using a pointing device and digging down into a menu structure, saving time as well as the file.
A note: if you choose File|Save As you have the option of changing the file name ... but be careful - after you have done this kind of save you are NOT editing your original any longer! You are editing the Saved-As file!
You should get in the habit of saving often. Any work not saved is totally lost if the power goes out or the machine hangs or some other disaster happens.
Task 9: Run the program? - NO - Compile it first
since you are working in an IDE you don't normally see all the steps required to create an executable program.
so far you have created the source file (.cpp) in a word processor. The next step is to create an object file (.obj) from your source code by compiling it. The final step is to link all needed object files into one executable file (.exe) which can be run (technically an executable program must be loaded before it can run). You can accomplish all these steps with one mouse click but you are required to know what's really going on since your program can have errors at either the compilation phase or the linking phase. The process of producing the executable file is sometimes called building.
Compile first
Programmers normally test their programs by compiling them first. When all compile-time errors are removed, they will only then attempt to run the program. The compiler will find syntactic errors - things that are not allowed by the C++ language. You should get in the good habit of compiling to check for errors first.
To compile your program:
Build|Compile
or
use the keyboard shortcut CONTROL-F7

Look carefully at the Output window.
This is where error messages and progress reports will be shown.
Always scroll to the top of the Output window. That's where to start reading. .NET (for some reason) does not do this automatically.
It might help to resize that panel so it is larger by reducing the size of the editing panel.
You should see Compiling... while the compiler is working.
You should rec01em - 0 error(s), 0 warning(s) if there were no errors.
Warnings are things that should be check on but are not fatal errors. An error means there's no way this program could run as it is now. All errors must be eliminated.
There will be some other messages in the Output window.
Since you either copied the program or were very careful to type everything exactly as shown above, you should have no errors.
If you got no errors then what the compiler created was an object file - rec01exam.obj.
This type of file cannot be run but it is guaranteed to have no syntax errors and that's one step closer to a complete program.
If you did get errors, ask a lab worker to help you fix the errors before you continue.
Task 10: Now that there are no compilation errors, Run the program - NO - Link next
Linking is a step in creating an executable file by putting together -linking - lots of object files to create an executable files (.exe). You created the object file of your program in the last Task. Now must join it together with some object code that is built in to the C++ environment (this object code is for doing low level things like working with the screen - very complicated code you won't be writing for a while).
There can be errors at link time.It's a good idea to check for these sorts of error before going on.
To link your program:
Build|Build Solution
or
Build|Build rec01exam
or
use the keyboard shortcut CONTROL-SHIFT-B
Again, look at the Output window as this process works.
You might see both Compiling... and Linking... If you see only Linking... that means that you had not recompiled your program before linking. If you had changed your source code and recompiled, you'd see both.
You should get no errors at this point. If you did, you'd read about them in the Output window.
If you do, ask a lab worker for help.
There should be no linkage errors.
Task 11: Now that there are no compilation errors and no linkage errors, Run the program - with Debug or not?
There are two ways to run a program inside the IDE that we will use.
One is called Debugging the program which allows you do execute each stop one at a time, observing what happens. You can decide after each step if what you told the computer to do was what should have been done by looking at the results of doing what you said. This gives you the most control and best helps you find errors. The kinds of errors you will hopefully find here are logical or programmer errors - meaning you solved the problem wrong to begin with!
The other way is to run the program without debugging. You should do this only after you have run the debugger enough times to have caught all the errors.
You will learn to use the Debugger in a later lab so for now pretend you have used the debugger and have eliminated
To run your program without debugging:
Debug|! Start Without Debugging
or
use the keyboard shortcut CONTROL-F5

You may be asked if you want to rebuild something things due to some things being out of date.
Say Yes.
Since we are creating Win32 CONSOLE applications (remember the Settings for the project), the interaction with the user will be in a window called the DOS or I/O (or sometimes confusingly the Output window).There will be a small C:\ in the upper left hand corner of this window. It will have a black background. This is where the output of your program will appear. This is where the keystrokes a user types will appear. This is the UI (user interface).
Even though it says "Press any key to continue" Don't. Go on to the next Task.
Again, Do NOT hit any key to continue.
Again, Do NOT hit any key to continue.
In Visual .NET, you can create or modify a program and go directly to the step of running the program. The IDE will compile, link and then run your program. You can just run the program if you want to but the normal programming practice is to compile first to check for errors. Running a program really means "let me check for my logic errors and to see what the output looks like." If you have compilation errors in your code, doing this is silly since the executable can't be created until a successful compile has taken place.
Task 12: Minimize and maximize the I/O window
If you did hit any key and the I/O window is gone, rerun your program (CONTROL-F5) to get it back.
Minimize the output window.
Sometimes you'll "lose" this window during an IDE session - check the bottom of your screen. It's probably down there!

Maximize the output (MS-DOS) window and go on to the next Task before closing this window.
But STILL DON'T press any key!
Task 13: Who caused the cs1114 and your name to appear on the screen?
If you did hit any key and the I/O window is gone, rerun your program (CONTROL-F5) to get it back.
YOU caused your name to appear in the output window? A program is a sequence of statements in which you tell the computer what to do. The lines that start with cout <<
are where you told the computer write these literal strings on the screen. The "stuff that's between double quotes" is a sliteral string,
Task 14: Who told the computer to write "Press any key to continue" on the screen?
You didn't.
The IDE puts this line on the screen and pauses execution until the user hits any key.
This helps you see what was written on the screen. Sometimes all the output flashes by and the I/O window closes before anyone has a chance to see anything.
Hitting Enter or "any key" really or clicking the X in the upper right hand corner of the I/O window will close it.
Task 16: Now try to intentionally produce an error - how many errors are there?
Remove the semicolon (the ";") from the end of the first output statement (make only this one change).
Remove this semicolon
|
V
using namespace std
Save the file and recompile. Do NOT simply run the program - recompile it:
Build|Compile
or
use the keyboard shortcut CONTROL-F7
The Output window has changed to Task list window. Notice that the Task list tab at the bottom of this window is now chosen.
This shows a list of all the errors in your program but it is wrong. How many errors does it say there are in your program?
It probably shows at least two lines of messages - again, this is wrong.
Click the Output tab at the bottom of this window. How many errors are there?
(You may need to scroll to the top of the Output window in order to see all the messages.)
It probably says there are 2 error(s)
Again, this is wrong.

How many errors are there in your program?
Either there are none and you are very happy or
THERE IS ALWAYS ONLY ONE ERROR.

Even if the IDE claims there are more, there's only one.
This is became the first most likely causes all the rest.
If you attempt to fix any error other than the very first one, you are wasting your time.
A rule: NEVER DEAL WITH ANY ERROR OTHER THAN THE FIRST ONE!
Read the first error message. You will have to start learning what these error messages mean.
It is a TERRIBLE IDEA to just start changing things in your code and recompiling until it has no more compilation errors.
Make sure you have the Task list tab selected then read the first error message.
Resize the Description column so that you can see more of the error's description. You can make this column wider by dragging line separating the Description and File to the right.
s
error C2114 is pretty meaningless but " 'int' should be preceded by ';' " is something that can help you identify the error: something about 'int' and a ';' is wrong.
Look at the line number in the Line column. It says that this error is on line 5 in your source code. Your error may actually somewhere before this point in your code.
(The line numbers shown for your program may be a little different.)
Since this is an IDE you have a very good way of working. Whenever you double click on an error message in the Task list window, you will be moved to the line shown in the editor window and an arrow will show you where you are.
Do that now. Double click on the top error message. (Recall that double clicking on any other error shown is futile. There is only one error!)

Somewhere on or before this line of code is where the actual error is.
Re-read the error message to see if you can figure it out.
" 'int' should be preceded by ';' "
Aha! There is a missing semicolon on line 4 of this code.
When you are debugging, you should read the first error message and try understand what the IDE is suggesting is the problem.
Often something before the error point is causing a different error but often the message is correct.
Remember the rule: NEVER DEAL WITH ANY ERROR OTHER THAN THE FIRST ONE!
Read only the first error message (at the top of the list), double click it, reread the error message, fix it, save, recompile.
Do this process over and over until you've a got a program with no compile time errors
you'll also see the message "Error executing cl.exe." which means that the environment was unable to execute your code because of the error
remember a quick way to find out where the compiler thinks the error is is to double click on the first error message (after reading it, of course) and the environment will place the cursor inside your source code at the error point and shows a blue arrow pointing to the line
A BIG NOTE:
Just because a program compiles does not make it correct.
It must link and run and it must solve the problem assigned, have a good user interface, be written in good style to be considered a good program.
Successful compilation is only one tiny step towards getting an A for your work!
So the pattern is
edit
save (CTRL-S)
compile CTRL-F7
errors? yes do it again
when there are no more errors continue to the next Task.
Task 17: Correct any runtime errors or logic errors or bad UI errors and rerun the program
Are there any problems? Does your name appear on the screen?
That was the assignment!
Fix this in the edit panel.
Now what are the steps? - you've edited your program so you are back to:
edit
save (CTRL-S)
compile CTRL-F7
errors? yes do it again
when no errors, run
is what's in the UI correct? look good?
if not, do the whole process again
when there are no more errors, you are finished with Step 0.
Do Step 1 , Step 2, and Step 2...Continued
Did you compile first or did you just run your program?
Hopefully you followed the suggestions at the end of the previous Task, compiling first.
Here's a recap, with more detail, of How to work:
Task 18: What files have you created now?
Use the Windows Explorer to search for the pattern rec01exam.*
* is called a wildcard character so this means to search for any files matching rec01exam no matter what the extension is.
(Note that Windows Explorer is not Internet Explorer!)
There should be a rec01exam.cpp
This is the source file. Look at its Type.
There should be a rec01exam.exe in the Debug folder in your project folder.
This is the executable. Its Type is Application.
Double click the icon for this file and see what happens.
Did you see it?
Yes, an I/O window was opened, the output was displayed but the Press any key to continue was not and the window immediately closed without waiting. You'll learn how to keep this window on the screen longer if you need to in a later recitation.
There may or may not be a rec01exam.obj because the IDE doesn't keep that around always. If it is there it will be in the Debug folder inside your project folder.

Using the Windows Explorer will help you if you cannot remember where your files are when it comes time to upload things to utopia.
Task 19: What changes occurred in the Class View panel?
At the bottom of the Solution Explorer panel, click the Class View tab.
This shows a very convenient way to move around in your code when it starts getting pretty big.
Open the rec01exam folder if it isn't already open.
Open Global Functions and Variables if it isn't already open.
You should notice that it contains a main() function.
This is the function you wrote in your program.Yes, you wrote a function!
It should look like main() in your code but because we are using .NET in the Class View it will look like: main(void).
Task 20: What happens if you double click on the main() function with the diamond to its left?
In the Class View panel, you can go directly to a function by double clicking. When you are writing programs with dozens or even hundreds of functions, this becomes a a good way to move around.
Double click on the small icon to the left of main(void) in the Class View panel and watch the cursor move into the editing window on the line where the definition of the main() function is.

Task 21: Do we need to keep all these files?
No.
with the exception of the .exe file, the Debug subfolder contains intermediate files used by the Visual C++ environment to provide help in finding errors in your code
you do not need to keep any of the files in the Debug subfolder, Visual C++ will recreate them for you
nor do you need all of the other files
you DO NEED the source code files, however.
at this point that only includes the .cpp file
it may be simpler however, to keep all the files other than the Debug subfolder for the time being
Task 22: Take the lab exam and submit it
the lab exam today is really about learning how to use the handin system
normally it will be projected on the front board using an overhead projector but today it's online for the last time
earlier in this recitation you checked that you could telnet into utopia. to have the test graded you'll need to upload your .cpp source file to your utopia account and then submit it to the handin system there.
the exam is shown below. it consists of only one question.
take Recitation Exam 1 now and then come back to the next task
For today's lab, there is only one problem to solve:
Create a project called "rec01exam" (you should always create a new project for each lab exam.)
Write a program called "rec01exam.cpp" which displays the following information (appropriately modified):
CS1114
Programmer: John Q. Student
(You've just done this!)
Now...
Submit your program via handin for grading.