Capnproto D language Print all data in a file - d

I downloaded the capnproto for dlanguage and started tinkering the sample-addressbook.. My problem is, everytime I add a person to the addressbook, it still prints only one of the data in the file, instead of the whole file..
void writeAddressBook()
{
MessageBuilder message = new MessageBuilder();
auto addressbook = message.initRoot!AddressBook;
auto people = addressbook.initPeople(1);
auto person = people[0];
person.id = 123;
person.setName("Tomas");
person.email = "tomas#example.com";
auto personPhones = person.initPhones(1);
personPhones[0].number = "0917xxxxxxx";
personPhones[0].type = Person.PhoneNumber.Type.mobile;
person.employment.school = "School of Thoughts";
File f = File("1.dat","a+b");
SerializePacked.writeToUnbuffered(new FileDescriptor(f), message);
}
If I call the writeAddressBook() 4 times, then I have 4 People in the addressbook with the same name, The Problem is, everytime I print all of the data, it only prints the first one..
void printAddressBook()
{
File f = File("1.dat","r");
auto message = SerializePacked.readFromUnbuffered(new FileDescriptor(f));
auto addressbook = message.getRoot!AddressBook;
foreach(person; addressbook.people)
{
writefln("%s: %s", person.name, person.email);
foreach(phone; person.phones)
{
string typeName = "UNKNOWN";
switch(phone.type) with(Person.PhoneNumber.Type)
{
case mobile:
typeName = "mobile";
break;
case home:
typeName = "home";
break;
case work:
typeName = "work";
break;
default:
break;
}
writefln(" %s phone: %s", typeName, phone.number);
}
auto employment = person.employment;
switch(employment.which()) with(Person.Employment.Which)
{
case unemployed:
writefln(" unemployed");
break;
case employer:
writefln(" employer: %s", employment.employer);
break;
case school:
writefln(" student at: %s", employment.school);
break;
case selfEmployed:
writefln(" self-employed");
break;
default:
break;
}
}
}

Related

I need to group multiple functions into one function

I need to group multiple if functions into one big function with a custom name.
if (NPC == NPC1)
{
Attack = NPCAttack1;
}
if (NPC == NPC2)
{
Attack = NPCAttack2;
}
if (NPC == NPC3)
{
Attack = NPCAttack3;
}
if (NPC == NPC4)
{
Attack = NPCAttack4;
}
if (NPC == NPC5)
{
Attack = NPCAttack5;
}
if (NPC == NPC6)
{
Attack = NPCAttack6;
}
if (NPC == NPC7)
{
Attack = NPCAttack7;
}
if (NPC == NPC8)
{
Attack = NPCAttack8;
}
if (NPC == NPC9)
{
Attack = NPCAttack9;
}
if (NPC == NPC10)
{
Attack = NPCAttack10;
}
I want all of that to be inside a function called AttackFunction. How do I do that?
First of all, let's replace all of those if statements with a single switch statement:
switch (NPC) {
case NPC1:
Attack = NPCAttack1;
break;
case NPC2:
Attack = NPCAttack2;
break;
case NPC3:
Attack = NPCAttack3;
break;
case NPC4:
Attack = NPCAttack4;
break;
case NPC5:
Attack = NPCAttack5;
break;
case NPC6:
Attack = NPCAttack6;
break;
case NPC7:
Attack = NPCAttack7;
break;
case NPC8:
Attack = NPCAttack8;
break;
case NPC9:
Attack = NPCAttack9;
break;
case NPC10:
Attack = NPCAttack10;
break;
}
Now we can inject this code into a function, which receive the NPC, and return the desired type of attack.
<attack_type> get_attack_type_from_npc(<npc_type> NPC) {
switch (NPC) {
case NPC1:
return NPCAttack1;
case NPC2:
return NPCAttack2;
case NPC3:
return NPCAttack3;
case NPC4:
return NPCAttack4;
case NPC5:
return NPCAttack5;
case NPC6:
return NPCAttack6;
case NPC7:
return NPCAttack7;
case NPC8:
return NPCAttack8;
case NPC9:
return NPCAttack9;
case NPC10:
return NPCAttack10;
}
throw std::runtime_error("No attack type found");
}
in your main:
int main() {
// ... Declare Attack & NPC ... Set NPC ...
Attack = get_attack_type_from_npc(NPC);
}

