sepp2k 378 Practically a Master Poster

Even if you make abc global, it still doesn't work in Visual Studio 2015

Because your function is returning a copy of abc and that copy is a temporary object that goes out of scope at the sequence point, just as deceptikon explained.

returnValue seems to point to I'm not sure where, but clearly it's not pointing to whatever c_str() returned?

returnValue is exactly equal to the pointer retunred by c_str() - after all you just assigned it to that on the line before. However you called c_str() on the temporary object, not on abc and c_str() on the temporary object returns a different pointer than c_str() on abc, specifically it returns a pointer to temporary storage that gets freed when the temporary goes out of scope, which is why you see those results.

AssertNull commented: Good explanation +6
sepp2k 378 Practically a Master Poster

Using your first definition account1.compareTo(account2) will call account2.name.compareTo(account1.name) and using your second definition it will call account1.name.compareTo(account2.name). So using the result of the comparisson will be the other way around and thus the sort order is reversed.

sepp2k 378 Practically a Master Poster

This isn't what's causing your syntax error, but Java doesn't have a type named sum nor do you define one anywhere in your prorgram (unless you defined it in a different file that you didn't show), so sum c is a type error (both on line 7 and line 8).

The compiler will probably complain about the (sum c)-> - it is non-terminated (no semicolon) and there isn't some value or method that the "pointer-to" operator references.

Java doesn't have pointers - -> is only used for lambdas (which would make (sum c) the argument list. But you're definitely right that a ; is missing. And there needs to be a function body after the ->. Also it makes no sense to have a lambda there.

sepp2k 378 Practically a Master Poster

There's only a difference when you use the return value of the increment for something. So the two loops are completely equivalent.

sepp2k 378 Practically a Master Poster

Please post a compilable and runnable piece of code that reproduces your problem.

sepp2k 378 Practically a Master Poster

It'd have helped us if you had described in what way the code did not behave as expected, but your problem is that you access the matrix out of bounds (the matrix can hold 2x2 items, but you're trying to store 3x3).

Also you're printing "1th" and "2th" when the correct forms would be "1st" and "2nd" (also "0th" isn't very pretty, you should probably add one the index when displaying it, so that the first item is referred to as the first, rather than the zeroth).

tobyITguy commented: Exactly. Descriptions are important +1
sepp2k 378 Practically a Master Poster

no instance of overloaded function "System::Console::WriteLine" matches the argument list argument types are: (const std::string)

I've never used C++/CLI, but it looks like std::string is a distinct type from System::String and presumably Console::WriteLine only works with the latter as it is a .net method and std::string is a C++ class.

Presumably C++/CLI offers some way to convert between the two string types, but I imagine in this case it'd be easiest to just either only use C++ strings and C++ IO functions or .net strings and .net IO functions.

Anyway, the problem here is that the System class is not included.

No, it's not. using namespace System; just means that you're then allowed to write Console::WriteLine instead of System::Console::WriteLine. It does not change for which types the WriteLine method has overloads.

sepp2k 378 Practically a Master Poster

If you know how many elements you're going to add to the vector (or you at least have a reasonable upper bound), you can also use reserve to reserve the proper amount of memory up front, preventing reallocations. Then you'd have exactly one move constructor call per push_back and no calls of the copy constructor.

sepp2k 378 Practically a Master Poster

gcc produces a binary executable, not a shell script. To run it, type ./main.out.

CattleOfRa commented: Really stupid of me... thank you so much +0
sepp2k 378 Practically a Master Poster

EOF is not captured in the char data type. EOF is represented as an int with the value -1. Now you say "why can't we just use a char with the value -1 - isn't that the same thing?". And the answer is: because, depending on the encoding, there are already 256 valid character values. So the char -1 (or 255 depending on whether char is signed or unsigned) already represents a valid character - it can't also represent EOF.

What getchar does is it represent the 255 valid character values as ints from 0 to 255 and it represents EOF as -1. So 255 and -1 are two very different results.

sepp2k 378 Practically a Master Poster

No a char value can not be larger than what fits inside a char. However what getchar returns does not have to be a char value. It could also be EOF.

Except for the missing &, your scanf line is perfectly fine.

When scanf reaches the end of file (or any unexpected input), it will return a number that is less than the expected one. It will not write anything into the given pointers. Therefore there is no need for an EOF value with scanf.

sepp2k 378 Practically a Master Poster

You need to #include <stdio.h> to get the declaration of fopen. As it is now you're implicitly declaring fopen in a way that is incompatible with its definition. You should be getting a warning about that (and in C99 and later you should instead be getting an error that you're using a function without a declaration).

