Home > Articles > Programming > C/C++

  • Print
  • + Share This
This chapter is from the book

If You’re Not Using Microsoft

If you’re not using Microsoft Visual Studio as your compiler, most of the steps described in the previous sections won’t apply. If any documentation comes with your compiler, make sure you read it in case, like Microsoft Visual Studio, it has idiosyncrasies of its own.

With compilers other than Visual Studio, do not put in the line #include “stdafx.h” and make sure you use the simpler program skeleton:

int main() {

}

Beginning with the next section, this book is going to adhere fairly closely to generic C++, which has nothing that is platform or vendor specific. But in this chapter, I’ll keep reminding you of what you need to do for Visual Studio.

Example 1.1. Print a Message

Here is the program introduced earlier, written in generic C++ (except for the comment, which indicates what you have to do to run it in Visual Studio).

print1.cpp

// If you're using Microsoft V.S. leave in this line:
// #include "stdafx.h"

#include <iostream>
using namespace std;

int main()
{
     cout << "Never fear, C++ is here! "
     return 0
}

Remember that exact spacing does not matter, but case-sensitivity does.

Also remember that if and only if you are working with Microsoft Visual Studio, then, at the beginning of the program, you must leave in the following line:

#include "stdafx.h"

After entering the program, build and run it (from within Microsoft Visual Studio, press Ctrl+F5). Here’s what the program prints when correctly entered and run:

Never fear, C++ is here!

However, this output may be run together with the message “Press any key to continue.” In the upcoming sections, we’re going to correct that.

How It Works

how.jpg

Believe it or not, this simple program has only one real statement. You can think of the rest as “boilerplate” for now—stuff you have to include but can safely ignore. (If you’re interested in the details, the upcoming “Interlude” discusses the #include directive.)

Except for the one line in italics, the lines below are “boilerplate”: these are items that always have to be present, even if the program doesn’t do anything. For now, don’t worry about why these lines are necessary; their usage will become clearer as you progress with this book. In between the braces ({}), you insert the actual lines of the program—which in this case consist of just one important statement.

#include <iostream>
using namespace std;

int main()
{
     Enter_your_statements_here!
     return 0;
}

This program has one only real statement. Don’t forget the semicolon (;) at the end!

cout << "Never fear, C++ is here!";

What is cout? This is an object—that’s a concept I’ll discuss a lot more in the second half of the book. In the meantime, all you have to know is that cout stands for “console output.” In other words, it represents the computer screen. When you send something to the screen, it gets printed, just as you’d expect.

In C++, you print output by using cout and a leftward stream operator (<<) that shows the flow of data from a value (in this case, the text string “Never fear, C++ is here!”) to the console. You can visualize it this way:

Don’t forget the semicolon (;). Every C++ statement must end with a semicolon, with few exceptions.

For technical reasons, cout must always appear on the left side of the line of code whenever it’s used. Data in this case flows to the left. Use the leftward “arrows,” which are actually a pair of less-than signs (<<).

The following table shows other simple uses of cout:

STATEMENT

ACTION

cout << “Do you C++?”;

Prints the words “Do you C++?”

cout << “I think,”;

Prints the words “I think,”

cout << “Therefore I program.”;

Prints the words “Therefore I program.”

Exercises

exerc.jpg

Exercise 1.1.1. Write a program that prints the message “Get with the program!” If you want, you can work on the same source file used for the featured example and alter it as needed. (Hint: Alter only the text inside the quotation marks; otherwise, reuse all the same programming code.)

Exercise 1.1.2. Write a program that prints your own name.

Exercise 1.1.3. Write a program that prints “Do you C++?”

  • + Share This
  • 🔖 Save To Your Account