'Pointer to member' error C++

I need some help with this error, I don't understand what has to be done to get rid of it. I am trying to build this code - the last exercise, Graduation. Here is my code:
// Bunnies.cpp : Defines the entry point for the console application.
// Male = 0; Female = 1
#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include<stdio.h>
#include<time.h>
using namespace std;
int num;
class bunny {
int val;
int gender;
int colour;
int age;
int type;
bool radioBun;
string name;
public:
void create() {
val = num;
age = 1;
gender = rand() % 2;
switch (rand() % 100) {
case 1: radioBun = true;
break;
case 2: radioBun = true;
break;
default: radioBun = false;
break;
}
if (gender = 0) {
switch (rand() % 20) {
case 0: name = "Bob";
break;
case 1: name = "Alexis";
break;
case 2: name = "William";
break;
case 3: name = "Cleo";
break;
case 4: name = "Mark";
break;
case 5: name = "Jarod";
break;
case 6: name = "Billie";
break;
case 7: name = "Nathan";
break;
case 8: name = "Richard";
break;
case 9: name = "Thomas";
break;
case 10: name = "Rudolf";
break;
case 11: name = "Troy";
break;
case 12: name = "Wesley";
break;
case 13: name = "Jacob";
break;
case 14: name = "Cody";
break;
case 15: name = "Gavin";
break;
case 16: name = "Norris";
break;
case 17: name = "Matt";
break;
case 18: name = "Colton";
break;
case 19: name = "Daniel";
break;
}
}
else {
switch (rand() % 20) {
case 0: name = "Cecila";
break;
case 1: name = "Scarlet";
break;
case 2: name = "Abby";
break;
case 3: name = "Sandra";
break;
case 4: name = "Melissa";
break;
case 5: name = "Lizabeth";
break;
case 6: name = "Susie";
break;
case 7: name = "Cherly";
break;
case 8: name = "Kaitlin";
break;
case 9: name = "Debbie";
break;
case 10: name = "Evalyn";
break;
case 11: name = "Amalia";
break;
case 12: name = "Mendy";
break;
case 13: name = "Nora";
break;
case 14: name = "Brigitte";
break;
case 15: name = "Ebony";
break;
case 16: name = "Beatrice";
break;
case 17: name = "Tiffany";
break;
case 18: name = "Ying";
break;
case 19: name = "Kesha";
break;
}
}
}
void printCreate() {
cout << "Bunny ";
cout << val;
cout << " was zapped into existence!" << endl;
}
};
int main()
{
srand(time(0));
for (int x = 0; x == 10; x++) {
num = x;
bunny num;
num.create();
num.printCreate;
}
system("pause");
return 0;
}
I am working out a way to create 10 bunnies at the start of the code, by creating a variable num. The for loop runs 10 times, everytime increasing num by +1. Each time creating a new instance of the class bunnies and assigning the value of num to the name of that instance.
The void create() is not giving me any problems, its when I try to do void printCreate() that I get this error. It just comes up with "Severity Code Description Project File Line Suppression State
Error C3867 'bunny::printCreate': non-standard syntax; use '&' to create a pointer to member Bunnies c:\users\bob\source\repos\bunnies\bunnies\bunnies.cpp 145
". I have looked this up, but I can't understand how to fix this error. Could someone please tell me how to fix this in simple terms?
Replace num.printCreate; in your loop by num.printCreate();.

String was not recognized as a valid DateTime in Android

