Using CodeWarrior
In this, you find out how to use CodeWarrior to create a program from generated source code. Source code is text that represents a specific set of instructions that a computer must follow. Source code is not written in English. It is written in a programming language. CodeWarrior makes creating a program easy work because it uses an Integrated Development Environment (IDE). IDE enables you to use a common graphical interface for your compiler, file browser, settings, and source code editor (the window in which you edit and view source code). For example, when we started programming, we used a free C++ compiler that required us to enter all the settings for a program at the DOS prompt. Doing so was time consuming. The IDE takes care of all your project and file settings, which makes programming faster and easier. It’s time to start your quest into the world of programming. As we explain how to create a project with CodeWarrior and present the code, try the information on your computer. Practice makes the unfamiliar seem natural. Creating a New Project The first time you open CodeWarrior, it will appear as shown in Figure. As you can see, nothing magic is going on (well, maybe just a little). CodeWarrior is simply an application, as are Microsoft Word and Netscape Navigator, except CodeWarrior is an application that you use to create other applications. This is what CodeWarrior looks like the first time you open it.  To create a new C++ project, with CodeWarrior running, follow these steps (note that the names of menus, dialog boxes, and other options might be different on your compiler):
- Go to the CodeWarrior main menu bar and click the File menu.
- From the drop-down menu that appears, click New. The New dialog box opens (see Figure). From this dialog box, you can create almost any type of application.

Use the New dialog box to select the kind of project you want to create. - Use the New dialog box to select the kind of project you want to create.
- Under the Project tab (already selected in Figure), choose Win32 C/C++ Application Stationery.
- In the Project name text box ,type Hello as the name of your project.
- Click OK. The New Project dialog box appears(see Figure).

