arkoenig 340 Practically a Master Poster

Lines 27, 29, 31, 33: This technique of implementing operator++(int) and operator--(int) is wrong. These operators return the previous value of the object, which you can accomplish along these lines:

Iterator operator++(int) {  //Note: Iterator, not Iterator&
    Iterator ret = *this;  // Save a copy of *this
    ++elem;
    return ret;            // Return the saved copy
}

The error message you cite refers to line 287. Which line is that in what you posted?

arkoenig 340 Practically a Master Poster

You haven't shown the definitions of your classes. I presume your MyChild classes are derived from MyBase. In that case, I see nothing wrong with your code except that your MyBase class must have a virtual destructor for it to work correctly.

arkoenig 340 Practically a Master Poster

If you're going to generate a list of primes, rather than testing whether a single integer is prime, then why not use the list itself for the divisors?

arkoenig 340 Practically a Master Poster

Here is what you need to know.

arkoenig 340 Practically a Master Poster

Your download function doesn't contain a return statement, which means that it doesn't return a value. So when you execute line 34, the variable "lines" is set to None. In line 35, when you try to select elements of Lines, the Python system tells you that you can't select elements of None.

If your goal is for download to return the value of the local variable "lines," which is completely distinct from the variable "lines" in line 34 of your program, then you need to put

return lines

at the end of your download function (appropriately indented, of course).

vegaseat commented: good point +13
Gribouillis commented: on target +5
arkoenig 340 Practically a Master Poster

I'm trying to fix these things you pointed ... just one question ... about defining the == and != as friends ... how should I do that?

Go to your nearest C++ textbook and read about friend functions.

arkoenig 340 Practically a Master Poster

Saying "it just won't work" doesn't really give readers much to go on.

However, I will say that your iterator has several problems:

1) It defines == and != as members rather than friends. Worse, it defined them only recursively. In other words, you're defining it1==it2 by saying that it's the value of it1==it2. That can't work.

2) You define prefix ++ and -- but not postfix ++ and --.

3) You define = with a pointer as its right-hand side but not an iterator.

4) None of your member functions are const, so you can't do anything useful with const iterators.

5) For some reason, you define == and != comparisons between interators and pointers, but not between pointers and iterators. I'm not sure why you want to bother, but it makes no sense to define only one of them.

I suggest fixing these problems before looking further.

arkoenig 340 Practically a Master Poster

Option #3 is by far the best, in my humble but correct opinion.

arkoenig 340 Practically a Master Poster

If you are trying to solve the problem of putting eight queens on a chessboard so that none of them attacks any of the others, your program is dramatically larger than it needs to be. You can find a shorter solution here.

arkoenig 340 Practically a Master Poster

When the call to scanf in line 18 returns, it leaves unread in the input stream the first character that cannot possibly be part of an integer. In this case, that is the newline that ends the input.

As a result, the first call to gets reads an empty line, namely the one that is terminated by the newline that was left in the input.

Two additional comments:

1) You should never use gets, because there is no way to specify a limit on how much memory it will overwrite. As a result, any program that uses it has potential security problems that are impossible to fix.

2) This is a C program. Why are you posting it in a C++ forum?

arkoenig 340 Practically a Master Poster

I mean that once your recursivecandy function has been called, there is no legitimate way to get out of it. Consider:

int foo()
{
    return foo();
}

There is no way out of this function, because in order to return, it must first call itself, and in order for that call to return, it must first call itself again, and so on. Let's try to solve that problem by making the call conditional:

int bar(int n)
{
    if (n < 100)
        return bar(n+1);
}

Now there are two ways to leave this function. One is by falling off the end, which returns garbage; the other is by calling itself first. So again there is no legitimate way to leave it.

Your recursivecandy function has the same problem as my bar function.

arkoenig 340 Practically a Master Poster

I don't even need to ask how your program is supposed to work because it can't -- there is no way for recursivecandy to return that does not involve a recursive call, so there is no way for it ever to return a meaningful value.

arkoenig 340 Practically a Master Poster

Line 15 uses hobby as the first argument of strcpy without ever having initialized it.

arkoenig 340 Practically a Master Poster

If you really want to define a bidirectional iterator that is not a random-access iterator, just do it. Define a class that contains one of your random-access iterators, and then implement just the bidirectional-iterator operations on it. Each operation would be implemented in terms of the corresponding random-access iterator operation.

arkoenig 340 Practically a Master Poster

Yikes, is that really Andrew Koenig or a very good Impersonator!!!