I am developing an Android client for Microsoft Dynamics NAV. I am using ksoap2 for making communication with NAV webservices. I used MarshalDate class to serialize date. When I pass Date to webservice I am getting an error like "String was not recognized as a valid DateTime."
MarshalDate.java
public class MarshalDate implements Marshal {
public static Class DATE_CLASS = new Date().getClass();
public Object readInstance(XmlPullParser parser, String namespace, String name, PropertyInfo expected) throws IOException, XmlPullParserException {
return IsoDate.stringToDate(parser.nextText(), IsoDate.DATE_TIME);
}
public void register(SoapSerializationEnvelope cm) {
cm.addMapping(cm.xsd, "dateTime", Date.class, this); }
public void writeInstance(XmlSerializer writer, Object obj) throws IOException {
writer.text(IsoDate.dateToString((Date) obj, IsoDate.DATE_TIME)); } }
My object for salesordercard,
SalesOrderCard.java
public class SalesOrderCard implements KvmSerializable
{
String Key;
String No;
String Sell_to_Customer_No;
Sales_Order_Line_List Salesorderlinelist;
String ShopNo;
Date OrderDate;
double Received_Amount;
boolean Cheque;
public SalesOrderCard(){}
public SalesOrderCard(String key,String no,String selltocustno,Sales_Order_Line_List salesorderline,String shopno,Date orderdate ,double received_Amount,boolean cheque) {
Key = key;
No = no;
Sell_to_Customer_No = selltocustno;
Salesorderlinelist=salesorderline;
ShopNo=shopno;
OrderDate=orderdate;
received_Amount=Received_Amount;
Cheque=cheque;
}
public Object getProperty(int arg0) {
switch(arg0)
{
case 0:
return Key;
case 1:
return No;
case 2:
return Sell_to_Customer_No;
case 3:
return Salesorderlinelist;
case 4:
return ShopNo;
case 5:
return Received_Amount;
case 6:
return Cheque;
case 7:
return OrderDate;
}
return null;
}
public int getPropertyCount() {
return 8;
}
public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
switch(index)
{
case 0:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "Key";
break;
case 1:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "No";
break;
case 2:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "Sell_to_Customer_No";
break;
case 3:
info.type = Sales_Order_Line_List.class;
info.name = "SalesLines";
break;
case 4:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "Shop_No";
break;
case 5:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "Received_Amount";
break;
case 6:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "Cheque";
break;
case 7:
info.type = MarshalDate2.DATE_CLASS;
info.name = "Order_Date";
break;
default:break;
}
}
public void setProperty(int index, Object value) {
switch(index)
{
case 0:
Key = value.toString();
break;
case 1:
No = value.toString();
break;
case 2:
Sell_to_Customer_No = value.toString();
break;
case 3:
Salesorderlinelist = (Sales_Order_Line_List)value;
break;
case 4:
ShopNo = value.toString();
break;
case 5:
Received_Amount = Double.parseDouble(value.toString());
break;
case 6:
Cheque = Boolean.parseBoolean(value.toString());
break;
case 7:
OrderDate=(Date)value;
break;
default:
break;
}
}
}
And I am passing date like,
String DateString="22-05-2014 19:32:52";
SimpleDateFormat format=new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
d=(Date)format.parse(DateString);
SalesOrderCard cr=new SalesOrderCard();
cr.OrderDate=d;
PropertyInfo custProp = new PropertyInfo();
custProp.setName("SalesOrderCard");
custProp.setValue(cr);
custProp.setType(SalesOrderCard.class);
request.addProperty(custProp);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
MarshalDouble md = new MarshalDouble();
md.register(envelope);
MarshalDate mdate=new MarshalDate();
mdate.register(envelope);
Assuming d is a Date this code looks correct
String DateString="22-05-2014 19:32:52";
SimpleDateFormat format=new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
d=(Date)format.parse(DateString);
I suspect this line may be the problem
writer.text(IsoDate.dateToString((Date) obj, IsoDate.DATE_TIME));
If you know the format writer.text expects then I would try using SimpleDateFormat to create the string for you instead:
String dateString = format.format((Date)obj);

an error about heap corrupting

