www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - User imput string int and float[DOUBT]

reply Jean Cesar <jeanzonta777 yahoo.com.br> writes:
How do I make a class person where I use set and get methods to 
imput the user type:

Import std.stdio;

class person
{
   private:
   string name, address;
   int age;
   float height;

public:
   void setNome()
   {
     write("Enter Your Name:");
     // the problem is here how am I going to read the imput of a 
string typed by the user?
   }

void setIty()
{
    write("Enter Your Age:");
   // Another problem here also to read integer values like I 
would?
}

void setHeight()
{
   write("Enter Your Height:");
   // Another problem here also to read floats or double values 
like I would?
}

float getHeight()
{
   return height;
}

int getIty()
{
   return age;
}

string getNome()
{
   return name;
}

}

void main ()
{
   person p = new person();

   p.setName();
   p.setIdade();
   p.setHeight();

   p.getName();
   p.getIdade();
   p.getHeight();
}
Feb 15 2017
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 02/15/2017 03:20 PM, Jean Cesar wrote:
 How do I make a class person where I use set and get methods to imput
 the user type:
I have some information here: http://ddili.org/ders/d.en/input.html You should also know how to read strings: http://ddili.org/ders/d.en/strings.html And this section about refactoring has the concept of a readInt() function template: http://ddili.org/ders/d.en/functions.html#ix_functions.refactor Combining all three: import std.stdio; import std.traits; auto read(T)(ref T t, string message) if (!isSomeString!T) { writef("%s: ", message); readf(" %s", &t); return t; } auto read(S)(ref S s, string message) if (isSomeString!S) { import std.string : strip; writef("%s: ", message); s = readln().strip(); return s; } class person { private: string name, address; int age; float height; public: void setNome() { read(name, "Enter Your Name"); } void setIty() { read(age, "Enter Your Age"); } void setHeight() { read(height, "Enter Your Height"); } float getHeight() { return height; } int getIty() { return age; } string getNome() { return name; } } void main () { person p = new person(); p.setNome(); p.setIty(); p.setHeight(); writeln(p.getNome()); writeln(p.getIty()); writeln(p.getHeight()); } Unrelated, a bunch of get/set methods is commonly seen as inferior to a design where another piece of code does the reading and makes the object after the fact: person readPerson(File input) { // ... parse the input ... // Potentially, use the constructor: auto p = new person(name, age, /* ... */); return p; } One reason is the fact that the person may be seen as incomplete and unusable unless all fields are set. Again, it's beside the point... :) Ali
Feb 15 2017
parent reply Jean Cesar <jeanzonta777 yahoo.com.br> writes:
On Wednesday, 15 February 2017 at 23:40:41 UTC, Ali Çehreli wrote:
 On 02/15/2017 03:20 PM, Jean Cesar wrote:
 How do I make a class person where I use set and get methods 
 to imput
 the user type:
I have some information here: http://ddili.org/ders/d.en/input.html You should also know how to read strings: http://ddili.org/ders/d.en/strings.html And this section about refactoring has the concept of a readInt() function template: http://ddili.org/ders/d.en/functions.html#ix_functions.refactor Combining all three: import std.stdio; import std.traits; auto read(T)(ref T t, string message) if (!isSomeString!T) { writef("%s: ", message); readf(" %s", &t); return t; } auto read(S)(ref S s, string message) if (isSomeString!S) { import std.string : strip; writef("%s: ", message); s = readln().strip(); return s; } class person { private: string name, address; int age; float height; public: void setNome() { read(name, "Enter Your Name"); } void setIty() { read(age, "Enter Your Age"); } void setHeight() { read(height, "Enter Your Height"); } float getHeight() { return height; } int getIty() { return age; } string getNome() { return name; } } void main () { person p = new person(); p.setNome(); p.setIty(); p.setHeight(); writeln(p.getNome()); writeln(p.getIty()); writeln(p.getHeight()); } Unrelated, a bunch of get/set methods is commonly seen as inferior to a design where another piece of code does the reading and makes the object after the fact: person readPerson(File input) { // ... parse the input ... // Potentially, use the constructor: auto p = new person(name, age, /* ... */); return p; } One reason is the fact that the person may be seen as incomplete and unusable unless all fields are set. Again, it's beside the point... :) Ali
So I'm a beginner in this language and have very little time I started I'm interested in apprehending concepts of object orientation polymorphism inheritance, multiple inheritance as in c ++, but I did not understand how to use constructor in it Because I simply did. Class person { person(){} ~ Person () {} } And error ...
Feb 15 2017
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 02/15/2017 05:49 PM, Jean Cesar wrote:

 So I'm a beginner in this language and have very little time I started
 I'm interested in apprehending concepts of object orientation
 polymorphism inheritance, multiple inheritance as in c ++
