www.digitalmars.com         C & C++   DMDScript  

D - Newbie Q: d-strings, arrays

reply Alkaja <Alkaja_member pathlink.com> writes:
Sorry for 59 lines code. Put many questions in one message
to save everybodys work. My real program is longer, but
this works too.


(1) Using struct gives
pal.d(27): Can only concatenate arrays

(2) Is this really this much work?
Is there easier way?

(3) Seems OK, just ask am I doing this right?

(4) scanf demands fixed length like [80]?

(5) Here [80] gives
pal.d(37): cannot change reference to static array 'a2'

(6) With no ~="" I get same text in every entry! But length
of text is minimum of original and last entered name!
Look like every person has own length of string but same
pointer to string?

Friends tell me D is easy? Am I doing much wrong, or what?
I want to have forename as D string, not C string.


import std.c.stdio;
import std.string;

class Pal {                     //(1) cant use struct!?
char[] forename;
int age;
}
Pal[] pals;

void main()
{
char[80] action;
char[] action2;
int person_id = 0;

bool goOn = true;
while(goOn)
{
printf("New,List,Quit?(n/l/q): ");

scanf("%s",&action);
action2 = std.string.toString(cast(char*)action);   //(2)

switch(action2)
{
case "n":
pals ~= new Pal;                    //(3) Right Way?
person_id = pals.length -1;         //(3) Right Way?

char[80] a;                         //(4)
char[] a2;                          //(5)
int b;

printf("Name Age: ");
scanf("%s %d",&a,&b);

a2 = toString(cast(char*)a);

pals[person_id].forename = a2 ~= ""; //(6)
pals[person_id].age = b;

printf("\tperson_id: %d\n",person_id);

break;
case "l":
for(int i =0;i<pals.length;i++)
{
printf("%.*s\n", pals[i].forename);
printf("%d\n", pals[i].age);
}
break;
case "q":
printf("QUITTING! \n");
goOn = false;
break;
default:;
}
}
}
Jan 12 2004
next sibling parent Ilya Minkov <minkov cs.tum.edu> writes:
Alkaja wrote:
 Sorry for 59 lines code. Put many questions in one message
 to save everybodys work. My real program is longer, but
 this works too.
 
 
 (1) Using struct gives
 pal.d(27): Can only concatenate arrays

struct is OK. See (3), i believe
 (2) Is this really this much work?
 Is there easier way?

Look at phobos docs. It has its much more convenient stream IO.
 (3) Seems OK, just ask am I doing this right?

Outsch. Look, you have an array of structures. Not an aray of pointers, but an array of structures. They need not be allocated, their storage is allocated as a part of an array. Like with basic types like int and so on. You just have to assign fields correct values. It is currently forbidden to do things like array.length++, but it is only for the sake of irritating newbees i guess. Any other way to increase the value of length should work. I would say that this is a nonsense limitation, it has its purpose to forbid people to wite slow programs. I think this is the wrong spot for such a pressure.
 (4) scanf demands fixed length like [80]?

scanf demandes something which is already allocated - dynamically or statically doesn't matter. But i wouldn't use C IO if i were you. Go phobos.
 (5) Here [80] gives
 pal.d(37): cannot change reference to static array 'a2'

Maybe slice or dup? I think it's time to check examples in the manual. Please, it's not that big. Check out the array and slicing semantics.
 (6) With no ~="" I get same text in every entry! But length
 of text is minimum of original and last entered name!
 Look like every person has own length of string but same
 pointer to string?

I get headache. And that's what you also get when you (mis)use too much C library, be it in C or in D. I think the problem is rather on the C part of the library.
 Friends tell me D is easy? Am I doing much wrong, or what?
 I want to have forename as D string, not C string.

The language fairly is, but your use of library sucks. BTW, you may want to see Vathix libararies. He just posted the adress in the group. -eye
Jan 12 2004
prev sibling parent reply "Ben Hinkle" <bhinkle4 juno.com> writes:
I've modified your code to be closer to D standards:

