Using CodeBlocks 10.05 – Part 7

In the previous post, Using CodeBlocks 10.05 – Part 6, I was able to cover up to the end of Book IV of C++ All-In-One Desk Reference For Dummies. This post begins with Book V Chapter 1. I’m hoping to wrap up the coding differences for CodeBlocks 10.05 soon. You can find a complete list of these posts in the C++ All-in-One for Dummies category archive.

All of the projects in Book V Chapter 1 compile cleanly and work fine in CodeBlocks 10.05. Some readers have complained about the use of .DAT as a file extension. You can change it to .TXT if desired. A few others report problems with Windows Vista and Windows 7 using the root directory for file storagethere isn’t any problem with using a different directory to hold the test files as long as you use the same directory consistently. For example, you might want to change the directory to C:/Author/MyFile.dat.

There is a sidebar on page 585 (Book V Chapter 2) that tells how to get the working directory. This example appears in the \Author\BookV\Chapter02\GetWorkingDirectory folder. It won’t currently compile because it’s missing an #include for stdlib.h. The code should actually look like this:

#include <iostream>
#include <direct.h>
#include <stdlib.h>
 
using namespace std;
 
int main()
{
    char CurrentPath[_MAX_PATH];
    getcwd(CurrentPath, _MAX_PATH);
    cout << CurrentPath << endl;
    return 0;
}

Making this simple change will let the code compile in both CodeBlocks 8.02 and 10.05 without problem.

The main.cpp file for Listing 2-2 on page 591 is missing completely. The code in the book contains an extra curly brace at the end, so a number of readers have complained about this particular example. Here is the the source code for the main.cpp file that was supposed to appear in the \Author\BookV\Chapter02\PrecisionFunction folder of the CD.

#include <iostream>
 
using namespace std;
 
int main()
{
    int i;
    cout.setf(ios_base::showpoint);
    cout.precision(4);
    for (i=1; i<=10; i++) {
        cout << 1.0 / i << endl;
    }
    cout << 2.0 << endl;
    cout << 12.0 << endl;
    cout << 12.5 << endl;
    cout << 123.5 << endl;
    cout << 1234.9 << endl;
    cout << 12348.8 << endl;
    cout << 123411.5 << endl;
    cout << 1234111.5 << endl;
    return 0;
}

All of the examples in Book V Chapter 3 will compile just fine and execute without error, but readers have complained that some examples don’t work as expected due to the UAC in Windows Vista and Windows 7. Paths such as /MyData/delims.txt (found in the \Author\BookV\Chapter03\ReadString01 and \Author\BookV\Chapter03\ReadString02 folders) worked fine in the past, but if you encounter problems, change them to something like C:/author/delims.txt to ensure you have rights to read and write the files. As odd as it might seem, you won’t even see a UAC message when the error occursWindows will simply refuse the write the file.

In Book V Chapter 4 you’ll encounter a warning message for the GetDirectoryContents example (found on page 613 and the \Author\BookV\Chapter04\GetDirectoryContents folder) that the & should be surrounded by parenthesis. The affected function is DumpEntry(). The parenthesis should appear around the (data.attrib & _A_SUBDIR) part of the if statement to avoid potential confusion as shown here:

void DumpEntry(_finddata_t &data)
{
    string createtime(ctime(&data.time_create));
    cout << Chop(createtime) << "\t";
    cout << data.size << "\t";
    if ((data.attrib & _A_SUBDIR) == _A_SUBDIR)
    {
        cout << "[" << data.name << "]" << endl;
    }
    else
    {
        cout << data.name << endl;
    }
}

If you changed the /MyData/delims.txt path earlier to avoid UAC issues, you’ll likely also need to change the paths in the \Author\BookV\Chapter04\RenameFile example. This example also uses paths such as /mydata/dog.txt, which could cause problems with Windows Vista and Windows 7. Otherwise, the examples in Book V Chapter 4 should work precisely as they appear in the book with CodeBlocks 10.05.

Finally, the sole example in Book V Chapter 5 should work precisely as shown in the book with CodeBlocks 10.05. If you encounter any other problems with Book V, please be sure to let me know. In the next post I’ll start
with Book VI Chapter 1. In the meantime, keep those e-mails coming to John@JohnMuellerBooks.com.