Duoas 1,025 Postaholic Featured Poster

The problem is so simple you'll laugh...

Your loop says:

while there is no file failure (eof):
    abc <-- read next record
    display abc

What happens is this:
Read record one (no file read failure), display record one.
Read record two (no file read failure), display record two.
...
Read record N (no file read failure), display record N.
Read record N+1 (couldn't: file read failure), display record N (since abc == last record)
While loop terminates.

You need to test the file failure before displaying the record. There are a zillion ways to rewrite this, so I offer my way:

while (true)
{
  if (! file.read( ... )) break;
  abc.showdetails();
  std::cout << std::endl;
}

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

No.

Re-read my first answer. Exceptions immediatly transfer control away from the current block. No code following the exception will ever be executed.

If you really want to, you can wrap the offending code in a try block and re-raise the exception (or, as in the following example, a new exception) later:

bool ok = true;
e1 = new E1();
marker = 1;
try { e2 = new E2(); } catch (...) { ok = false; }
marker = 2;
...
if (!ok) throw "E2() failed to construct.";

BTW. If this is production code get rid of the catch (...) stuff and explicitly list all the exceptions that you are prepared to handle. (Some exceptions are so awful that they actually do need to make it back to the system.)

Duoas 1,025 Postaholic Featured Poster

Your problem is that your input is variable. You can enter one of:

number  operator
  number  number  operator

However, your code only permits the second type of input:

cin >> first >> second >> op;

I'll give you a hint: don't use int variables as the targets of your cin statement. Use strings and convert them to a number if necessary.

Good luck.

Duoas 1,025 Postaholic Featured Poster

Seriously! (Love your avitar!)

One of the major problems I find with people (particularly, newbie programmers) is that they somehow expect the computer to solve their problems.

Natural, because that is how they have used computers so far...

Coding is different. It is important to remember that computers aren't just stupid, they are about as smart as a rock.

When preparing to code anything, take the time to figure out, for yourself, exactly how the problem is solved. Then tell the computer how to solve the problem.

"Just hacking" never works...

Thanks for sharing the cruft. ;)

Alas.

Duoas 1,025 Postaholic Featured Poster

Er, when you post code make sure to place it between 'code' blocks, like this:

[[b][/b]code[b][/b]]
std::cout << "Hello, world!" << std::endl;
[[b][/b]/code[b][/b]]

which looks like this:

std::cout << "Hello, world!" << std::endl;

I assume that filename is a std::string and outdata is a std::ostream. The stream classes are meant for parsing data, not pure string input. Instead, say:

std::getline( std::cin, filename );
if (filename.empty()) filename = "newfile.txt";

outdata.open( filename.c_str() );

A very useful website is http://www.cppreference.com/

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

In short: no.

If I understand your question properly, what you want to do is execute all the code in the try {...} block before the exception transfers control to the catch (...) {...} block.

If that is the case, you need to reconsider how exceptions work. Anytime an exception is thrown control immediately transfers out of the current block and up the chain until it is handled.

This is also reasonable, because the error occurred when marker had a value of 1, not 2. In your example, you don't want marker to have a value of 2 if the E2 constructor throws an exception, since further on you'll try to delete E2 when it doesn't exist...

However, if you just have a list of initializations that must occur before some exception may, put them before the code that may cause the exception.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

Presumably s0 and s1 are the arguments to your function...

Look through and try to see what is happening to each register. I find it convenient to get out the crayons and construction paper and make little boxes with the values of each register in it (seriously).

The result is that register s0 is logically right-shifted by one bit,
and register s1 is also right-shifted by one bit but the MSB gets the bit shifted out of register s0.

In other words, you are treating s0 and s1 as if they were a single, double-sized register and logically shifting the whole thing right by one bit.

This won't make a bit of sense until you get out the paper and colored pencils and draw it.

Hope this helps.

Oh yeah, you should invest in a book called "A Programmer's View of Computer Architecture" by Goodman and Miller.

Duoas 1,025 Postaholic Featured Poster

You are about to get more information than you bargained for... :icon_cheesygrin:

On Windows systems, an EXE file has a tag in its header that declares whether or not to start it as a Console process (using the DOS window) or as a GUI process (without the DOS window). Programs can use both at any time... but the tag simply indicates whether or not to create a new console and attach it to your program when you run it (assuming you didn't start the program from the DOS prompt to begin with... ehe)

Visual C++ adds that "Press any key to continue..." thing just so you get a chance to read what was written to the console window before it disappears after your program terminates. That is, it is a convenience. If you compile your program and run the EXE without the IDE you won't see the prompt (and the console window will appear and disappear just as fast as your program runs).

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

The problem isn't with adding or deleting nodes, it is with using the operator <<. When you say

cout << iteratorNode->data << endl;

you need to be aware of what type of data data is. If it is a simple type (int, float, etc.) or a predefined type (std::string, etc.) then the left-shift operator has already been properly overloaded to send data to cout.

In your case, you have added a node type that does not tell the compiler how to "left-shift" data into the cout stream.

Try adding the following to your node definition:

ostream &operator << ( ostream &outs, const LinkListNode &node ) {
  cout << node.number;
  return outs;
  }

Now, with the operator properly overloaded you shouldn't have any trouble.

PS. Don't use using namespace std; unless you are editing your program's main source file. Library files (such as your linked list node) should refer to the desired namespace explicitly:

std::cout << "fooey" << std::endl;

Doing this will save you a lot of grief when your project gets large.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

Hi all. I'm a newbie to this forum so please forgive the odd question as first post... :-)

I'm writing myself a property class (just because I want to play with them). My property header looks something like this (all the usual clutter is removed):

#define property_readwrite( c, v, r, w ) property::property_t <c, v, &c::r, &c::w, (c::v *)0, (c::v *)0>
...
#define property( a, c, v, ... )  property_ ## a( c, v, __VA_ARGS__ )

namespace property {

template<
  typename Container,
  typename ValueType,
  ValueType (Container::*getf)(),
  void (Container::*setf)( ValueType value ),
  ValueType Container::*getv,
  ValueType Container::*setv
  >
class property_t {
  ...
  };

  ...
}

And I use the "property()" template macro thus:

class Person {
  private:
    unsigned f_age;
    void set_age( unsigned new_age );
    ...
  public:
    ...
    property( readwrite, Person, unsigned, f_age, set_age ) age;
  };

But the compiler complains that I am only supplying three of the six required template arguments. However, when I use the g++ -E option and look at my code I see:

property::property_t <Person, unsigned, (Person::unsigned *)0, &Person::set_age, &Person::f_age, (Person::unsigned *)0> age;

I've tried both the "property()" macro and the more direct "property_readwrite()" macro and both give the same error. Any idea why I am getting this grief?

Thanks in advance.