import std.c.stdio;
import std.string;
import std.stream;

struct Pal {
  char[] forename;
  int age;
}
Pal[] pals;

void main()
{
  char[] action;
  int person_id = 0;

  bool goOn = true;
  while(goOn)
  {
    printf("New,List,Quit?(n/l/q): ");

    std.stream.stdin.scanf("%.*s",&action);
    switch(action)
    {
    case "n":
      person_id = pals.length;
      pals.length = pals.length+1;

      char[] a;
      int b;

      printf("Name Age: ");
      std.stream.stdin.scanf("%.*s %d",&a,&b);

      pals[person_id].forename = a;
      pals[person_id].age = b;

      printf("\tperson_id: %d\n",person_id);

      break;
    case "l":
      for(int i =0;i<pals.length;i++)
      {
        printf("%.*s\n", pals[i].forename);
        printf("%d\n", pals[i].age);
      }
      break;
    case "q":
      printf("QUITTING! \n");
      goOn = false;
      break;
    default:;
    }
  }
}
Jan 12 2004
next sibling parent reply Ant <duitoolkit yahoo.ca> writes:
On Mon, 12 Jan 2004 17:50:57 -0500, Ben Hinkle wrote:

as a side note I think it's wrong to hide
the loop exit test.
that's why I don't like or use continue or break.

Why not put the test here it belongs!?

Ant
Jan 12 2004
parent reply "Ben Hinkle" <bhinkle4 juno.com> writes:
I don't follow. Can you post an example?

"Ant" <duitoolkit yahoo.ca> wrote in message
news:pan.2004.01.12.22.54.02.220973 yahoo.ca...
 On Mon, 12 Jan 2004 17:50:57 -0500, Ben Hinkle wrote:

 as a side note I think it's wrong to hide
 the loop exit test.
 that's why I don't like or use continue or break.

 Why not put the test here it belongs!?

 Ant

Jan 12 2004
parent reply Ant <duitoolkit yahoo.ca> writes:
On Mon, 12 Jan 2004 18:47:27 -0500, Ben Hinkle wrote:

 I don't follow. Can you post an example?

scanf(scanf("%.*s",&action); // init while (action != "q") // test { ... // exec scanf(scanf("%.*s",&action); // modify } // loop ITEML the first thing you learn at school. (probably now they call it the "iteraction design pattern" or something like that ;) of course for this case you can also use a do{...}while() (I guess a good name would be the "reversed iteraction design pattern" :D) (I seldomly do) using a bool to old the value of the test just confuses things. Simple cases should be kept simple. Ant
Jan 12 2004
parent "Ben Hinkle" <bhinkle4 juno.com> writes:
ah, I see what you mean now. At least there wasn't a goto! ;-)

"Ant" <duitoolkit yahoo.ca> wrote in message
news:pan.2004.01.13.01.03.57.553976 yahoo.ca...
 On Mon, 12 Jan 2004 18:47:27 -0500, Ben Hinkle wrote:

 I don't follow. Can you post an example?

scanf(scanf("%.*s",&action); // init while (action != "q") // test { ... // exec scanf(scanf("%.*s",&action); // modify } // loop ITEML the first thing you learn at school. (probably now they call it the "iteraction design pattern" or something like that ;) of course for this case you can also use a do{...}while() (I guess a good name would be the "reversed iteraction design pattern" :D) (I seldomly do) using a bool to old the value of the test just confuses things. Simple cases should be kept simple. Ant

Jan 12 2004
prev sibling parent Alkaja <Alkaja_member pathlink.com> writes:
In article <btv8cj$2v9k$1 digitaldaemon.com>, Ben Hinkle says...
I've modified your code to be closer to D standards:

THANK YOU! Very helpful! Much fewer changes than I guessed. Now D seems much friendlier!
Jan 13 2004