I'm not impersonating anyone--but it's not exactly obvious how I might go about proving it if you don't already know me. If you do, I could tell you something about myself that an impersonator would be unlikely to know--but if you don't know me, you also don't necessarily know how to distinguish what I tell you from information that might be publicly available.

If you do an online search for me, you will find lots of hits, both for me and for the actor with the same name who vanished last year in Vancouver and was later found dead. There's no relation between us that I know, although we had been friends on Myspace.

You'll find lots of copies of the photo I use for my avatar, too, so an impersonator could have snarfed that from anywhere. I can tell you lots of things about the kitten that was on my shoulder when that picture was taken, but of course I could be making them up.

So yes, it's really me; but I can't think of a convenient way to prove it.

I can think of a somewhat inconvenient way to prove it. Pick up a copy of Accelerated C++ and look at the preface, which tells where I live. Look me up in your phone directory of choice--my phone number is listed. Call it, and give me a piece of information that you'd like …

arkoenig 340 Practically a Master Poster

Every random-access iterator is a bidirectional iterator.

arkoenig 340 Practically a Master Poster

You haven't posted all of your code, so it's hard to know what all your problems are. However, the code that you posted for charStack::push ahd charStack::pop is broken. To see this, try creating a charStack object, pushing a few characters onto it, and then popping them one at a time and printing them. What you get should, of course, be the same as what you pushed, just in reverse order; but that's not what your code actually does.

arkoenig 340 Practically a Master Poster

And do you have a declaration of the string type in scope?

arkoenig 340 Practically a Master Poster

I see nothing obviously wrong with the code you've shown. However:

1) You should be aware that writing a parameter of the form string foo[] means exactly the same as if you had written it as string *foo -- that is, the parameter is just a pointer.

2) If you don't have a declaration of the string type in scope, the compiler won't know what it means.

arkoenig 340 Practically a Master Poster

How can I do that? Can you show me a code sample for it? please...

mike_2000_17 just posted a code sample.

arkoenig 340 Practically a Master Poster

On line 10 you say that you have a function named askGuess that takes no arguments and returns a char. On line 55 you define a function named askGuess that takes a string argument and returns a char.

Because these functions have different argument types, the compiler assumes that they are different functions.

On line 38, you call askGuess with no arguments. The compiler assumes that you are calling the function that you declared in line 10. That function is not defined anywhere, so your program doesn't link successfully.

Incidentally, while we're at it, the askGuess function that you define starting on line 55 is claimed to return a char, but you don't actually have a return statement anywhere in it. Therefore, if you were to call it, it would return garbage.

arkoenig 340 Practically a Master Poster

The problem is that there's a template in the standard library named plus, so your program is conflicting with that definition. Change the name to something else.

arkoenig 340 Practically a Master Poster

Before you learn about functions, I suggest you learn about how to write a loop and how to change the value of a variable.

arkoenig 340 Practically a Master Poster

In the particular original examples, the eventual result is the same.

arkoenig 340 Practically a Master Poster
arkoenig 340 Practically a Master Poster

In the case you've shown, it doesn't matter. However. if you want to use a constructor that takes more than one argument, you have to use the first form. For example:

string stars(10, '*');

defines a variable and initializes it to ten '*' characters. If you were to use form (b), you would have to write

string stars = string(10, '*');

because if you were to write

string stars = 10, '*';

it would be a syntax error, and if you were to write

string stars = (10, '*')

it would be equivalent to writing

string stars = '*';

which is a type error (because there is no string constructor that takes a single char as its argument).

arkoenig 340 Practically a Master Poster

I'm sorry to say that both answers are wrong.

Example (a) uses the overloaded constructor that matches the argument, which in this case has type const char*. The string type is actually an instantiation of the basic_string template, so that string is equivalent to basic_string<char>; but putting that detail aside, it is useful to think of the first example as asking for string(const char*).

Example (b) uses = along with an initializer, which means that it is a request to use the copy constructor (conceptually string(const string&)). The expression after the initializer is converted to type string, which means that the second example is equivalent to

string color = string("red");

Theoretically, then, it is a request to construct a temporary object of type string, initialize color as a copy of that object, and then destroy the temporary object.

However, the compiler is permitted to optimize away this copy (provided that the relevant copy constructor is accessible, which in this case it is), in which case the two examples may well generate identical code.

So the first answer, saying that there is no difference between these two examples, is not quite right; and the second, saying that it is using overloaded operator =, is just plain wrong.

arkoenig 340 Practically a Master Poster

Why aren't you using a vector?

