// archives

C++

This tag is associated with 39 posts

Writing to a Text File

For file output, C++ uses analogs to cout

So, to prepare for file output, let’s review some basic facts about using cout for console output
(B-1):  

You must include the iostream header file.
The iostream header file defines an ostream class for handling output.
The iostream header file declares an ostream variable, or object, called cout.
You must account [...]

The iomanip Header File

Setting some format values, such as the field width, can be awkward using the iostream tools

To make life easier, C++ supplies additional manipulators in the iomanip header file (B-1)

(B-1) The three most commonly used manipulators (B-2)

setprecision() for setting the precision
setfill() for setting the fill character
setw() for setting the field width

(B-2) These manipulators take arguments

The setprecision() [...]

Introducing the string Class

The ISO/ANSI C++ Standard

Expanded the C++ library by adding a string class (B-1)
(B-2) So now, instead of using a character array to hold a string, you can use a type string variable (or object, to use C++ terminology) (B-3)
(B-3) As you’ll see, the string class is simpler to use than the array and also provides [...]

Adventures in String Input

(A-1) shows that string input

Can be tricky

Example of (A-1)

// instr1.cpp — reading more than one string
#include <iostream>
int main()
{
using namespace std;
const int ArSize = 20;
char name[ArSize];
char dessert[ArSize];
 
cout << "Enter your name:\n";
cin >> name;
cout << "Enter your favorite dessert:\n";
cin >> dessert;
cout << "I have some delicious " << dessert;
cout << " for you, " << name << [...]

Using Strings in an Array

The two most common ways of getting a string into an array

Are to initialize an array to a string constant and to read keyboard or file input into an array
(A-1) demonstrates these approaches by initializing one array to a quoted string and using cin to place an input string in a second array

The program (A-1) [...]

The bool Type

The ANSI/ISO C++ Standard has added a new type (new to C++)

Is called bool     

 It’s named in honor of the English mathematician George Boole, who developed a mathematical representation of the laws of logic

In computing, a Boolean variable

Is one whose value can be either true or false

In the past, C++, like C

Has not had a Boolean [...]

Unsigned Types

You should use unsigned types

Only for quantities that are never negative (B-1)

(B-1) Such as populations and bean counts

To create unsigned versions of the basic integer types

You just use the keyword unsigned to modify the declarations (A-1):

Example of (A-1)

unsigned short change; // unsigned short type
unsigned int univac; // unsigned int type
unsigned eniac; // also unsigned int
unsigned long edvac; // [...]

Namespaces

If you use iostream instead of iostream.h

You should use the following namespace directive to make the definitions in iostream available to your program (A-1):

This is called a using directive

Example of (A-1)

using namespace std;

Namaspace support

Is designed to simplify the writing of programs that combine preexisting code from several vendors

One potential problem is that you might use [...]

Representing a Double-Word

(Image-1)

A double-word (Image-1)

Is a group of two consecutive Words
A double-word combines 4 bytes, or 8 nibbles, or 32 bits

The bits

Counted from right to left, start at 0 and end at 31 (Image-1)

(Image-2)
 

Considered as a group of 32 bits (Image-2)

The most right bit, bit 0, is called the Low Order bit or LO bit or LOBIT
The [...]

Enumeration Types

An enumeration type

Is a type whose values are defined by a list of constants of type int
An enumeration type is very much like a list of declared constants
Enumeration types can be handy for defining a list of identifiers to use the case labels in a switch statement

When defining an enumeration type

You can use any int [...]