Time spent doing this is appreciated but there are better sources for learning the basics of programming. For one, you can buy/download the book for yourself or look up free tutorials/references which cover almost all the basics.
Downloads you need:
Code::Blocks
or
Bloodshed Software - Dev-C++
Creating a Project.
The first step to creating a C++ program is to create what is known as a project. A project tells Code::Blocks the names of the .CPP source files to include ans what type of program to create. Most of the programs in the book will consist of a single source file and will be command-line style:
1. Choose Start→Programs→CodeBlocks→CodeBlocks to start up the CodeBlocks tool.
2. From within Code::Blocks, choose File→New→Project.
3. Select the Console Application icon and then click Go.
4. Select C++ as the language you want to use from the next window Click Next.
Code::Blocks and gcc also support plain ol' C programs.
5. Select the Console Application icon and then click Go.
6. In the Folder to Build Project In the field, navigate to the subdirectory where you want your program built.
7. In the Project Title field, type the name of the project, in this case name it "Conversion"
The resulting screen is shown below.
8. Click Next
The next window gives you the option of creating an application for testing or the final version. The default is fine.
9. Click Finish to Create the conversion project.
Entering the C++ code
The Conversion project that Code::Blocks creates consists of a single, default main.cpp file that does nothing. The next step is to enter our Program:
1. In the management window on the left, double-click main, which is under Sources, which is under Conversion.
Code::Blocks opens the empty main.cpp program that it created in the code editor as shown in the picture below.
2. Edit main.cpp with the following program exactly as written.
Don't worry too much about the indentation or spacing - it isn't critical whether a given line is indented two or three spaces, or whether there are one or two spaces between two words. C++ is case sensitive, however, so you need to make sure everything is lowercase.
3. Choose File→Save to save the source file.Code:// // conversion - program to convert temperature // Celsius degrees into Fahrenheit: // Fahrenheit = Celsius * (212 - 32)/100 + 32 // #include <cstdio> #include <cstdlib> #include <iostream> using namespace std; int main(int nNumberofArgs, char* pszArgs[]) { // enter the temperature in Celsius int celsius; cout << "Enter the temperature in Celsius:"; cin >> celsius; // calculate conversion factor for celsius // to Fahrenheit int factor; factor = 212 - 32; // use conversion factor to convert Celsius // into Fahrenheit values int fahrenheit; fahrenheit = factor * celsius/100 + 32; // output the results (followed by a NewLine) cout << "Fahrenheit value is:"; cout << fahrenheit << endl; // wait until user is ready before terminating program // to allow the user to see the program results system("PAUSE"); return 0; }
I know that it may not seem all that exciting, but you've just created your first C++ Program!
Building Your Program
After you've saved your C++ source file to disk, it's time to generate the executable machine instructions.
To build your Conversion program, you choose Build→Build from the menu or press Ctrl-F9. Almost immediately, Code::Blocks takes off, compiling your program with gusto. If all goes well, the happy result of 0 errors, 0 warnings appears in the lower right window.
Code::Blocks generates a message if it finds any type of error in your C++ program - and coding errors are about as common as ice cubes in Alaska. You'll undoubtedly encounter numerous warnings and error messages, probably even when entering the simple Conversion.cpp. To demonstrate the error-reporting process, let's change lin 16 from cin >> celsius; to cin >>> celsius;.
This seems an innocent enough offense - forgivable to you and me perhaps, but not to C++. Choose Build→Build to start the compile and build process. Code::Blocks almost immediately places a red square next to the erroneous line as shown in the picture below. The message in the Build Message tab is rather cryptic error: expected primary-expression before '>' token. To get rid of the message, remove the extra '>' and recompile.
Tip: You probably consider the error message generated by the example a little cryptic but give it time - you've been programminf for only about 30minutes now. Over time you'll come to understand the error messages generated by Code::Blocks and the gcc much better.
Warning!: Code::Blocks was able to point directly at the error this time but it isn't always that good. Sometimes it doesn't notice the error until the next line or the one after that, so if the line flagged with error looks okay, start looking at its predecessor to see if the error is there.
Executing your program
It's now time to execute your new creation...that is, to run your program. You will run the CONVERT.EXE program file and give it input to see how well it works.
To execute the Conversion program, choose Build→Build and Run or press F9. This rebuilds the program if anything has changed and executes the program if the build is successful.
A window opens immediately, requesting a temperature in Celsius. Enter a known temperature, such as 100 degrees. After you press Enter, the program returns with the equivalent temperature of 212 degrees Fahrenheit as follows:
The message Press any key to continue . . . gives you the opportunity to read what you've entered before it goes away. Press Enter, and the window (along with its contents) disappears. Congratulations! You just entered, built, and executed your first C++ program.Code:Enter the temperature in Celsius:100 Fahrenheit value is:212 Press any key to continue . . .
Credits:
C++ for Dummies 6th Edition for this.
-:TKK:-WaSsUp for typing all of this.
Last edited by -:TKK:-WaSsUp; 10-25-2010 at 08:52 PM.
Time spent doing this is appreciated but there are better sources for learning the basics of programming. For one, you can buy/download the book for yourself or look up free tutorials/references which cover almost all the basics.
Hell_Demon (10-26-2010),xTremist (10-29-2010)
And you had to choose the lesser-used/supported/featured IDE's why..?
Also C++ as a first language
Languages: C, C++, x86 ASM, PHP, Lua
Dev-C++ has not been maintained for over 5 years. Why still use it for?