Creating your C++ file

You can begin coding your HelloWorld program. The .cpp file that you create will be saved in the project folder you just created Creating a Makefile project.

Files are edited in the C/C++ editor located to the right of the C/C++ Projects view. The left margin of the C/C++ editor, called the marker bar, displays icons for items such as bookmarks, breakpoints, and compiler errors and warnings.

For more information about:

To create a C++ file:

  1. In the Project Explorer view, right-click the HelloWorld project folder, and select New > Source File.
  2. In the Source file: field, type main.cpp.
    By default the source folder should be your project folder.
    The template selected is probably Default C/C++ Source Template.
  3. Click Finish.
  4. A Comment template probably appears at the top of an otherwise empty file. Type the code, exactly as it appears below, in the editor. Or you can paste it in from this help file.
    #include <iostream>
    using namespace std;
    
    int main () {
        // Say HelloWorld five times
        for (int index = 0; index < 5; ++index)
          cout << "HelloWorld!" << endl;
        char input = 'i';
        cout << "To exit, press 'm' then the 'Enter' key." << endl;
        cin  >> input;
        while(input != 'm') {
            cout << "You just entered '" << input << "'. "
                 << "You need to enter 'm' to exit." << endl;
            cin  >> input;
        }
        cout << "Thank you. Exiting." << endl;
        return 0;
    }
    
  5. Click File > Save.

Your new .cpp file is displayed in the Project Explorer view. Your project now contains main.cpp. Before you can build your HelloWorld project, you must create a makefile.

Next icon Next: Creating your makefile

Back icon Back: Creating your project

Related concepts
CDT projects
Project file views
Coding aids

Related tasks
Working with C/C++ project files
Writing code

Related reference
C/C++ Projects view

IBM Copyright Statement