Goals (worth 15 in-class points)- to compile and execute a C++ program- to modify a program to enhance it - to try out the command line emacs editor - to redirect standard input and output OverviewThis activity will introduce you to the C++ compiler and two versions of emacs as well as the Unix command line. These are some of the tools you will be using in this course. You will type in the C++ program shown using the graphical version of emacs, compile it, and execute it. You will submit that program. Then, you will copy that program, make some changes to it using the command line version of emacs, and compile it. You will submit only the first program.Creating C++ program (hello.cpp)Create C++ Source File in a folder using the terminal:
Open a terminal window. The prompt you see should show that you are at the top
level of your files. If you haven't done so already, create a folder for your
CS362 classwork.
You could type Create a folder for this hello activity and move into it. |
// greet names entered by user
// your name - this month
#include <iostream>
#include <string>
using namespace std;
// prints hello to person
void printHello(const string & person);
int main( ) {
string name;
cout << "Type control-d to stop."<< endl;
while (cout << "Enter your name ", cin >> name) {
printHello(name);
}
return 0;
}
void printHello(const string & person) {
cout << "Hello, " << person << endl;
}
|
For this first part, we'll use the more graphical emacs that is on one of the menus. Create a new file using the emacs editor, name it hello.cpp, and save it in the hello folder you created above. Now there should be some new menus available in emacs including C++, Tools, and Options. Be sure Options/SyntaxHighlighting is on. The button should look pushed in. Emacs knows about C++ and the default style is two spaces of indenting. You may press spaces or tabs in emacs to indent your lines.
Type in the C++ program at above right. Include your name in the comments. Get into the habit of indenting the program with the #include directives at the left margin but statements nested within constructs indented two spaces.
The editor will highlight the special C++ words such as include, int and return. As you learn more about C++ and this highlighting, you will recognize when the code has a bug even before you compile.
Save the file, but leave it open in the editor.
The Translation Process:
You can read your C++ source program, but the computer needs it in a form it can execute.
You must translate it from its current language
into an executable language.
This translation process is shown in the picture below.
For most of this course, we will use the term compiler and
the c++ command
to refer to the combined compiler and linker.
Compile in emacs:
We can access the c++ compiler through emacs or the terminal.
The same command works the same way in either place.
The compiling command option we use turns on all warning messages and
saves the executable program in an a.out file.
In emacs, choose Tools/Compile and note that the bottom
line now reads
Compile command: make -k
But we don't need the make part, so you should backspace over that back
to the colon(:) and type
c++ -Wall -g hello.cpp
and press return.
That command tells your computer to run your source program (hello.cpp) through the c++ compiler. It asks that all warnings (-Wall means Warnings all) as well as errors be reported. That is the option I will be using, so you want to see all the warnings and fix the problems so that I don't see them when grading. The -g option asks to generate debugging information so that we can use the debugger. You don't have to use it now. We didn't provide a name for the executable program that results; therefore, it is placed in the default file, a.out. If you were doing this in the terminal, you would just type the part in bold (starting with c++).
When you compile in emacs, it splits the window and writes compiling information and error messages in the bottom one. If the translation was successful, you'll get a message saying something like Compilation finished. If it was unsuccessful, read the error messages and warnings. Each one suggests what is wrong and on which line. Sometimes the mistake is on the line above the one mentioned. The current line number is shown in the bar below your program text in emacs. You can also choose Edit/Go to and then Go to line and type in the line.
Fix the first mistake first. Check to see if you made similar ones and correct those as well. But sometimes one little mistake will cause a great many error messages. Try commenting out the using namespace line, saving, and compiling. So don't try to fix all of the bugs before compiling again. Compile again by repeating Tools/Compile (or in the terminal, press the up arrow key to get to previous commands and press return). Your c++ command should be there. Emacs will ask if you want to save the file. But if you are compiling in the terminal, you have to remember to save. This is one of those advantages to compiling inside emacs.
Keep fixing problems and compiling until you have no errors or warnings. If you are compiling in the terminal, you will just get the prompt back without a Compilation finished message.
Be in Correct Folder to Execute:
Move to the terminal window. You need to be in the directory where
you just compiled your program. That directory will now have more files.
Does the prompt show that you are in the correct directory?
If not, use cd to get there.
Type ls -l (those are the letter l)
to see a listing of the files in your folder. hello.cpp should be there and should be bigger than zero characters. The time stamp should be recent. You should also have an a.out file that contains your
translated and linked program.
To execute your program, type ./a.out and press return.
The dot at the front says to look in the current directory for this file.
What do you expect it to print? Does it? Is everything spelled correctly?
If not, fix the program, compile it again, and execute it again
until it is correct.
Submit your Program:
In the terminal, use submit and think about your answers to give your source program (not your executable program)
to my katz362 account as the hello assignment.
Note that the name to use for submitting is usually at the top of the lab handout.
Always be in the directory where you compiled and executed your program when you submit.
Create a New Program from an Existing Program:
In the terminal while in the folder for this activity, type
cp hello.cpp hello2.cpp
This copies the contents of the original file to a new file with a different name.
There are spaces between the three words.
Open new program with command line emacs:
In the terminal, type emacs hello2.cpp
This should open the new file in a emacs inside the terminal.
In this version of emacs, the mouse will not be helpful. Use the arrow keys.
Make a Few Small Changes: Edit hello2.cpp. Check to be sure that you are changing the correct file. Change the output statement so that it prints I am followed by your name. Save by typing control-x then control-s.
Compile and Execute the Program: Compile hello2.cpp by typing control-c and then c (or escape-x and compile). Be sure to put its name in the compiling command. To switch between the two windows (move to other window) use control-x then o. After a successful compile, execute it in the terminal. Because we are using the default a.out file, a.out's old contents will be replaced by the new executable. That's okay. That's just an executable program you can recreate by compiling and linking. Does your program do what you expected?
Create an input text file: Use either version of emacs to create a file named input.txt. You can use control-x then control-f to 'find' a new file. Put some names in the file and save it.
Redirect input to come from that file:
On the command line, type ./a.out < input.txt
Redirect output to go into a file:
On the command line, type ./a.out > output.txt
But this isn't very helpful because you don't see the output statements so that you know to
type any data. So combine these latest two steps.
When you are done, quit emacs with control-x then control-c. This ends the lab activity. Check with submitlog to see that you have submitted your hello program.
c++ -Wall -g hello.cpp
./a.out to run the executable code
For 25 points extended project credit complete this program by 5pm Friday, September 11th: Write a complete C++ program that reads in a positive integer (int) and a character (char). If the integer is less than 1 or greater than 50, an error message is printed and the program stops. Otherwise, the program prints the character as many times as was indicated by the integer all on one line, ends the line, and ends the program. You must use a function with two parameters to print the normal output. You must use a prototype for that function. Include appropriate comments. Submit as repeat. Program must end in .cpp. There is no reason to use a vector or a class for this program. Think simple. Test your program with several test cases to try to uncover mistakes.