Also: Why are you writing
DataType *Ptr = new DataType();
instead of simply writing
DataType Obj;
?
Also: Why are you writing
DataType *Ptr = new DataType();
instead of simply writing
DataType Obj;
?
What would you do with variable-length arrays that you can't do better with C++ vectors?
Remove the call to srand from the choose_number function.
As a general rule, you should call srand only once in your entire program.
The FBI, CIA, and the military comes to your house via dragon and breaths fire onto your soul sending you to hell for eternity to burn, simply because you deleted a pointer twice. So next time be aware.
And don't forget Basement Cat!
The results are undefined, which means that a program that does so is permitted to do anything at all.
By default C++ prints floating-point numbers in scientific notation if they are so large that doing so will provide more compact output than fixed-point notation. By sending "fixed" to the output stream, you are asking it to print in fixed-point notation (i.e. with a decimal point at a fixed place in each output number) regardless of value.
If you replace "fixed" by "scientific," it will print in scientific notation regardless.
What happens if you change
... << setprecision(2) << ...
to
... << fixed << setprecision(2) << ...
?
You say: "a smart compiler could optimise this by seeing that the next lines (below) of this function never use the derived part of d1 and d2 and that the base part has an identical content"
No it can't. The only way a compiler could do so would be if the program's behavior didn't change. But the moment you make your program depend in any way on d1 and d2 having different addresses, putting d1 and d2 in the same place would change the program's behavior. So that's not an acceptable optimization.
Different objects have different addresses.
You say "2 different objects share the same memory location if their content are identical."
The thing is: If they share the same memory location, they are not different objects.
IIRC, if a function that is declared to return a value falls off the end without doing so, the C89 standard permits this state of affairs so long as the caller does not try to use the value that the function failed to return. In other words:
int foo()
{
/* This function should return an int but doesn't */
}
int main()
{
foo(); /* Discards the invalid value, so the call is OK */
foo() + 0; /* Undefined behavior */
}
I do not remember for sure whether C99 tightened this rule.
When I try to run your code, it fails because "it" has the wrong time. When I remove the erroneous assignment to "it" in line 6, and the statement that tries to use "it" in line 8, the code prints 4 (correctly), not 6.
So if your code is printing 6, that means that what you're running is different from what you posted. I am not going to waste any more time by trying to read your mind.
If you want help with your code, you have to post the code that is having problems. You can't expect people to figure out what's wrong by looking at different code.
&*raiz means "give me the address of the contents of the thing the pointer raiz is pointing to"; '&' is an operator. It's a bit silly, as by definition raiz itself equals the address of the thing it is pointing to.
Actually, &*raiz by itself would make sense if raiz were, say, an iterator rather than a pointer. What doesn't make sense is to try to assign a value to &*raiz -- unless *raiz has a type with unary operator& defined. Such types are best avoided in any event.
How about explaining what you expect the statement to do? It looks like nonsense to me.
I suspect the solution is
std::vector<passenger*>& passenger_queue::passengers_at_floor(int floor_) const
The point is that at present, passengers_at_floor is permitted to modify its object, but you're passing it a const object, namely the key from a map.
So you need to add const to the definition of passengers_at_floor, as a promise that it will not modify its object. Of course you need to add it to the declaration inside the class as well, not just to the definition.
Please use code tags.
Are you sure that your user isn't picking k instead of K?
Also... Please use code tags.
In your search function, counter should be a reference, not a pointer. So you should delete the * on line 17, and in line 98, you should change "int * counter" to "int & counter".
rvalue-lvalue qualification is not the same as cv-qualification. You can have a const or non-const, lvalue or rvalue variable.
I think you're missing the point.
The C++ standard, clause 5.2.11, says that the result of const_cast<T>(e) is an lvalue if type T is a reference type; otherwise the result is an rvalue. So const_cast<myclass*>(anything) is an rvalue, because the type myclass* is not a reference type.
But, in theory, just reading or writing the value of the this pointer (not what it points to) is "valid" (if the compiler permits it), just like this code is valid, but useless:
void someFunction(int* some_ptr) { delete some_ptr; std::cout << "The pointer I just deleted had address: " << some_ptr << std::endl; some_ptr = NULL; // reading or writing some_ptr is valid and well-behaved after the delete. This also applies to the this pointer. };
I still believe you are mistaken. One of the interesting aspects of pointers is that it is undefined behavior even to copy the value of a pointer to memory that has been freed. Clause 3.7.3.2 of the C++ standard says that once memory has been deallocated, any pointer to that memory becomes invalid, and any use of such a pointer, even to pass it to a function, results in undefined behavior.
This behavior is inherited from C, and comes from some processor architectures that use different registers for addresses and other data. In such processors, the mere act of putting an address into an …
To give you an idea of how many worms are in this particular can, I'd like to isolate two lines from the OP's code:
delete this;
const_cast<myclass *>(this) = 0;
The program is already broken at this point, because once you have executed "delete this;" any subsequent use of this is invalid.
Come to think of it, the second statement is invalid anyway, even without "delete this", because this is not an lvalue. And even if it were, casting it to myclass* yields an rvalue, to which you can't assign.
If your compiler accepts this program, your compiler is broken.
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.
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).
Line 15 uses hobby as the first argument of strcpy without ever having initialized it.
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.
And do you have a declaration of the string type in scope?
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.
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.
Why aren't you using a vector?
if (sex=='m' || sex == 'M') { /* ... */ }
Note the use of == rather than = and the use of ||.
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.
Well, it is. Look on line 29: You're using array[j] but you have never given a value to j.
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.
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.
Your program never reads more than the first word of the input file.
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.
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.
Subtract.
In your program, nums is just a pointer to the first element of array1.
If your assignment includes a requirement to allocate a new array, why not start by doing that and then figuring out what to do with it?
Please show the full contents of your input file, as well as exactly what the program prints when you run it. Do not try to interpret or explain what the program does. Just the facts, please.
OK, then I take it you don't need any more help or advice. See you around.
Please show the entire program, and explain exactly what you did in order to run it.
It works fine over here, so I am thinking that something is odd about how you're running it.
So apparently your implementation doesn't do anything to an output file unless you try to write something.
myfile is an ofstream, which means that you are intending to use it for output. Calling myfile.open("exercise.txt") says that you intend to write a file named exercise.txt -- which means that anything that was in that file already is about to vanish.
Read a file that contains text, in the form of words separated by whitespace (spaces, tabs, or newlines).
1) Print the longest and shortest words.
2) Print each word, together with how many times it occurs.
3) Print every word that occurs only once.
You have (at least) two problems.
The first one is that the expression test % p in line 27 should be p & test.
The second is more subtle. Look at the loop in lines 23-32. You will see that the only way that your prime function can ever return anything but 0 is if the variable "test" reaches 0.
However, before it reaches 0, it has to reach 1--and when it does, line 27 will compute p % test (after you've fixed it) with test equal to 1, which will always be zero. So the function will always return zero.
I'm not going to tell you how to solve this problem, because I've already done enough of your homework for you.
That's because you have the same problem in your prime function: You never change the value of the variable test, so the program never terminates.