This page shows you the steps required to write, compile, run, and test a program.
The outline is pretty simple:
- Login to the system
- Make a folder for the project
- Figure out a procedure to solve the problem
- Write the source code
- Compile the source code (fix errors)
- Run the program
- Test the program
- Correct the errors
The following sections walk you through this process for a simple program to print the sum of two numbers.
a. Login
Use ssh
or puTTY
to connect
to homework.cs.tufts.edu
using the appropriate
software.
b. Make a Folder
Once logged in, make a folder under your cs11
folder. In this case, we will be making a folder called
starter
.:
If you did not call your foldercd cs11 mkdir starter cd starter
cs11
, you need to use
the correct name in that first command. Open your cs11 folder in
Atom. In the Project column, right click on cs11. Then,
got to Remote Sync PRO > Download Folder.
c. Figure Out a Procedure to Solve the Problem
We're not going to give you any guidance on this now. You'll get lots of practice over the term. For now, I'll assume we've done this. Do not try to do this and write source code at the same time!d. Write Some Source Code
In Atom, right click on your starter folder, and select New
File. Enter the name adder.cpp
and press the return key.
Now there will be an empty file, giving you a place to type in your code.
Type in this code:
The indenting does not matter to the computer, but it does matter to humans, like your graders and your instructor. Indent it as shown (there are 8 spaces for each level of indentation in the example above).// // adder.cpp // purpose: add two numbers // by: your name here // date: the date here // #include <iostream> using namespace std; int main() { double a, b, sum; cout << "Enter two numbers: "; cin >> a; cin >> b; sum = a + b; cout << "The sum is " << sum << endl; return 0; }
Now save your work by pressing command-S
for Mac or
Ctrl-S
for Windows (the same way you would save a
Word Document or any equivalent). This will upload your
adder.cpp
file to the Halligan servers. If you go to your
terminal or puTTY application and type ls
, you should see
the file adder.cpp
listed within the current directory.
If you make any changes to the file, just save it again in the same
way and the changes will be uploaded to the Halligan server.
e. Compile
To translate your C++ source code to an app that can run on the computer, you need to compile the source code. Type:This command runs theg++ -o adder -Wall -Wextra -Werror adder.cpp
g++
compiler on the
source file called adder.cpp
asking for all warnings
(-Wall
), even extra warnings (-Wextra
), and to turn all warnings into errors (-Werror
).
If things go well, there will be no messages printed.
If you made any typing mistakes, the compiler will print out its complaints. If there are complaints, then go back to step the previous step and make the needed changes.
f. Run
To run this program, type
which means to run the program named./adder
adder
, which can be
found in the current directory (.
).
g. Test
Now the program is waiting for the two numbers. Type in two numbers separated by a space:
The output should show that the sum is 12.55.2 7.3
h. Correct Errors
If the program contains any logic errors, the result will not be correct. If this happens, first, think about what happened. Trace the code based on the input followin the code precisely (not what you think should happen, but what actually happens!). Then go back to the editing step and make changes to correct the error.
That's It
The most difficult parts of using computers to solve problems are:
- figuring out the procedure
- translating that procedure into steps the computer can perform
- testing and debuggin a program