D is similar to C++ but also very different.
 but I did not
 understand how to use constructor in it
 Because I simply did.

 Class person
 {
    person(){}
    ~ Person () {}
 }

 And error ...
In D, constructor is always called this(): class Person { this(){} ~this() {} } void main() { auto p = new Person(); } Ali
Feb 15 2017
parent reply Jean Cesar <jeanzonta777 yahoo.com.br> writes:
On Thursday, 16 February 2017 at 02:17:49 UTC, Ali Çehreli wrote:
 On 02/15/2017 05:49 PM, Jean Cesar wrote:

 So I'm a beginner in this language and have very little time
I started
 I'm interested in apprehending concepts of object orientation
 polymorphism inheritance, multiple inheritance as in c ++
D is similar to C++ but also very different.
 but I did not
 understand how to use constructor in it
 Because I simply did.

 Class person
 {
    person(){}
    ~ Person () {}
 }

 And error ...
In D, constructor is always called this(): class Person { this(){} ~this() {} } void main() { auto p = new Person(); } Ali
So I used get methods and sets only as initial pattern to netender the functioning of the language in relation to some concepts of the same I intend to learn it not because it is a new language, but I want to understand how to leave a very small code with the largest number of Possible functionality type I still do not know very well or use constructors in C ++ but I have very high potential in a code with multiple inheritance, I think of compilers in the case of the code that favors me in reading so I would do something like: void main () { minhaclasse c = new minhaclasse(string text); minhaclasse d = new minhaclasse(int number); write("Enter your name: ") c.set(); write("Enter your age: ") d.set(); /* the set method would already fetch user i imput by mistake for the information automatically */ Writeln ( "\n\tString:", c.get (), "\n\tInt:", d.get () ); } Or something like: void main () { string txt; Int num; write("Enter your name: ") minhaclasse(text).set(); write("Enter your age: ") minhaclasse(num).set(); writeln ( "\n\tString:", minhaclasse(text).print() ;, "\n\tInt:", minhaclasse(num).print(); ); } I think of object orientation this way to avoid getting rewritten many things so I would only define what the set or get would return by initializing the constructor only but I have no idea how to do that .. My goal in learning to use languages like Java, C ++, D is with the intention of learning the best way to reuse code and orienation to objects and also development cross-platform codes that will run in standard ansi for, Unix, Linux, Windows, android etc. ..
Feb 16 2017
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 02/16/2017 02:05 PM, Jean Cesar wrote:

 So I used get methods and sets only as initial pattern to netender the
 functioning of the language in relation to some concepts of the same
Makes sense...
 how to leave a very small code with the largest number of
 Possible functionality type
I think D is very suitable for that.
 I still do not know very well or use
 constructors in C ++
Understandable: Many coding guidelines eschew doing non-trivial work in constructors. They require a member function like obj.initialize(/* ... */) to be called in order to get a functioning object.
 but I have very high potential in a code with
 multiple inheritance
That's going to be a problem because D does not allow multiple inheritance.
 I think of compilers in the case of the code that
 favors me in reading so I would do something like:

 void main ()
 {
    minhaclasse c = new minhaclasse(string text);
    minhaclasse d = new minhaclasse(int number);

  write("Enter your name: ")
  c.set();
So, your minhaclasse is basically ValorLegível (ReadableValue), which would not scale because likely it's also writable and movable, etc. And that explains why you're looking for multiple inheritance. :) // NOT valid D (and no, I don't speak Portuguese) class MinhaValor : ValorLegível, ValorEscrita, ValorMóvel /*, ... */ { // ... }
 void main ()
 {
   string txt;
    Int num;
  write("Enter your name: ")
  minhaclasse(text).set();

  write("Enter your age: ")
  minhaclasse(num).set();

   writeln
    (
     "\n\tString:", minhaclasse(text).print() ;,
     "\n\tInt:", minhaclasse(num).print();
    );
 }

 I think of object orientation this way to avoid getting rewritten many
 things so I would only define what the set or get would return by
 initializing the constructor only but I have no idea how to do that ..
