p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5 (http://p2p.wrox.com/forumdisplay.php?f=394)
-   -   Chapter 3 - Exercise 4 - Solution gives no output (http://p2p.wrox.com/showthread.php?t=78167)

Cryotic February 12th, 2010 01:09 PM

Chapter 3 - Exercise 4 - Solution gives no output
 
I did not find out how to write this application/exercise and decided to peek at the code/answer available for download. But when running/debugging the code provided for a solution nothing is ouput to the command line.

Below is the code/solution which did'nt work. Even if it should work, is there a specific reason for the hexadecimal values to be set with the values they were or could you set them to whatever you prefer?


// Soln3_4.cpp

/* This is a good test of your ability to work with hexadecimal values
and individual bits. Remember, each hexadecimal digit defines 4 bits.
There are six different combinations for the file open mode,
read, write or append combined with each of text and binary modes.
You can therefore set modes in a loop with six iterations.
*/

#include <iostream>
using std::cout;

const int text = 0x01;
const int binary = 0x02;

const int read = 0x10;
const int write = 0x20;
const int append = 0x40;

int main()
{
int mode = 0; // File open mode
for(int modeSelect = 0 ; modeSelect<6 ; modeSelect++)
{
// Set a different mode for each loop iteration
switch(modeSelect)
{
case 0:
mode = text | read;
break;
case 1:
mode = text | write;
break;
case 2:
mode = text | append;
break;
case 3:
mode = binary | read;
break;
case 4:
mode = binary | write;
break;
case 5:
mode = binary | append;
break;
}

// Determine the mode that is set and output it
if (mode & text)
cout << "mode is (text,";
else if (mode & binary)
cout << "mode is (binary,";

if (mode & read)
cout << "read)\n";
else if (mode & write)
cout << "write)\n";
else if (mode & append)
cout << "append)\n";
}

return 0;
}

meteormatt April 19th, 2010 08:06 AM

Quote:

Originally Posted by Cryotic (Post 253515)
I did not find out how to write this application/exercise and decided to peek at the code/answer available for download. But when running/debugging the code provided for a solution nothing is ouput to the command line.

Below is the code/solution which did'nt work. Even if it should work, is there a specific reason for the hexadecimal values to be set with the values they were or could you set them to whatever you prefer?


// Soln3_4.cpp

/* This is a good test of your ability to work with hexadecimal values
and individual bits. Remember, each hexadecimal digit defines 4 bits.
There are six different combinations for the file open mode,
read, write or append combined with each of text and binary modes.
You can therefore set modes in a loop with six iterations.
*/

#include <iostream>
using std::cout;

const int text = 0x01;
const int binary = 0x02;

const int read = 0x10;
const int write = 0x20;
const int append = 0x40;

int main()
{
int mode = 0; // File open mode
for(int modeSelect = 0 ; modeSelect<6 ; modeSelect++)
{
// Set a different mode for each loop iteration
switch(modeSelect)
{
case 0:
mode = text | read;
break;
case 1:
mode = text | write;
break;
case 2:
mode = text | append;
break;
case 3:
mode = binary | read;
break;
case 4:
mode = binary | write;
break;
case 5:
mode = binary | append;
break;
}

// Determine the mode that is set and output it
if (mode & text)
cout << "mode is (text,";
else if (mode & binary)
cout << "mode is (binary,";

if (mode & read)
cout << "read)\n";
else if (mode & write)
cout << "write)\n";
else if (mode & append)
cout << "append)\n";
}

return 0;
}

There may be have some errors.

fortran_ii April 19th, 2010 08:57 AM

I ran the solution code, and saw output which appears to be as expected.

The hex values need to be such that {text,binary} and {read,write,append} may each be uniquely decoded from one int value. The values used in the solution satisfy that requirement.


All times are GMT -4. The time now is 02:50 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.