The New Project dialog box enables you to optimize your programs. The New Project dialog box enables you to select the type of run-time environment in which the program will run.The run-time environment consists of the conditions in which the program will execute. Most often these conditions include only the operating system for which you will be compiling. For example, DOS-based programs operate in a DOS environment, whereas Win32 programs require a 32-bit Windows environment.The compiler will optimize the program for a particular environment. The end result is that your file sizes will be smaller and your programs will run faster. C++ Console applications are Windows applications that open a window similar to DOS prompts that are used to display text. They use a version of the DOS environment. - Select C++ Console App from the list in the New Project dialog box and click OK.
After CodeWarrior finishes creating the settings you requested for your project, a new window opens. This window bears the name of your project, hello.mcp. The extension .mcp is CodeWarrior’s project file extension. A project file stores all the settings for your project. It also contains a list of all the source files that are part of your project. A source file is like a text file, except that it stores source code. A source file has a .cpp extension. - Click the + icon beside the folder to open the folder. The folder icon opens revealing the project file, hello.cpp (.cpp is the extension for C ++ source files).
- Double-click the file to open it. The window shown in Figure appears on your screen.
You have made it to the dungeon’s heart. In the next section, we explain all the cryptic text shown in Figure 
You enter source code in this text editor In the Real World In early 1980, Bjae Stroustrup, at AT&T Bell Laboratories, began developing the C++ language. C++ officially received its name at the end of 1983, a name that cleverly acknowledges its predecessor, C. In October 1985, the first commercial release of the language and the first edition of the book, The C++ Programming Language, by Bjae Stroustrup, appeared. In the 1980s, the C++ language was refined until it became a language with its own personality, which it managed to do practically without any loss of compatibility with C or loss of C’s most important characteristics. C++ still maintains C’s strong structured programming techniques, but adds the functionality of object-oriented programming. C++ owes its origin to other languages as well—BCPL, Simula67, Algol68, Ada, Clu, and ML have all contributed to the C++ language. Luckily, C++ incorporates the advantages of all these languages so that you don’t need to lea all of them. Defining Source Code In order to program, you must send the computer instructions via source code. The programming language being used controls the syntax for the source code—in this case, the syntax for C++ is used. Syntax is a set of rules determining how a language is put together.) Why can’t you tell the computer what to do in English? Because English is a very complex language, and a computer would have a hard time figuring out what you are trying to say. C++ is like a simplified version of English that CodeWarrior can understand. In the next section, you lea that even C++ is too complex for a computer to understand directly. CodeWarrior must translate your C++ code into machine code. For now, though, you just focus on writing the source code and the specific rules for doing so. C++ is very specific about how you write things. For example, punctuation and order are important in C++. Even capitalization matters because C++ is case sensitive, which means the compiler can tell the difference between uppercase and lowercase letters (the compiler thinks that K is different from k. To create a program, first you enter your source code into a source code editor. Then the compiler converts the source code into a language that your computer can read (machine code). The compiler and the source code editor are both integrated into the CodeWarrior IDE. The compiler is the more important part, so often IDEs and everything in them are called compilers. Each line of code does something different, similar to each ingredient in a recipe. The compiler breaks these lines of code into instructions called commands. Each command is a single instruction to the computer. A bit earlier, we wrote that CodeWarrior is an IDE and, as such, does a lot of work for you. The text that you see in the hello.cpp window is an example of that work. This text is generated code that will serve as the basis for any program you create. Think of the total program as a bridge that you’re building; the generated lines of code are supports. Every bridge needs supports, but if the bridge relies only on supports, it is useless. Take a look at this generated code; it displays This is a test onscreen: #include <iostream> using namespace std; //introduces namespace std int main( void ) { cout << "This is a test" ; retu 0; } These lines are some of the valid instructions that you can send to the computer. You can edit and add to this code(or just erase and start from scratch) if you want to create your own program. However, assume for the moment that this code is a program that you wrote. In this case, you compile and run the generated code to create a working program. Compiling Before you can run a program, you must convert the code that you write(C++) into language that a computer can read. This is where your compiler goes to work. Imagine that you are an elf, and the computer is a dwarf. In order for the computer to understand your instructions, you must overcome a language barrier. You need a translator that can speak both Dwarven and Elven. In the computer world, this translator is a compiler. As we mentioned earlier, a compiler tus your language into a language called machine code that your computer can read. However, this translation goes only one way. The compiler cannot translate machine code into source code. Using CodeWarrior to compile, follow these steps: - On the main menu, click Project and select Compile from the drop-down menu. A window named Building Hello.mcp appears.(This step sometimes takes a few minutes. Just let the compiler finish its work.)
When this window is active, it is converting the file into machine code and checking to make sure that you haven’t broken any of the rules of the C++ language. If you altered the code that CodeWarrior gave you, an error screen might appear. If you did not change the code (or your changes are error-free), the compiler will close the Building Hello.mcp window when it finishes tuing your program into machine code. The program is now ready to be run. - Select Run in the Project drop-down menu.
Now a window appears showing the output of your program(see Figure). You should see This is a test onscreen. 
- After you compile your program and run it, here is what you will see onscreen.
- Press any key to close the window.
Writing Your First Program
Next, you write your first program: the HelloWorld application. It displays the message HelloWorld onscreen. This project will help you understand the C++ language and how it works. First, however, you need to know what CodeWarrior has already done for you. Starting Out with the Default Program When you create a new project, CodeWarrior writes some code for you. This is a basic framework within which you can program. You can delete all or part of it, depending on your needs, but most of the time, generated code helps get you started. This code is called the default program. It should look like the following code. Here, we guide you through it line by line: #include <iostream> using namespace std;//introduces namespace std int main( void ) { cout << "This is a test" ; retu 0; } The first line tells the compiler that you will be using commands from the iostream library. iostream is an existing library that comes with CodeWarrior and all other C++ compilers. It is part of the standard C++ library. Before moving on, we need to explain a few other terms that we will be using: - Commands. This is a generic word for lines of code entered into the source code editor that act as instructions for a computer.(These do not include comments.)
- include directives. These are lines of code that begin with #include. You use them to incorporate files you or others have created containing source code into your program. After #include, you place the name of the file surrounded by < and > (for the standard library files)or quotation marks(for every other file). Strangely enough, the standard library files have no file extension. That is why you include iostream rather than iostream.cpp. However, most of the other files you include will have a file extension, and you must remember to include it. Normally, include directives are placed near the beginning of a file. (Don’t worry too much about how include directives work. For now, all you have to know is that you use them to include other code in your program.)
- Libraries. Sets of existing code provided for your convenience as part of C++. (Different libraries are available with different compilers. You can find others by searching the Web. You can find a particularly good resource for free C++ libraries. Libraries are a kind of compiled include file. For example, you might generate a random number using the random number generating function rand because it is in the cstdlib library (C Standard Library). You then have to put the line #include <cstdlib> at the beginning of the program. This opening statement tells the compiler which library you are using. Library files typically have a header file extension (.h file), except for the C++ standard library files, which have no extension.
You use the second line of the preceding code, using namespace std, to make the standard libraries work correctly. The details of how this line works and what it does are advanced topics. You lea more about this line in ,“Building Namespaces.” The second part of the line //introduces namespace std is a comment. It has no influence on the way the program runs. Whenever you type two forward slashes (//) together, you are telling the compiler to ignore the rest of that line. The purpose of comments is to help make the code more understandable. Comments can be written in one of two ways. A single-line comment (like the preceding one) consumes only one line. Everything after the // on the line is ignored, as shown here: //I am an army of one Another form of the C++ comment enables you to spread a single but lengthy comment over as many lines as you want: /* dragons rule the world */ or /* dragons rule the world */ Although it is usually confusing, you can place the multiple-line comment into code at almost any point, as illustrated here: using namespace /*introduces namespace std*/ std; However, just because a possibility exists does not mean that you use it. Using comments like this can quickly make your code unreadable. A good rule of thumb is to use comments simply to make your code more understandable. If they aren’t helping, take them out. Again, nothing within a comment affects the code. Use comments only to explain complicated or large parts of the program in plain English. The compiler ignores the comments when it tus the program into machine code. The next line, int main( void ), specifies the beginning of the main function. You put most of your code inside the main function. In, “Writing Functions,” you lea how to put your code in other functions, but for now, almost every line you write will go inside the main function. The main function begins with { and ends with }. Within these brackets, you can place almost any line of code. Every program must have a main function (and only one) The next line says that the code of the main function starts here with the open bracket ({ ). The next line displays the message This is a test on the screen. The word cout displays text on the screen. Because it is not built into the language but is part of the iostream library, you must include the iostream library in order to use it. We cover the cout statement in more detail in the section “Working with Text,” later in this chapter. (Statements are single thoughts or commands—think of them as equivalent to sentences. They usually end with a semicolon.) retu 0; tells the computer that you don’t want to do anything more in the main function and to exit the function. The closing bracket(}) in the last line tells the compiler that there are no more lines of code within the main function. retu 0;is an executable statement that exits the function, but the closing bracket is there to indicate that no more executable statements are in the function. Note that all the semicolons must be included. Most statements must end with a semicolon. In Chapter 3, “Taking Command with Control Statements,” you lea about statements that do not require semicolons, but until then, every statement you encounter must terminate with a semicolon. The exact rule is that every executable statement must end with a semicolon. You use the semicolon to indicate the end of a statement, rather than use the end of the line, which means that you can have more than one statement on a single line. Now that you have overcome the first challenge, your quest should be looking even easier. In the Real World Comments are useful, not only so that others can read your code, but also so that you can read your code. Creating Hello World You are now ready to create your very first program, which we suggest naming “Hello World” (although you could name it anything you want). This program will display the message Hello World onscreen. By creating this program, you will lea how to edit source code and become a bit more familiar with how the source code generated by CodeWarrior works. Begin by replacing cout << "this is a test"; with cout << "Hello World"; Now, when you compile and run the new program, you will see Hello World onscreen. The finished code is shown here.(Note that we added a comment at the beginning of the program to provide the names of the program and the author. Although not necessary, doing so is a good idea.) //1.1 - Hello World - Dirk Henkemans -Premier Press #include <iostream>using namespace std;//introduces namespace std int main( void ) { cout << "Hello World!!!" ; retu 0; } Follow the same steps that you used to compile the code generated by CodeWarrior, or you can (with most compilers) just press F5 to compile and execute the program. The program displays the message Hello World onscreen. The Development Cycle
The development cycle describes the process that you must go through in order to create a program. You will eventually find that this process is straightforward and easy. The flowchart in Figure illustrates this cycle. This chart shows the process for creating a working program.  Generally, to create a program, you follow these steps:
- Type the code in the source code editor (CodeWarrior or some other editor). You did a little bit of this in the earlier section, “Writing Your First Program,” and you will do a lot more by the time you finish this book.
- Compile the code. If there are errors, you must go back to the code to fix them. For step-by-step information on compiling code using CodeWarrior, refer to the earlier section “Compiling.”
- Link the code. Linking the code is the process of checking to see whether the code works with all the files that you included in the program. If you get an error, you must retu to Step 1 to fix the error. CodeWarrior does this automatically for you. You don’t even have to worry about the linking process unless you get an error.
- Test the program. You test the program to be sure that it functions properly. In other words, you test it to be sure that that there are no semantic errors. Semantic errors are problems with the way your program works. Basically, a semantic error is when your program compiles and runs, but it doesn’t do what it is supposed to do. For example, if you write a program to display the word Hello on screen but get the word Good-bye instead, you have a semantic error that must be fixed. However, the code will still compile with semantic errors.
If your program makes it through all of these steps successfully, then the development cycle is now complete. Working With Text
Before the advent of graphics, text was the basis of all programs. Text-based adventure games and text-based Bulletin Board Systems were our first programming experiences. Text is still a very important element of programming. In this section, you find out how to assemble and store text. Text creates the simplest medium for displaying the output of programs. This book starts by using only text because text is sufficient to display the output of most programs. The technical name for text in the computer world is string. For example, in the “Hello World” program, “Hello World” is a string. Assembling Strings A string is a collection of characters; in general, we think of a character as a symbol that you can type from the keyboard, including spaces. The computer interprets strings such as “For Honor!!!” as a series of characters, each occupying one letter or space(‘F’ is the first character,‘o’ is the second, and so on). Strings are enclosed within quotation marks, but characters are enclosed within single quotes. For example, “a”, “ “ (space), “4”, and “%” are all strings consisting of one character. However, ‘a’, ‘ ’, ‘4’ and ‘%’ are all characters. You can use characters almost anywhere that you can use a string. However, characters are not always what you might expect. You cannot type some characters from the keyboard. There are 256 different kinds of characters. You can access many other characters by holding down Alt and writing their numeric values. For example, the numeric value of A is 65. Appendix C, “Using the Standard ASCII Character Table,” provides a reference for the standardized characters and their numeric values. It is important to realize the distinction between strings and characters. Strings are made up of characters, but are quite different from them. In addition, a character can easily be converted to a string, but a character is not a string. This might all seem complex, but creating strings is actually easy to do. The following are some examples of strings. As you can see, they are just a little bit of text surrounded by quotation marks. "For Honor!!!" "Pass me my sword." "Who put my staff on the wagon?" Storing Strings You can store strings in a computer so that you don’t have to write them more than once. Specifically, you store strings by putting them in memory (we cover memory in detail in Chapter 2, “Descending Deeper . . . into Variables”). The code for strings is held in a library aptly named the string library. In order to store strings, you must include the string library. You do this by including the following line at the beginning of your program: #include <string> When you want to store a string, you must provide a name for it so that the computer knows which string you want to access in the future. For example, we will put “A dragon is coming” into memory and name the string yell. There is one small complication, though. A computer can store many things in memory, not just strings, and it stores different things differently. For example, a number is stored differently than a string is stored. So, you must also tell the computer what kind of thing it is storing. Here’s what you must provide the computer: - The type of thing that is being stored (in this case, a string)
- The characters that make up the string
- The name of the string
Although C++ does not provide you with the ability to store strings, the standard library does. In order to use it, though, you must include the <string> library in your program. Here is how you store a string, after you include the library: string yell = "A dragon is coming"; First comes the type of thing being stored: a string. Second comes the name of the string: yell. The equal sign tells the computer that the string, “A dragon is coming”, is being stored in yell. Last comes the semicolon, which tells the computer that you have finished the command and are moving on to another command. Displaying Strings With Cout
Now that you know how to write a string, you probably want to display it. In this section, you find out how to display strings in multiple ways. You display a string with the cout command. The cout command is a command that comes in the standard C++ library in the file iostream. Here is the cout command statement used earlier: cout<< "Hello World"; You can display any string using cout, for example: cout<< "For Valour!"; First, you type cout, then the two less-than signs (<<), and then the string that you want to display followed by a semicolon (which, as you probably recall, tells the compiler that the command is finished). Displaying Multiple Strings with cout You can now use the basics of cout, but there’s much more to lea about cout and strings. For example, you can display multiple strings at the same time. Say that you want to display two things, but they are in two different strings. According to the preceding techniques, the code will look something like this: cout<<"Red"; cout<<" Dragon"; Output: Red Dragon However, there is an easier method. You can display as many strings as you want side-by-side and separated by <<. You can create the previous code fragment like this: cout<<"Red"<<" Dragon"; Output: Red Dragon The output for both code fragments is identical, yet the second one is a little simpler. Also, because of the way C++ is written, you can split up code over more than one line. For example, the previous code is also equivalent to the following: cout <<"Red" <<" Dragon"; Output: Red Dragon This is actually a trivial example because you can use a single, larger string (“Red Dragon”). Generally, you structure your code so that it is easy to read. You should place a new cout at the beginning of each phrase. For example, to display a sentence about dragons and a sentence about elves, everything in the dragon sentence should be after one cout, and everything in the elven sentence should be after a different cout. In this way, your code is more organized and easier to read. Working with Escape Characters You cannot express characters such as quotation marks (“) and line breaks literally in a string. If you include a quotation mark as part of a string, the compiler will think that the quotation mark indicates the end of the string. For example, to create a string quoting someone, you might write "He said, "This is a quote"." However, the compiler will interpret this as two separate strings, “He said, “ and “.” Because the words This is a quote are assumed to be non-strings, you wind up with a syntax error. A similar problem occurs with line breaks. All strings must be contained on a single line, so if you try to place a line break in the middle of a string, you get a syntax error. Fortunately, there is a solution to problems like these. The solution is to use escape characters. Escape characters (or meta-characters) are a set of characters used to represent characters that cannot be expressed literally. To create an escape character, you combine a backslash () and the particular character. For example, the escape character for a quotation mark is ”. Here’s what it looks like in the code: "He said, "This Is a quote"." This code produces the desired string. If you were to output this string, He said, “This is a quote”. would be displayed onscreen. There are many other escape characters as well. Table summarizes the important ones. 
You will want to become familiar with most of these escape characters because they come in handy; in fact, some of them, such as the line break, are essential. Displaying Stored Strings Up to this point, you have displayed only literal strings. Now, you are ready to display stored strings. Using the name of the string in place of the string itself enables you to display stored strings. Here, as a quick reference, is the earlier string example: string yell = "A dragon is coming"; To display the string, you use its name, which is yell, rather than the actual text: cout<<yell.c_str(); The preceding line does the same thing as the following one, which we used earlier in the section “Storing Strings.” cout<<"A dragon is coming"; Don’t worry about the .c_str() after the name of the string; it is just “magic” code that causes the string to display properly. We explain this magic code in Chapter 6, “Moving to Advanced Data Types.” The Town Crier Program You’ve leaed how to create saved strings and how to display them onscreen using cout. Now, it’s time to test that knowledge by creating the “Town Crier Program.” Imagine that a dragon is approaching your village and you have to yell a waing to everyone. If you can yell your waing four times faster than usual, the village will be saved. This is obviously a job for our heroes . . . the saved string and cout—only they can save the village. Your objective, therefore, is to create a program that will display the text of your waing four times in a row. Following is a code listing that illustrates one way to create this program, but we suggest not looking at it unless you become stuck while creating your own program: //- Town Crier Program - Dirk Henkemans - Premier Press #include<iostream> #include<string> using namespace std; //introduces namespace std string yell = "A dragon is coming, take cover!!!"; int main(void)< { cout<< yell.c_str() <<endl << yell.c_str() <<endl << yell.c_str() <<endl << yell.c_str() <<endl; retu 0; } Output: A dragon is coming, take cover!!! A dragon is coming, take cover!!! A dragon is coming, take cover!!! A dragon is coming, take cover!!! Using Cin
In your programming, you need to set up the capability for users to use and store their input. For example, after typing their names, they should be able to store and then use their names. You need a way to retrieve and use information from the user when the program is running (run time), rather than when you are writing the program (design-time) because you can’t anticipate what users will write. In this section, you find out how to accept user input and store that input so that it can be used later. Storing Strings Using cin You can store strings using cin much as you’ve stored them before, except now you declare the name and assign its value later. When you assign this value, you use the cin object. (An object is a programming construct that represents an entity or concept. The cin object represents the keyboard or some other input device, whereas the cout object represents the screen or some other output device.. Here is an example of storing a user-input string: string name; cin>>name; Notice that you use cin in much the same way that you use cout. Notice that the less-than signs are now greater-than signs. These signs indicate that the computer is accepting data rather than printing it. Here is an example of a complete program using cin: //1.3 - Hello Program – Dirk Henkemans – Premier Press #include <iostream> #include <string> using namespace std;//introduces namespace std string name = ""; // "" means empty string int main( void ) { cout<< "What is your name?"; cin>>name; cout <<endl<< "Hello " << name.c_str() ; retu 0; } Output (bold word is user input): What is your name? Jackie Hello Jackie Trick With some compilers and situations, Windows closes the window for your program without displaying the last section of the program. Actually, Windows is recognizing that only text is to be displayed; once the text displays, Windows immediately closes the window. To see the end of your program, you can place a cin statement at the end of the program (which you will do in the “Pirate Musketeer Game,” later in this chapter). Windows will not close the window until after you press Enter at the end of your program. In line 5 of the preceding code, string name tells the computer that there is space in the computer for a string called name. Remember that you must include the string library in order to use strings. In line 8, cout<< “What is your name?”; displays a prompt for the user. This prompt asks the computer to write the user’s name. In line 9, cin<<name; tells the computer to stop in order for the user to type. When the user presses Enter, the computer assigns everything that the user typed before pressing Enter to the string called name. In line 10, cout<< endl << “Hello “ << name.c_str(); begins by telling the computer to start on the next line. The computer then displays Hello onscreen followed by the user’s name. For example, if the user types Joe or Jane for his or her name in line 9, line 10 will display Hello Joe or Hello Jane. The space between Hello and the name is generated because a space is added at the end of the “Hello string (before the closing quotation mark). Although you can lea much more about text, you now have the basic information regarding using text, so we tu your attention to using numbers. Working With Numbers
A computer is entirely number-based. Even the text you worked with earlier is made up of numbers (for example, if you add one to B, it becomes C). Numbers are the foundation for everything that happens on the computer. Having a good grasp on numbers is essential in the computer world. In this section, you find out about basic math, the modulus operator, and how to use integers. Introducing Integers Computers can store information many ways. For now, however, we cover the basics on integers and how to use them. Integers are all the whole numbers, including zero and the positive and negative numbers. For example, 5, 0, and –100 are all integers, whereas 0.5 is not an integer. If you try to store a decimal as an integer, the computer will respond by truncating the remaining section; that is, the computer will chop off everything after the decimal. Taking Action with Operators In a general sense, an operator is any symbol or double symbol such as <=, and in some cases even terms such as sizeof(), that causes the compiler to take an action. The actions of adding, subtracting, multiplying, and dividing use operators. For example, when you ask a computer to add two numbers, you use the addition operator (+), which makes perfect sense, doesn’t it? We all know that 2 + 2 = 4. Here’s how you do the same thing in code with C++: cout<<2 + 2; This line displays 4 onscreen. These four basic operators are self-explanatory; they do exactly what you probably think they do, but take a moment to review the symbol for each one: As in math, these operators do not all execute from left to right. The multiplication and division operators execute before the addition and subtraction operators, for example: 1 + 3 * 2 3 * 2 is executed first, and then 1 is added, resulting in the number 7. Parentheses increase the order of precedence. If you take the preceding formula and add parentheses, as shown here (1 + 3) * 2 1 + 3 executes first, producing 4, which is multiplied by 2, resulting in 8. Trick Add lots of parentheses. Doing so makes debugging much easier. The rule of thumb is as follows: If you think your formula might need parentheses, put them in. This course of action will make your code easy to read and understand. The Modulus Operator Remember way back to sixth grade when you did long division and your answers always worked out to be whole numbers? Then you began to work with remainders because you couldn’t yet deal with decimals. Sometimes, it is especially useful to know the remainder of a number when it is divided. You get this information using the modulus operator. Themodulus operator is the remainder of x divided by y (x % y). To find the remainder of 5 divided by 2, you write the code like this: 5 % 2 This line retus 1. Here’s another easy example. Imagine that there are five pirates and 16 shiny gold coins. The pirates need to figure out whether the treasure can be divided evenly among themselves or whether they will have to get into a big, drunken brawl, which leads us to the next game.(Mind you, they will probably get into a drunken brawl anyway.) Creating The Pirate Musketeer Game
It’s now time to test your new-found skills. Although this program is on the CD-ROM at the back of this book, we highly suggest that you try it on your computer. This program will be your first real program and will test your knowledge on your use of numbers and text. Happy swashbuckling! //- Pirate Musketeer Game - Dirk Henkemans - Premier Press #include <iostream> #include <string> using namespace std;//introduces namespace std int main( void ) //tells a pirate story { int buddies; int afterBattle; string exit; cout<< "You are a pirate and are walking" << " along in the crime filled " << endl << "city of Havana (in 1789). " << "How many of your pirate buddies "<<endl <<"do you bring along? (lots)"<<endl; //records the amount of friends you bring along cin>>buddies; //calculates the amount of pirates left after the battle. afterBattle = 1 + buddies - 10; cout<< "Suddenly 10 musketeers jump out " << "from the local tave and " <<endl << "draw their swords. " << "10 musketeers and 10 pirates die in the " <<endl << "battle. There are only " <<(buddies + 1 - 10)<< " pirates left." <<endl <<endl; cout<< "They drop 107 gold coins. That is " <<(107 / afterBattle) << " gold coins each." <<endl; cout<< "There is a big drunken brawl for the last " <<(107 % afterBattle)<< " coins."; //pauses so you can see the result cin>>exit; retu 0; |