You make it sound as if OOP is for code reuse or for reducing code repetition. I think regular functions provide that already. Unless polymorphism is really beneficial, functional style is preferable. Additionally, D has this very useful universal function call syntax (UFCS), which makes your use case easy to implement, and which my earlier code could have benefited from as well. import std.stdio; import std.traits; auto read(T)(ref T t, string message) if (!isSomeString!T) { writef("%s: ", message); readf(" %s", &t); return t; } auto read(S)(ref S s, string message) if (isSomeString!S) { import std.string : strip; writef("%s: ", message); s = readln().strip(); return s; } class person { private: string name, address; int age; float height; public: static person fromConsole() { auto p = new person(); /* UFCS in action: Note how these are not written as * read(p.name, /* ... */) */ p.name.read("Enter Your Name"); p.age.read("Enter Your Age"); p.height.read("Enter Your Height"); return p; } float getHeight() { return height; } int getIty() { return age; } string getNome() { return name; } } void main () { person p = person.fromConsole(); writeln(p.getNome()); writeln(p.getIty()); writeln(p.getHeight()); }
 My goal in learning to use languages like Java, C ++, D is with the
 intention of learning the best way to reuse code and orienation to
 objects and also development cross-platform codes that will run in
 standard ansi for, Unix, Linux, Windows, android etc. ..
Ali
Feb 16 2017
parent Jean Cesar <jeanzonta777 yahoo.com.br> writes:
On Thursday, 16 February 2017 at 22:44:58 UTC, Ali Çehreli wrote:
 On 02/16/2017 02:05 PM, Jean Cesar wrote:

 So I used get methods and sets only as initial pattern to
netender the
 functioning of the language in relation to some concepts of
the same Makes sense...
 how to leave a very small code with the largest number of
 Possible functionality type
I think D is very suitable for that.
 I still do not know very well or use
 constructors in C ++
Understandable: Many coding guidelines eschew doing non-trivial work in constructors. They require a member function like obj.initialize(/* ... */) to be called in order to get a functioning object.
 but I have very high potential in a code with
 multiple inheritance
That's going to be a problem because D does not allow multiple inheritance.
 I think of compilers in the case of the code that
 favors me in reading so I would do something like:

 void main ()
 {
    minhaclasse c = new minhaclasse(string text);
    minhaclasse d = new minhaclasse(int number);

  write("Enter your name: ")
  c.set();
So, your minhaclasse is basically ValorLegível (ReadableValue), which would not scale because likely it's also writable and movable, etc. And that explains why you're looking for multiple inheritance. :) // NOT valid D (and no, I don't speak Portuguese) class MinhaValor : ValorLegível, ValorEscrita, ValorMóvel /*, ... */ { // ... }
 void main ()
 {
   string txt;
    Int num;
  write("Enter your name: ")
  minhaclasse(text).set();

  write("Enter your age: ")
  minhaclasse(num).set();

   writeln
    (
     "\n\tString:", minhaclasse(text).print() ;,
     "\n\tInt:", minhaclasse(num).print();
    );
 }

 I think of object orientation this way to avoid getting
rewritten many
 things so I would only define what the set or get would
return by
 initializing the constructor only but I have no idea how to
do that .. You make it sound as if OOP is for code reuse or for reducing code repetition. I think regular functions provide that already. Unless polymorphism is really beneficial, functional style is preferable. Additionally, D has this very useful universal function call syntax (UFCS), which makes your use case easy to implement, and which my earlier code could have benefited from as well. import std.stdio; import std.traits; auto read(T)(ref T t, string message) if (!isSomeString!T) { writef("%s: ", message); readf(" %s", &t); return t; } auto read(S)(ref S s, string message) if (isSomeString!S) { import std.string : strip; writef("%s: ", message); s = readln().strip(); return s; } class person { private: string name, address; int age; float height; public: static person fromConsole() { auto p = new person(); /* UFCS in action: Note how these are not written as * read(p.name, /* ... */) */ p.name.read("Enter Your Name"); p.age.read("Enter Your Age"); p.height.read("Enter Your Height"); return p; } float getHeight() { return height; } int getIty() { return age; } string getNome() { return name; } } void main () { person p = person.fromConsole(); writeln(p.getNome()); writeln(p.getIty()); writeln(p.getHeight()); }
 My goal in learning to use languages like Java, C ++, D is
with the
 intention of learning the best way to reuse code and
orienation to
 objects and also development cross-platform codes that will
run in
 standard ansi for, Unix, Linux, Windows, android etc. ..
Ali
I tried to define a method to read vectors of chars but this is giving error auto read(C)(ref C c, char[] message) if (!isSomeChar!C) { writef("\n\t%s: ", message); read(" %s", &c); return c; }
Feb 16 2017