Once you do that, you should be getting another warning that you're implicitly converting the result of fopen from FILE* to int. That's because fopen doesn't read anything from the file nor does it return an int. fopen opens the file and then returns a file pointer. That file pointer can then be given to other file IO functions (like fread, fwrite, fgets, fscanf, fprintf etc.), which will actually read from or write to the file. Once you're done working with the file, you need to close it by passing the file pointer to fclose.

So no, you're not currently doing it correctly. The number you're printing is simply the numeric value of whichever pointer was returned by fopen. It has nothing to do with the contents of the file.

sepp2k 378 Practically a Master Poster

is the + operation returning a new big integer object?

Yes, exactly.

sepp2k 378 Practically a Master Poster

You're right, that when you do something = new Something(); that basically assigns a "very safe pointer" (a.k.a. reference) to something. In fact when you do something = anything;, you're assigning a reference.

Also note that x += y is just a shortcut for x = x + y;. So when you do:

BigInteger current = enumerable1.getCurrent();
current += enumerable2.getCurrent();

You're assigning a reference to current on the first line and then assigning another reference on the second line. In C, the equivalent would look something like this:

BigInteger* current = getCurrent(enumerable1);
current = add_big_integer(current, getCurrent(enumerable2));

Where add_big_integer has the signature BigInteger* add_big_integer(BigInteger*, BigInteger*). The important thing here is that the second line is not:

*current = *add_big_integer(current, getCurrent(enumerable2));

Assigning to a non-ref/out variable in C# never changes the object that the variable currently refers to - it simply makes it refer to something else. In order to change the object itself, you either need to assign to one of the object's properties (or variables if it has any public ones) or call a mutating method on it (i.e. method that changes the state of the object).

However the BigInteger class does not have any mutating methods, so you simply can't do this.

sepp2k 378 Practically a Master Poster

How long are the strings you enter? If the first string has more than 2 characters, your first call to gets overflows your array. Likewise your second call to gets overflows if the second string entered is 2 * sl characters long or longer.

Either way the call to strcat will overflow str unless str2 is the empty string.

Further the arguments given to strcpy must not overlap, so your call to strcpy always invokes undefined behavior. What is the goal of that call?

sepp2k 378 Practically a Master Poster

Are you sure that that's the definition of check that's in scope when you call it? Do you perhaps have any other definitions of check in your program, perhaps even one that takes two arguments?

In any case, it would help if you gave us a reproducible code sample of your problem, that is a piece of code that, when we run it, produces the error you get.

sepp2k 378 Practically a Master Poster

If you do make them variables, dircopy $src $dst will work fine.

sepp2k 378 Practically a Master Poster

Learning Java on Arch Linux is no different than learning Java on any other system. You don't need an emulator - both the Oralce JDK and OpenJDK are available for Linux natively, as are all commonly used Java IDEs (eclipse, netbeans, IntelliJ).

So your steps for learning Java would be: pick the Java learning resource of your choice (be it a book, website, online course, personal tutor, whatever - this is not affected by your using Linux), install the JDK (pacman -S jdk8-openjdk - you might also want to install openjdk8-doc to view the Java docs offline and/or from within your IDE and openjdk8-src if you want to look into the sources for deeper understanding), install the IDE or editor of your choice, start programming.

sepp2k 378 Practically a Master Poster

You check the file size after you've already opened the file for writing. Since opening a file for writing creates an empty file with that name (overriding the existing file if it exists), this means that a) the file will always exist and b) it's size will always be 0.

You need to check the size before opening the file instead.

sepp2k 378 Practically a Master Poster

It is indeed better to use printf("%s", message) than printf(message), but only because it prevents problems if the string contains characters that have special meaning to printf (like %), which isn't the case here anyway. As long as message does not include special characters, printf(message) will work fine.

Replacing message with (const char*)&message[0] makes no difference at all. Arrays decay to pointers to their first element implicitly and converting from a pointer to a const pointer also happens implicitly. There is no need for an explicit cast there.

sepp2k 378 Practically a Master Poster

By providing the flags mentioned in the error message: -std=c99 for standard C99 mode or -std=gnu99 for C99 with GNU extensions. For C++11 mode just replace 99 with 11.

sepp2k 378 Practically a Master Poster

Your joueur array holds only one element, but your index goes up to 1 (which would be the second element), which is out of bounds.

sepp2k 378 Practically a Master Poster

When I try to compile this program in gcc I get this error

As the error message says, you need to supply the mentioned language options to make the code compile. Alternatively, if you want to make your code compile in C89 mode, you need to replace char *p = message with p = message and then put char *p; at the beginning of the function. I'd personally prefer using C99(or even C11) though.

Also I'm having trouble understanding the for loop because I don't see a condition that says, if we hit '\0' we are done.

