dimanche 28 juin 2015

Take sentence from user input with C

I wana take input from user and then print, I think I should also allocate memory could someone show me how to do that properly?

Here is my try:

    int days = 1;
    char * obligation[1500];
    char * dodatno[1500];

    puts("Enter nuber of days till obligation:\n");
    scanf(" %d", &days);
    puts("Enter obligation:\n");
    scanf(" %s", obligation);
    puts("Sati:\n");
    scanf(" %s", dodatno);

printf("%s|%s|%s \n",days,obligation,dodatno);

Alternative for #subList() for String array?

So I modified an API (for command management) to use String[] instead of a list of Strings.

My problem is here:strings.subList(1, strings.length) So I need to change this to a different alternative that would do the same job.

What exactly could I do here?

Why doesn't byte[] b = new byte[8](); work? [duplicate]

I already know that byte[] b = new byte[8]; works fine. But why doesn't byte[] b = new byte[8]; work?

For example, byte b = new byte(); works fine, so an array style also should work but it doesn't. I have no exact idea of this reason. Could someone please explain?

AS3: hitTestObject turning up null for no apparent reason

I'm currently coding a game, and in one part of the code, it's checking to see if the player is close enough to a monster in mArray for its health bar to appear. I use hitTestObject for this with var i incrementing to go through the list. However, I will randomly get this error message:

TypeError: Error #2007: Parameter hitTestObject must be non-null.

After seeing this for the first time, I put the test all under the conditional if (mArray.length > 0) to make sure the Array() was filled before even attempting the test... but it still occurs at random times. I even used trace(i) to see if it happened on a certain part of the list, but it's completely random each debug session! I know the player portion can't be null since it is on the screen at all times during the hitTest, so I'm at a loss for words right now.

The portion of code in question (nothing else is related to this except for the use of mArray[i] in different places):

for (var i:int = 0; i < mArray.length; i++) {
    if (mArray.length > 0) {

        trace(i); //my attempt to see if it occurred at a specific incrementation of i

        if (player.hitTestObject(mArray[i])) {
            mArray[i].healthBar.visible = true;
        } else {
            mArray[i].healthBar.visible = false;
        }

    }
}

Again, it works perfectly for everything all of the time except for random cases it gives me the TypeError. Is this just a hiccup in my code, is there some obvious problem that I'm overlooking, or does the code just like to throwback to 2007?

EDIT: The debug session ALWAYS points to the line with if (player.hitTestObject(mArray[i])).

The left hand side must be a variable (ArrayList and Array)

I'm a little confused between an ArrayList and an Array, I understand what a variable is but I don't understand why I can't convert the ArrayList to an Array and have the same effect.

This piece of code I currently have when using an ArrayList

import java.util.Objects;

import com.google.gson.Gson;
import com.google.gson.JsonObject;


public final class NpcDefinitionLoader extends JsonLoader {

    /**
     * Creates a new {@link NpcDefinitionLoader}.
     */
    public NpcDefinitionLoader() {
        super(DefinitionEditor.fileChooser.getSelectedFile().getAbsolutePath());
    }

    @Override
    public void load(JsonObject reader, Gson builder) {
        int index = reader.get("id").getAsInt();
        String name = Objects.requireNonNull(reader.get("name").getAsString());
        String description = Objects.requireNonNull(reader.get("examine").getAsString());
        int combatLevel = reader.get("combat").getAsInt();
        int size = reader.get("size").getAsInt();
        boolean attackable = reader.get("attackable").getAsBoolean();
        boolean aggressive = reader.get("aggressive").getAsBoolean();
        boolean retreats = reader.get("retreats").getAsBoolean();
        boolean poisonous = reader.get("poisonous").getAsBoolean();
        int respawnTime = reader.get("respawn").getAsInt();
        int maxHit = reader.get("maxHit").getAsInt();
        int hitpoints = reader.get("hitpoints").getAsInt();
        int attackSpeed = reader.get("attackSpeed").getAsInt();
        int attackAnim = reader.get("attackAnim").getAsInt();
        int defenceAnim = reader.get("defenceAnim").getAsInt();
        int deathAnim = reader.get("deathAnim").getAsInt();
        int attackBonus = reader.get("attackBonus").getAsInt();
        int meleeDefence = reader.get("defenceMelee").getAsInt();
        int rangedDefence = reader.get("defenceRange").getAsInt();
        int magicDefence = reader.get("defenceMage").getAsInt();

        NPCDefinitions.definitions.get(index) = new NPCDefinitions(index, name, description, combatLevel, size, attackable, aggressive, retreats,
            poisonous, respawnTime, maxHit, hitpoints, attackSpeed, attackAnim, defenceAnim, deathAnim, attackBonus, meleeDefence,
            rangedDefence, magicDefence);
    }
}

I get an error here

NPCDefinitions.definitions.get(index) = new NPCDefinitions(index, name, description, combatLevel, size, attackable, aggressive, retreats,
        poisonous, respawnTime, maxHit, hitpoints, attackSpeed, attackAnim, defenceAnim, deathAnim, attackBonus, meleeDefence,
        rangedDefence, magicDefence);

Saying the left side must be a variable. When I use an Array like this

NPCDefinitions.definitions[index] = new NPCDefinitions(index, name, description, combatLevel, size, attackable, aggressive, retreats,
        poisonous, respawnTime, maxHit, hitpoints, attackSpeed, attackAnim, defenceAnim, deathAnim, attackBonus, meleeDefence,
        rangedDefence, magicDefence);

Code works perfect.

I'm confused about this, how would I make this work by using an ArrayList?

Parse object containing array elements

I have a Parse object called Recipes and a column called ingredients, which is an array. I want to query my object list and retrieve a recipe based on some ingredients that I select.

If I use the whereKey:containsAllObjectsInArray: message on the query object, I will get recipes with more ingredients. Also, whereKey:containedIn: does not solve my problem. The retrieved objects should have an array of ingredients containing all my selected ingredients or only some of them. It should never have more ingredients than those I've selected.

Any ideas?

Cannot Assign Converted Value To Array

I would like to assign the integer value which is included in a string to an array.

The code:

int s[10][10];
string line="1asd";
char z=line[0];
s[0][0]=z-'0';

Error:

  `Invalid types 'char[int]' for array subscript`