samedi 27 juin 2015

how to get an integer from a char array

I have a char array that has '4''5'. But I want to convert those characters into actual integers so I subtract '0' from each index of the char array and store it into the same array. If I want to set an int called value to the 45 (the stuff inside the char array) How would I do it?

Does storing objects in a std::vector increase the lifetime of the object ?

for (int i = 0; i < 10; i++)
    { 
        nueron temp; // my Class
        _inputNuerons.push_back(temp); // _inputNuerons is a std::vector 
    }

From what I know , C++ deallocates the temp object when it reaches the } bracket of the for loop. But does storing the object in a std::vector make the object usable later on in the program ? Now when I try to access the "temp" nueron outside the for loop , the program crashes.

How can I store my objects in a std::vector such that it is usable later on ?

Programming without semicolon in C or Java or C++ [on hold]

I was trying to solve easy arithmetic problems with expression...

<expre>::=<num><opt><num>
<num>::=0|1|2|...|99
<opt>::=+|-|*

But the constraint is I can not use semicolons in my program.

I was able to print strings without semicolons but found no luck with taking input from user or printing calculated value.

#include <stdio.h>
main()
{
    if((printf("cspractical")))
    {
        if(int n=0 && scanf("%d",&n) && printf("%d",n))
        {}
    }
}

This is what I was trying. It is printing cspractical but nothing else.

Any help would be appreciated.

C++ CreateFile function locks the file with UAC

I'm working on a DLL file that extracts a resource file (.exe) from it to TEMP folder and executes it. The problem is it fails to execute the file unless the application that is using the DLL file is elevated (has administrator privileges). When I open TEMP folder from file explorer and double-click the extracted file, I get a UAC prompt. And when I click on "change when this notification appears" it is set to default (when a program tries to make changes to your computer). And I really don't understand why I get this notification because it's just a random file that runs without privileges on my PC unless it's extracted from the DLL. I use Windows 8 and Visual Studio 2013. Here's a part of the code of the DLL file:

    HANDLE hFile = CreateFile(exePath, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hFile == INVALID_HANDLE_VALUE) continue;
    WriteFile(hFile, pExeResource, SizeofResource(hinstDLL, hrsrc), NULL, NULL); 
    CloseHandle(hFile);
    if (!CreateProcess(exePath, NULL, NULL, NULL, FALSE, REALTIME_PRIORITY_CLASS, NULL, tmpPath, &info, &pi))
        cout << "FAIL !\n";
    else cout << "SUCCESS !\n";

my c++ random number is not random

I am running this

#include <boost/mpi.hpp>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <time.h>
namespace mpi = boost::mpi;

int main()
{
    mpi::environment env;
    mpi::communicator world;



    srand (time(NULL));
    std::srand(time(0) + world.rank());
    int my_number = std::rand();
    if (world.rank() == 0) {
        std::vector<int> all_numbers;
        gather(world, my_number, all_numbers, 0);
        for (int proc = 0; proc < world.size(); ++proc)
            std::cout << "Process #" << proc << " thought of "
            << all_numbers[proc] << std::endl;
    } else {
        gather(world, my_number, 0);
    }

    return 0;
}

to distributively generate random number, however, it gives me the number around the same magnitude everytime....

dhcp-18-189-66-216:ising2 myname$ make
mpic++ -I/usr/local/include/boost -L/usr/local/lib -lboost_mpi -lboost_serialization main.cpp -o main
mpirun -n 4 main
Process #0 thought of 238772362
Process #1 thought of 238789169
Process #2 thought of 238805976
Process #3 thought of 238822783
dhcp-18-189-66-216:ising2 myname$ make
mpic++ -I/usr/local/include/boost -L/usr/local/lib -lboost_mpi -lboost_serialization main.cpp -o main
mpirun -n 4 main
Process #0 thought of 238805976
Process #1 thought of 238822783
Process #2 thought of 238839590
Process #3 thought of 238856397
dhcp-18-189-66-216:ising2 myname$ make
mpic++ -I/usr/local/include/boost -L/usr/local/lib -lboost_mpi -lboost_serialization main.cpp -o main
mpirun -n 4 main
Process #0 thought of 238856397
Process #1 thought of 238873204
Process #2 thought of 238890011
Process #3 thought of 238906818
dhcp-18-189-66-216:ising2 myname$ 

In the website, http://ift.tt/1LuAXhT , others said they get:

Process #0 thought of 332199874
Process #1 thought of 20145617
Process #2 thought of 1862420122
Process #3 thought of 480422940
Process #4 thought of 1253380219
Process #5 thought of 949458815
Process #6 thought of 650073868

I am very confused.... Any help? Thank you.

cocos2dx - Alignment with LabelBMFont

I am trying and struggling to align the BMFontLabel with a sprite side by side. I am using code below to show score of the game with a coin on the left, and score on the right side..

Sprite *coin=Sprite::create("coinimg.png");
Label *scoreLbl=Label::createWithBMFont(GameConfig::scoreBigFont, strScore);
scoreLbl->setScale(.5);

MenuItemSprite *itemCoin=MenuItemSprite::create(coin);
MenuItemLabel *itemScore=MenuItemLabel::create(scoreLbl);

Menu *mainMenu=Menu::create(itemCoin,itemScore, NULL);

mainMenu->alignItemsHorizontallyWithPadding(0.0);

mainMenu->setPosition(Vec2(winsize.width*.1,winsize.height*.8));

The problem is I am getting different alignment with different values I input like below in image

![enter image description here][1]

"invalid pure specifier" when I meant no pure specifier?

Consider the following snippet:

class UltraProbe {
public:
  ConnectProbe *CP() {
    return probes.CP;  // if type == UP_CONNECT
  }
private:
  probespec mypspec; /* Filled in by the appropriate set* function */
  union {
    IPExtraProbeData IP;
    ConnectProbe *CP;
    //    ArpProbe *AP;
  } probes;

};

bool do_one_select_round(UltraScanInfo *USI, struct timeval *stime) {

  UltraProbe *probe = NULL;
  int errno = (probe->CP()->connect_result);

}

Why am I getting the error below?

scan_engine_connect.cc:592:22: error: invalid pure specifier (only ‘= 0’ is allowed) before ‘probe’
         int errno = (probe->CP()->connect_result);
                      ^