The condition is *p. In C an expression counts as false if it evaluates to 0 and true otherwise and \0 is simply another way of writing 0. So the conditions *p, *p != 0 and *p != '\0' are all equivalent.

sepp2k 378 Practically a Master Poster

To tell you where you went wrong, it'd be good to know how you got to ten. For each iteration of the outer while loop what's the value of i going in, what's the value of j after the inner while loop and what's the value of i after incrementing it by j?

sepp2k 378 Practically a Master Poster

Did you create the text file on Windows? If so, it probably uses '\r\n' as the line ending, so after stripping the '\n', the passwords will end up as 'password\r' and thus not compare equal to 'password'.

That would explain why the problem does not occur in Windows as Python (like most languages) will automatically translate '\r\n' to '\n' when opening files in text mode on Windows (and only there).

sepp2k 378 Practically a Master Poster

What you posted does not look like source code, it looks like what you might see when you open a binary file in a text editor. Presumably you downloaded the zcode file (which is a bytecode format), not the source code. I do not think the source code of Zork is publically available at all, which would explain why you were unable to find it.

PS: Neither Zork nor Adventure were written in Python, so even if you did find the source code, it wouldn't be in Python and reading it would probably not help you at all with learning Python.

sepp2k 378 Practically a Master Poster

use 'out' parameter

There's no such thing in C++.

sepp2k 378 Practically a Master Poster

A getUserInput function simply can not be sensibly defined with that signature. It either has to return something other than void or it has to take its parameters by reference (or pointer), so that it can change them.

sepp2k 378 Practically a Master Poster

I'm not sure what's left to be explained. In your example the third case calls split with zero as the second argument and in the fourth case you call it with no second argument. As documented this causes trailing empty strings to be discarded.

sepp2k 378 Practically a Master Poster

From the Java docs (emphasis mine):

If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

The one-argument version of split is equivalent to the two-argument version with n being zero.

sepp2k 378 Practically a Master Poster

You're only getting one occurence because you're only calling find once. If you use a while-loop instead of an if, you'll call it until it returns false, which will give you all the matches.

sepp2k 378 Practically a Master Poster

First of all you need to include string.h, not string, to get the declaration of strcmp (or even better keep including string, use the string class and don't bother with strcmp at all).

Secondly A is an array of chars, not strings or char pointers, so A[i] is a char and thus not a valid argument to strcmp. It also can't possibly be equal to "new" for that reason.

sepp2k 378 Practically a Master Poster

I don't think it's a good idea to define only one class for all arithmetic expressions and I don't think that's what your TA meant either. What you can do is to define one class that corresponds to the a1 opa a2. That class would look like your Add and Sub classes except it would also contain a member for the opa, which you could define as an enum.

sepp2k 378 Practically a Master Poster
Cabin(8) = Cabin(8/2) + 1
         = Cabin(4) + 1
         = Cabin(4/2) + 1 + 1
         = Cabin(2) + 1 + 1
         = Cabin(2/2) + 1 + 1 + 1
         = Cabin(1) + 1 + 1 + 1
         = 0 + 1 + 1 + 1
         = 3

That's why the answer is 3.

sepp2k 378 Practically a Master Poster

Oh, I seem to have misread the last sentence of your question (I thought you wanted us to explain what an AST is and how to implement it in Java).

To answer your actual question: I think by "specify" the assignment simply means "define". That is it wants you to define the classes that make up the AST.

sepp2k 378 Practically a Master Poster

An abstract syntax tree is a tree where each node represents a syntactical element and each child of a node represents a sub-element of that element.

For example for the statement set x := y + 42; the AST would consist of an assignment node whose left child node would be an identifier node with the value "x" and whose right child would be an addition node. The addition node's left child would be an identifier node with the value "y" and its right child would be an integer literal node with the value 42.

To represent an AST in Java you'd define a class for each type of syntactical element that your language contains, using abstract base classes and inheritance to allow for the fact that multiple types of elements can be used as child nodes for some nodes (for example the child nodes of most expressions are themselves arbitrary expressions, so you'd have an abstract base class called Expression from which classes like Addition and IntegerLiteral inherit, so if a node has a member of type Expression, either an Addition or an IntegerLiteral (or any other type of expression that you implement) can be used there).

sepp2k 378 Practically a Master Poster

The power function has three returns. The first is inside the first if, the second (which is wrongly indented) is inside the second if and the third is outside of any if. The third will only execute if none of the previous ifs were executed (just like the second will only execute if the first did not execute (and the if condition is true, of course)), so you can think of it as being inside an implicit else, yes.

sepp2k 378 Practically a Master Poster

but the first sprintf works, the one that writes toDB[0] to param1