arkoenig 340 Practically a Master Poster

It's hard to answer your question without knowing what GetHeight does.

arkoenig 340 Practically a Master Poster

There is no any square root algorithm that guarantee a 100% accurate result. For example: Let's say what is 100% accurate result of square root of 2?

The IEEE floating-point specification requires the square root operation to return the representable number that is the correctly rounded value of the infinite-precision square root. So in that sense, it is reasonable to talk about a particular implementation as being accurate or not.

arkoenig 340 Practically a Master Poster

Paul is right. And I apologize for not having mentioned his effort earlier; it slipped my mind.

arkoenig 340 Practically a Master Poster

That would work too.

arkoenig 340 Practically a Master Poster

@narue: In line 11 of your example, shouldn't value%2 be value%indices?

arkoenig 340 Practically a Master Poster

if (sex=='m' || sex == 'M') { /* ... */ }

Note the use of == rather than = and the use of ||.

arkoenig 340 Practically a Master Poster

You haven't actually said what you want it to do, so it's hard to say whether it's ok.

Copying a vector copies its elements. However, if those elements are pointers, copying a pointer doesn't copy the object that the pointer points to. This is true whether or not the pointer is part of a vector, so it doesn't really have anything to do with vectors.

arkoenig 340 Practically a Master Poster

Sorry, no more hints. I've already told you all you need to know.

arkoenig 340 Practically a Master Poster

You can use iterators without using a "for" statement.

My earlier remarks show you exactly what you need to do.

I can't be more specific because (a) You haven't been specific about what data structures you're using, and (b) If I were, I would be doing your homework for you.

arkoenig 340 Practically a Master Poster

This will tell you what to do.

arkoenig 340 Practically a Master Poster

Don't use a for statement. The trouble with for statements is that they push you in the direction of iterating over a single range of values, and the problem you're trying to solve involves iterating over two ranges of values at once.

What you want to do is have two iterators, each marking a place in one of the input lists. Now you want to repeat the following steps:

1) If both iterators are at the ends of their respective lists, you're done.

2) If one iterator is at the end of its list and the other isn't, copy to the output the element to which the iterator refers that's not off the end; then increment that iterator.

3) If you got here, then neither iterator is at the end of its list. Therefore, both iterators refer to elements. Compare the elements, copy the smaller of the two to the output, and increment the corresponding iterator.

arkoenig 340 Practically a Master Poster

Why are you asking?

arkoenig 340 Practically a Master Poster

The trouble is that unless you've verified that an int is capable of containing the value 600851475143, I don't see how you can count on the results.

What happens if you execute this on your implementation?

cout << 600851475143 << '\n';

arkoenig 340 Practically a Master Poster

How many times do you expect the loop in lines 28-34 to execute?

arkoenig 340 Practically a Master Poster

Not every C++ implementation is capable of supporting an integer as large as 600851475143. So if you want to use a C++ implementation that doesn't support integers that large, you have a few choices:

1) Figure out how to represent such large integers for yourself.

2) Find an extended-precision integer arithmetic package and use it.

3) Do your computations in floating-point. This may sound like a surprising approach, but it's workable if you're careful.

arkoenig 340 Practically a Master Poster

So, what should I use instead?

You should arrange for your program to use >> as many times as necessary to read each word, so that you can see which is the longest and which is the shortest.

arkoenig 340 Practically a Master Poster

Well, it is. Look on line 29: You're using array[j] but you have never given a value to j.

arkoenig 340 Practically a Master Poster

But this line
"infile >> mystring;"
was supposed to read the whole string

No -- when you use >> to read into a string, it reads one word.

arkoenig 340 Practically a Master Poster

If your program reads only the first word, it is impossible to get the shortest and longest word, because you have no way of knowing what those words are.

arkoenig 340 Practically a Master Poster

Your program never reads more than the first word of the input file.

arkoenig 340 Practically a Master Poster

Your program reads and prints the first word in the input. The first word is "I". If that's what your program printed, it is working correctly.

arkoenig 340 Practically a Master Poster

There are 4 salespeople and 5 products. Let's number the salespeople 0 through 3 and the products 0 through 4. Then the problem is asking you to create a 4 by 5 array, where each row represents a different salesperson and each column represents a different product.

So, for example, sales[2][3] would hold the information for salesperson number 2 and product number 3.

Your problem is to read all of the slips. For each slip:

1) Find which array element corresponds to the information on the slip.
2) Update that array element with the information from the slip.

When you are done, figure out how to print the whole array.