C++ is one of many programming languages. When you install a Desktop IDE (Interactive Development Environment) like Microsoft Visual Studio or XCode, it usually comes with many other programming languages. For example you might see options to write Swift, C#, Java or Objective-C programs. Replit, like these others, offers a wide array of options. We will, however, focus on developing console mode C++ code throughout these labs. It can be confusing to know which options to pick at first. In this lab we want you to know how to set up a simple C++ program of the right type and run it.
A console mode application is a character based program that runs in a text-based input/output window. |
For your first C++ program, you will build a console mode application that asks for your name and then displays a greeting message. This (i.e. a console mode application) is the kind of program that you will build for all your lab and class exercises/assignments because console mode programs are often simpler to build than GUI or Windowed applications.
There are video instructions for downloading various IDEs and creating the correct program type for various IDEs in URCourses. We have included links to those videos here, set to start at the timecode for creating a console application. You can ignore any File I/O instructions you see in the video for now.
When you come to the part where you are ready to put C++ code in your project, type this code into the C++ editor:
// FILE: hello.cpp
// PURPOSE: An example of a simple I/O stream
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Please enter your name" << endl;
cin >> name;
cout << "Hello, " << name << endl;
return 0;
}
Make sure you type it... it's important to get used to fixing typing mistakes while you work.
Save hello.cpp after you have finished editing it.
In order to compile any code in an IDE, you have to create a project. A project holds three major types of information:
hello.cpp
will be the only source code file, but in larger
applications you often break the code up into several different
files to make it easier to understand (and also to make it
possible for several people to work on it simultaneously).
The project maintains a list of the different source files and
compiles all of them as necessary each time you want to create a
new executable.For now we create a very simple project "hello" and use it to compile hello.cpp.
g++ code_file.cpp -o output_name
You can then run the resulting executable by typing something like this afterward:
./output_name
For our hello example that's:
g++ hello.cpp -o hello
./hello
When you run in most IDEs, a window will pop up. If errors or warnings are displayed in the Build status window, there is probably an error in the source file. Often, you can double click on these to be taken to the error. Check your source file again for missing semicolons, quotes, or braces or for spelling mistakes and so on.
XCode and replit are a little different. Watch the videos to see what to do!
© Department of Computer Science, University of Regina.