The first sprintf causes undefined behavior just like the second does. Some times undefined behavior means that you get a segfault and some times it seems to work fine. That doesn't change the fact that it's undefined behavior.

sepp2k 378 Practically a Master Poster

The return type of your * operator is vector. So when you return a double from that operator, it gets automatically converted to vector (via your constructor - if you don't want implicit conversion from double to vector, mark your constructor as explicit). However there's no implicit conversion from vector to double, so trying to assign the vector v2*v1 to the double z is an error.

Since there's no reason for the result of multiplication operator to be a vector, you should probably just change the return type to double.

sepp2k 378 Practically a Master Poster

The error means that the readUser function expects an argument of type char*, but you're giving it a char.

sepp2k 378 Practically a Master Poster

When you say "functor" are you using the term in the C++ sense (i.e. a callable object) or something else?

And would a good example be the most simple one that shows you how to create one or something that shows you where they'd be useful?

sepp2k 378 Practically a Master Poster

<string.h> (also available as <cstring>) is a C header that declares functions that work on C strings (i.e. 0-terminated char*s). The std::string class is defined in <string>. You'll also need to refer to it as std::string as it's located in that namespace.

sepp2k 378 Practically a Master Poster

When you say 'member' are you refering to the, what would be an object if the structure were a class or something else?

No, he's talking about what comes after the . in foo.bar (foo is the object, bar is a member of that object) or in other terms: members are the things you define inside the class/struct/union body.

sepp2k 378 Practically a Master Poster

I assume it only looks at the last 5 bits of the right operand to << and ignores anything else (presumably that's how it's implemented in the CPU). So 32 is the same as 0, 33 as 1 etc. The reason it's allowed to do that is that, as I said, it's undefined behavior and thus any behavior is allowed.

sepp2k 378 Practically a Master Poster

Shifting an n-bit integer by m bits when m > n invokes undefined behavior in C and C++.

If you change your code like this, it will give you 0 from count 32 onwards as you'd expect:

unsigned int t = 1;
for (unsigned int p = 0; p < 40; p++)
{
    cout << "Count: " << p << " - "<< t << endl;
    t = t << 1;
}

Here I made two changes to your code:

  1. Instead of shifting t by p, I shift t by one and store the result in t. This will still lead to t having been shifted p times, but since I only ever shift by 1 bit at a time, I don't invoke undefined behavior.

  2. I made t into an unsigned integer. The reason for that is that signed integer overflow invokes undefined behavior as well.

sepp2k 378 Practically a Master Poster

A destructor is called (automatically) when an object is destroyed. It does not itself destroy the object. An object is destroyed when it goes out of scope or when delete is used on it (in case of objects created using new).

You should never call a destructor manually unless you're doing advanced manual memory management (like defining your own allocator using placement new).

sepp2k 378 Practically a Master Poster

Can you show us the other files (or rather a minimal version of them that still produces the same error message) in the project and how you build the project?

sepp2k 378 Practically a Master Poster

Use a while loop.

sepp2k 378 Practically a Master Poster

So I just started using the Goto command

Perfect time to stop.

But when I type in follow or Follow it gives me the text that I intend ( You get up and go to the door) but it still gets to the first goto it finds and sends me back to the beggining

That's because the goto isn't part of the else-block. It's outside of the if-else and thus gets executed either way (same as the print statement before it other than the first one).

To clarify this, here's what your code looks like indented correctly:

if ((sof=="follow")||(sof=="Follow"))
{
    cout<<"*You get up and go to the door*"<<endl;
    cout<<"-Well thats it for now xD"<<endl;
}
else
    cout<<"*5 hours later*"<<endl;

cout<<"-So, not getting anywhere, huh?"<<endl;
cout<<"Let me just warp you back a few hour so you can make the 'right' choice."<<endl;
cout<<"*You feel a strange buzz*"<<endl;
cout<<"So here we are at your first choice."<<endl;
cout<<"-Do you 'stand' there and TRY to get me out of your head..."<<endl;
cout<<"Or do you 'follow' the strange noises coming from the door?"<<endl;
goto firsttp;

As you see, everything other than "5 hours later" happens regardless of whether the if- or the else-block was hit. To add the other statements to the else-block, you'll need to add braces around them.

And stop using goto.

ddanbe commented: Wise advice! +15
sepp2k 378 Practically a Master Poster

You don't link individual functions - you link files. If all your files are added to the same project, the compiler (or rather the linker) will link all your files together.

If you get error messages about missing definitions, that means that the file with the missing definition is not part of your project or for some other reason (like a broken makefile or project file) does not get linked.

If you get error messages about missing declarations, that has nothing to do with linking. It simply means that you did not properly declare the function in your header file or did not include the header file in the file where the function is called. It may also mean that you have circular includes.