I wrote a C++ program, actually it's a game.
I've received this error:
Windows has triggered a breakpoint in bla bla...
Can someone help me?
That's code, but error occur on line AAA:
void r_motions(char **map,int size)
{
int parameter_i,parameter_j,player_i,player_j;
int *r_location_i = new int[1],*r_location_j = new int[1];
player_finder(map,size,player_i,player_j);
int r_num = robots_finder(map,size,r_location_i,r_location_j);
for(int i=1;i<=r_num;i++)
{
parameter_i =0;
parameter_j =0;
if(r_location_i[i]>player_i) parameter_i = -1;
if(r_location_i[i]<player_i) parameter_i = 1;
if(r_location_j[i]>player_j) parameter_j = -1;
if(r_location_j[i]<player_j) parameter_j = 1;
map[r_location_i[i]][r_location_j[i]] = '.';
r_location_i[i] = r_location_i[i]+parameter_i;
r_location_j[i] = r_location_j[i]+parameter_j;
}
for(int i=1;i<=r_num;i++)
{
switch (map[r_location_i[i]][r_location_j[i]])
{
case '.':
map[r_location_i[i]][r_location_j[i]] = '+';
break;
case '#':
map[r_location_i[i]][r_location_j[i]] = '+';
print_map(map,size);
cout << "Robots win ." << endl;
sleep(1000);
exit(1);
break;
case '+':
map[r_location_i[i]][r_location_j[i]] = '*';
break;
case '*':
map[r_location_i[i]][r_location_j[i]] = '*';
break;
default: cout << "what r u doin' ??";
break;
}
}
}
All right, just for starters, look at this:
int *r_location_i = new int[1], ...;
...
for(int i=1;i<=r_num;i++)
{
parameter_i =0;
...
if(r_location_i[i]>player_i) parameter_i = -1; // reading outside the array
...
r_location_i[i] = r_location_i[i]+parameter_i; // writing outside the array
...
}
Go back and study arrays. Do not touch another pointer until you know exactly what's wrong with the code above.

c++ Recursive Tree building with pointers and a state machine

I have a simple state machine (entered below). My major problem is that I am trying to make a recursive call to the function that is my state machine. What I do upon entry of the function is create a new node for my tree and then push that through. When I do a recursive call, I create a new node over and over. This can work, but when adding children to a parent I'm a little confused. Can someone help look over this and help me take my tree node (parent I'm assuming) and attach a child to it?
TreeNodeClass* ProcessTree(TokenT token, vector <list <stateAssoc> >& vecTree, int depth)
{
int state = 1; //Assume this is a new record.
bool noState = false;
bool update = true;
int dex = 0;
string root, value, data, tag;
TreeNodeClass* treeNode;
treeNode = new TreeNodeClass; //Assume a new node per state machine visit.
//Need 11 to break out of loop as well.
while(state != 10)
{
switch(state)
{
case 1: dex = 1;
break;
case 2: dex = 6;
root = yylval;
break;
case 3: dex = 7;
break;
case 4: dex = 3;
value = yylval;
treeNode->CreateAttrib(root, value);
break;
case 5: dex = 2;
break;
case 6: dex = 4;
data = yylval;
break;
case 7: //Really Don't do anything. Set the tag creation at 8...
dex = 8;
tag = yylval;
if(data != "" and data != "authors")
treeNode->CreateTag(data, tag);
break;
case 8: {
//New TreeNode already grabbed.
//TreeNodeClass* childNode = new TreeNodeClass;
childNode = ProcessTree(token, vecTree, depth+1);
childNode->SetHeight(depth);
treeNode->AddChildren(childNode);
}
token = TokenT(yylex()); //Get a new token to process.
dex = 5;
break;
case 9: dex = 9;
update = false;
if(yylval != treeNode->ReturnTag())
{
state = 11;
}
break;
case 10: update = false;
treeNode->SetHeight(1);
break;
default: cout << "Error " << endl;
cout << state << endl;
cin.get();
break;
}
if(!noState)
state = FindMatch(vecTree[dex], token);
if(update)
token = TokenT(yylex());
else
update = true;
}
return treeNode;
}
You may assume that the dex is simply an index to an array of lists that will return a correct state or 11 (error). Also you may assume that this funciton has atleast been called once on an input file and has started parsing. Thank you for your help.
Looking at your code, you have
int state = 1;
//Code
while(state != 10) {
switch(state) {
case:1 dex = 1; break; //Only case you run
//more cases
case 8: { //never enter here
//New TreeNode already grabbed.
//TreeNodeClass* childNode = new TreeNodeClass;
childNode = ProcessTree(token, vecTree, depth+1);
childNode->SetHeight(depth);
treeNode->AddChildren(childNode); //should work if assuming function is correct
}
//stuff
break;
default: break;//blah
}
}
//blah
return treeNode;
I see no other reason than the fact state is always equal to 1 that your code at case 8 would fail. This is assuming treeNodeClass::AddChildren(TreeNodeClass*) has been properly implemented. WIthout that code, I can assume it's not your problem. Is there some method in which state wouldn't be 1 in the switch?