CIS 150 Basic C++ Program

Objectives

  • Use variables and data types in C++
  • Create a basic C++ program
  • Write comments in a C++ program
  • Write statements to perform basic input in a C++ program
  • Write statements to perform basic output in a C++ program
  • Compare and contrast the different types errors in programs

C++ program structure

/* Documentation comments */ #include <iostream> using namespace std; int main() { // C++ program statements return 0; }

Notes on above program:

  • Documentation comments are required in this course, but the C++ compiler ignores them.
  • Single line comments start with // and go to the end of the line.
  • Block comments start with /* and end with */
  • You may see different forms of "int main()", such as: int main(int argc, char* argv[])
  • Lines starting with # are preprocessing directives.
  • Including the iostream library allows the program to do input and output.
  • There is a more accurate form of the using statement that you will use after this first course. The version shown here allows you to reference everything in the std namespace. In the future, you will want to restrict that to particular items using a statement such as "using std::cout;"
  • C++ programs start at main().

Variables/h2>

  • Variables are memory locations where values are stored
  • The naming rules for variables are:
    • must start with letter or '_' (underscore)
    • can only contain letters, digits, and '_' (underscore)
    • case-sensitive
    • may not be a C++ reserved word
    • special rules apply to identifiers that start with an underscore or contain a double underscore
  • Declaring a variable requires a data type and a name: int num;
  • Initializing while declaring: int num = 7;
  • Letting the compiler infer the data type: auto num = 7;

Data types

  • C++ has many data types available, but we will use a select few.
  • int: used for integer values, such as 1, -5, 1786, 0
  • long: same as int but with more range
  • float: used for floating- point values, such as: 3.14159, 42, 0, -4.3
  • double: same as float but with more precision and range
  • char: individual characters, such as 'a', '3', '?'
  • Note: '3' (a char) is not the same as 3 (an int)
  • bool: stores Boolean values (True, False)
  • string: Used for storing multi-character sequences such as names. This is not a primitive (built-in) data type. You have to include the string library and specifiy that you are using std::string (or namespace std)

Basic output

#include <iostream> using namespace std; int main() { cout << "Welcome to CIS 150\n"; return 0; }

Basic input

#include <iostream> using namespace std; int main() { int age = 0; cout << "Enter your age: "; cin >> age; cout << "You are " << age << "years old\n"; return 0; }

Errors

  • Errors such as not ending a statement with a semicolon are called syntax errors. The compiler will catch these and fail to compile the program.
  • Errors such as indenting incorrectly are called style errors. The compiler will not catch these. Style conventions are for program readability and maintainability and the compiler does not care.
  • Errors such as having an incorrect formula or algorithm are called logic errors. The compiler will usually not catch these. The program will usually still compile, but run incorrectly. That is why testing is so important.