ISO C++
In the last few years the International Standards Organization (ISO) has created a standard for C++. VC++ 5.0 is already compatible to this standard. This new standard has made some changes to C++ compared to a few years ago. Unfortunately, our textbook still uses "old C++".

We have already seen one new data type bool that is not explained in the book. There is another interesting type called string in ISO C++ that we will use in this course.


New C++ headers

According to ISO C++, header files do not have the .h at the end. Furthermore the header files for stdlib.h is now called cstdlib, and the header file for mathematical functions is called cmath. Furthermore, we must tell C++ to use the standard namespace (we will not explain what that is).

  // 
  // This is the beginning of a program in ISO C++
  //

  #include <cstdlib>
  #include <iostream>
  #include <cmath>

  using namespace std;

The ctype.h header file can still be used as before.


The string type

Like cout, the string type is not part of the C++ language itself, it is defined in a header file string. To use it, we have to include the header file:

   #include <string>
Note that we have to use <string>, not <string.h>!

A variable of type string can contain a string like "Hello World". The length of the string can change during the execution of the program.

A string variable is declared as follows:

  string s1;    // Declare a new string

We can also initialize it with a value as follows:

  string hello = "Hello World";

We can output the string as usual:

  cout << hello << endl;   // prints "Hello World"


Two strings can be concatenated (put together) using the plus sign:
  string aa, newstr;
  aa = 'A';
  newstr = aa + " " + hello;  
  // newstr == "A Hello World"
  newstr += "!!";
  // newstr == "A hello World!!";

We can also compare strings with all the six comparison operators. When comparing two strings, the one that appears first in a dictionary is "less" then the one that appears later. So, for instance, Cheng is less than Cheong.


#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;
  
int main()
{
  string first, second;

  cout << "Enter two names : ";
  cin >> first >> second;
  if (first <= second) {
    cout << "The order is correct\n";
  } else {
    cout << "Wrong order: '"
      << second << "' should be before '"
      << first << "'\n";
  }
  return 0;
}


#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;
  
int main()
{
  string oldw, neww;
  cout << "Enter old string: ";
  cin >> oldw;
  cout << "Enter new string: ";
  cin >> neww;
  
  string word;
  cout << "Enter text:\n";
  do {
    cin >> word;
    if (word == oldw)
      cout << neww << " ";
    else
      cout << word << " ";
  } while (word != "exit");
  cout << endl;
  return 0;
}

Otfried Cheong