Simple console input / output framework for teaching beginners

Discussion in 'C++' started by K. Frank, May 26, 2014.

  1. K. Frank

    Stefan Ram Guest

    Jorgen Grahn <> writes:
    >The other side of the coin is, I think with the rise of the GUI
    >it suddenly became much harder to become a hobby programmer.


    Hobby programmers used to use BASIC, and GUI programming
    with VBA is a snap.

    The problem starts only when people that want to become
    hobby programmers insist on learning/using C++.
     
    Stefan Ram, May 27, 2014
    #21
    1. Advertisements

  2. K. Frank

    K. Frank Guest

    Hi Jorgen!

    On Tuesday, May 27, 2014 3:34:38 AM UTC-4, Jorgen Grahn wrote:
    > ...
    > One thing is, if you "buy" the command line way of working, it's much
    > easier to write C++ programs to get things done.


    I certainly agree with that.

    > And it's easier to
    > play with and test those programs.


    This is also true, and part of my motivation.

    There have been many, many times when I was writing
    a real program that I compiled in some data or test
    parameters, and when I needed to change the test, I
    changed the parameters and recompiled. This gets
    tiresome very quickly (and counts as a bad habit, as
    well).

    Having a simple (non-production quality) cin input driver
    (or argc/argv driver, but let's stick with cin for this
    discussion) is really helpful (for me, at least) for
    development and testing.

    It seems to me pedagogically sound to teach beginners
    (or intermediates, or whatever) how to do this, and
    encourage them to do so, although a lot of people in
    this discussion disagree with this.

    So what do we teach them? What actual code do we walk
    them through to show them how to approach this with good,
    modern C++, while keeping it simple enough for an
    instructional setting?

    There's been lots of good discussion in this thread, but
    Stefan's post was the only one that suggested any code.

    > ...
    > /Jorgen



    Further thoughts would be very welcome.


    K. Frank
     
    K. Frank, May 27, 2014
    #22
    1. Advertisements

  3. K. Frank

    Ian Collins Guest

    Jorgen Grahn wrote:
    > On Tue, 2014-05-27, Öö Tiib wrote:
    >>
    >> In real world the GUI apps are often tightly integrated with command
    >> line programs or scripts (often "app" being only thin wrapper).

    >
    > Is that really that common? I associate it with older Unix GUIs ...
    > but I must admit that I'm a command line user myself.


    I don't know how real my wold is, but my applications are often back
    ends for a web application, so most of their data comes in through the
    standard input.

    >> What is the point to keep that as secret for wannabe programmer?
    >> It still feels good idea to explain *how* *software* *is* *made* to
    >> students who want to learn that. When their prior impression is
    >> naive then there is simply more to teach.

    >
    > One thing is, if you "buy" the command line way of working, it's much
    > easier to write C++ programs to get things done. And it's easier to
    > play with and test those programs. Especially if you buy into the
    > Unix pipelone philosophy. Many of my best programs are just building
    > blocks which integrate with grep, sort et cetera.


    I agree there.

    > The other side of the coin is, I think with the rise of the GUI
    > it suddenly became much harder to become a hobby programmer.
    >
    > Perhaps web programming is a third route which is easier, quicker and
    > more useful, but C++ is not strong in that environment.


    But not here! With the current fad for Ajax style web applications, C++
    is a fine choice for the back end.

    --
    Ian Collins
     
    Ian Collins, May 27, 2014
    #23
  4. K. Frank

    Stefan Ram Guest

    Ian Collins <> writes:
    >I don't know how real my wold is, but my applications are often back


    I visited the real world once, and everything was like here,
    except that the Un*x system call »creat« was spelt: »create«.
     
    Stefan Ram, May 28, 2014
    #24
  5. K. Frank

    Jorgen Grahn Guest

    On Tue, 2014-05-27, Ian Collins wrote:
    > Jorgen Grahn wrote:

    ....

    >> Perhaps web programming is a third route which is easier, quicker and
    >> more useful, but C++ is not strong in that environment.

    >
    > But not here! With the current fad for Ajax style web applications, C++
    > is a fine choice for the back end.


    I was not very clear -- I meant AFAICT C++ doesn't have a strong
    presence in that area. Or does it? I know almost nothing about the
    web world.

    If the APIs are there, I see no reason why C++ wouldn't work well.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, May 28, 2014
    #25
  6. K. Frank

    Jorgen Grahn Guest

    On Wed, 2014-05-28, Drew Lawson wrote:
    ....
    > I spent about 5 years working on web apps for
    > LexisNexis, ending about 5 years ago. The bulk of what I worked
    > on was in C++.
    >
    > They were shifting toward more Java/Javascript/AJAX at the time.
    >
    > At the time, this was a "grand new system" project, largely wanting
    > to replace the "old thing" with a "new thing." I suspect that the
    > new thing ended up as rushed and kludged as the old thing had evolved
    > into.


    "We wanted the best, but it turned out as always"
    -- Viktor Chernomyrdin

    (Which was the grand new system by the way -- yours or the
    AJAX thing?)

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, May 28, 2014
    #26
  7. K. Frank

    Guest

    On Wednesday, May 28, 2014 1:28:30 AM UTC-5, Jorgen Grahn wrote:
    > On Tue, 2014-05-27, Ian Collins wrote:
    >
    > > Jorgen Grahn wrote:

    >
    > ...
    >
    >
    >
    > >> Perhaps web programming is a third route which is easier, quicker and

    >
    > >> more useful, but C++ is not strong in that environment.

    >
    > >

    >
    > > But not here! With the current fad for Ajax style web applications, C++

    >
    > > is a fine choice for the back end.

    >
    >
    >
    > I was not very clear -- I meant AFAICT C++ doesn't have a strong
    > presence in that area. Or does it? I know almost nothing about the
    > web world.
    >


    One thing I find surprising is how little C++ is used in
    terms of web servers. That seems kind of embarrassing.


    Brian
    Ebenezer Enterprises
    http://webEbenezer.net
     
    , May 28, 2014
    #27
  8. K. Frank

    David Brown Guest

    On 28/05/14 23:03, wrote:
    > On Wednesday, May 28, 2014 1:28:30 AM UTC-5, Jorgen Grahn wrote:
    >> On Tue, 2014-05-27, Ian Collins wrote:
    >>
    >>> Jorgen Grahn wrote:

    >>
    >> ...
    >>
    >>
    >>
    >>>> Perhaps web programming is a third route which is easier, quicker and

    >>
    >>>> more useful, but C++ is not strong in that environment.

    >>
    >>>

    >>
    >>> But not here! With the current fad for Ajax style web applications, C++

    >>
    >>> is a fine choice for the back end.

    >>
    >>
    >>
    >> I was not very clear -- I meant AFAICT C++ doesn't have a strong
    >> presence in that area. Or does it? I know almost nothing about the
    >> web world.
    >>

    >
    > One thing I find surprising is how little C++ is used in
    > terms of web servers. That seems kind of embarrassing.
    >


    Why should that be embarrassing? Different languages have their
    strengths in different areas. C++ is a useful language, but it is not
    the best choice for every task.
     
    David Brown, May 29, 2014
    #28
  9. K. Frank

    Guest

    On Thursday, May 29, 2014 3:06:38 PM UTC, David Brown wrote:
    > On 28/05/14 23:03, wrote:
    >
    > > One thing I find surprising is how little C++ is used in
    > > terms of web servers. That seems kind of embarrassing.

    >
    > Why should that be embarrassing? Different languages have their
    > strengths in different areas. C++ is a useful language, but it is not
    > the best choice for every task.


    This seems to me to be an area where C++ should do
    well. I thought something like this about C++
    compilers before Clang came along. I think Microsoft
    has long had C++-based C++ compilers also.

    It feels in web server land like compiler land
    without Clang or Microsoft compilers. If arguments
    made here about the advantages of C++ are right,
    why isn't there a C++-based webserver that is
    giving Apache and Nginx a lot of competition?
    I buy the arguments about C++ being better than C
    and figure eventually a C++-based web server will
    be taking market share away from Apache and Nginx.
    Perhaps Apache or Nginx will make the switch
    themselves rather than having someone do it for
    them.

    Brian
    Ebenezer Enterprises - So far G-d has helped us.
    http://webEbenezer.net
     
    , May 29, 2014
    #29
  10. K. Frank

    David Brown Guest

    On 29/05/14 22:35, wrote:
    > On Thursday, May 29, 2014 3:06:38 PM UTC, David Brown wrote:
    >> On 28/05/14 23:03, wrote:
    >>
    >>> One thing I find surprising is how little C++ is used in
    >>> terms of web servers. That seems kind of embarrassing.

    >>
    >> Why should that be embarrassing? Different languages have their
    >> strengths in different areas. C++ is a useful language, but it is not
    >> the best choice for every task.

    >
    > This seems to me to be an area where C++ should do
    > well. I thought something like this about C++
    > compilers before Clang came along. I think Microsoft
    > has long had C++-based C++ compilers also.
    >
    > It feels in web server land like compiler land
    > without Clang or Microsoft compilers. If arguments
    > made here about the advantages of C++ are right,
    > why isn't there a C++-based webserver that is
    > giving Apache and Nginx a lot of competition?
    > I buy the arguments about C++ being better than C
    > and figure eventually a C++-based web server will
    > be taking market share away from Apache and Nginx.
    > Perhaps Apache or Nginx will make the switch
    > themselves rather than having someone do it for
    > them.
    >
    > Brian
    > Ebenezer Enterprises - So far G-d has helped us.
    > http://webEbenezer.net
    >


    Ah, you are thinking about the webserver itself rather than
    website-specific code, for which Python, Ruby, PHP, etc., are often
    better suited and definitely more popular.

    For the webserver itself, then I can understand thinking C++ is a better
    choice than C (though I still think languages like Python can be a good
    choice). I would guess that the biggest reason is historical - people
    who write webservers are used to C, and when most major webservers were
    started, C++ was not nearly as good a language as it is now (both the
    tools and the standards have improved significantly in recent years).
    If someone were to write a new general-purpose webserver from scratch,
    then it is not unlikely that they would choose C++ over C.

    However, I still don't see it as "embarrassing". There are only a few
    major general webservers - far too few to consider it an "area where C++
    should do well".
     
    David Brown, May 29, 2014
    #30
  11. K. Frank

    Ian Collins Guest

    David Brown wrote:
    >
    > Ah, you are thinking about the webserver itself rather than
    > website-specific code, for which Python, Ruby, PHP, etc., are often
    > better suited and definitely more popular.


    I guess it's down to programmer mass (not fat programmers!).

    My standard approach to web applications is to have a boiler-plate CGI
    process on the web server that formats the requests in a common format
    and forwards them on to the back end. Naturally C++ is the ideal choice
    of language in this situation!

    The main problem I have found with web application design is web
    developers are a conservative bunch and they stick to what they know:
    JavaScript+PHP or JavaScript+.NET. Only a minority of PHP or .NET
    programmers know C++.

    --
    Ian Collins
     
    Ian Collins, May 30, 2014
    #31
  12. K. Frank

    David Brown Guest

    On 30/05/14 01:33, Ian Collins wrote:
    > David Brown wrote:
    >>
    >> Ah, you are thinking about the webserver itself rather than
    >> website-specific code, for which Python, Ruby, PHP, etc., are often
    >> better suited and definitely more popular.

    >
    > I guess it's down to programmer mass (not fat programmers!).
    >
    > My standard approach to web applications is to have a boiler-plate CGI
    > process on the web server that formats the requests in a common format
    > and forwards them on to the back end. Naturally C++ is the ideal choice
    > of language in this situation!
    >
    > The main problem I have found with web application design is web
    > developers are a conservative bunch and they stick to what they know:
    > JavaScript+PHP or JavaScript+.NET. Only a minority of PHP or .NET
    > programmers know C++.
    >


    The deciding factor for web applications is, I think, the amount of
    effort required to write that type of code. C++ is a great language in
    many ways - but for handling strings and texts, languages like Python
    and PHP are vastly easier. PHP (and Python, if you use the right tools)
    can also be mixed with the HTML, making it simple to add a little bit of
    server-side code to a mostly static page. And the interpreted nature of
    these languages makes the whole development process faster and more
    iterative. In this branch, time-to-market is usually a far more
    important metric than run-time efficiency.
     
    David Brown, May 30, 2014
    #32
  13. K. Frank

    Guest

    On Friday, May 30, 2014 10:49:55 AM UTC-5, Scott Lurndal wrote:
    > writes:
    >
    > >On Thursday, May 29, 2014 3:06:38 PM UTC, David Brown wrote:

    >
    > >> On 28/05/14 23:03, wrote:

    >
    > >>

    >
    > >> > One thing I find surprising is how little C++ is used in

    >
    > >> > terms of web servers. That seems kind of embarrassing.

    >
    > >>

    >
    > >> Why should that be embarrassing? Different languages have their

    >
    > >> strengths in different areas. C++ is a useful language, but it is not

    >
    > >> the best choice for every task.

    >
    > >

    >
    > >This seems to me to be an area where C++ should do

    >
    > >well. I thought something like this about C++

    >
    > >compilers before Clang came along. I think Microsoft

    >
    > >has long had C++-based C++ compilers also.

    >
    >
    >
    > I've worked on two full operating systems and two
    > complete hypervisors (all running bare metal, obviously)
    > in C++ since 1989. Although, for performance reasons
    > many things needed to be avoided (all rtti, all exceptions,
    > all templates, well templates didn't exist when we
    > started three of the four projects).


    What OSes? I worked on Taligent years ago.

    >
    > One of the large certification authorities web-back end
    > infrastructure is all C++ (mostly written in the mid 90's).
    >
    > C++ is a viable language for most projects, but one needs to
    > often use a subset of the total language depending on
    > the target.
    >
    > Quite a bit of embedded code is also written in C++.
    >
    > >

    >
    > >It feels in web server land like compiler land
    > >without Clang or Microsoft compilers. If arguments
    > >made here about the advantages of C++ are right,
    > >why isn't there a C++-based webserver that is

    >
    > Why does anyone _care_ what language a webserver
    > is written in? For all you know, IIS _is_ written
    > in C++ (I believe that is it, actually).
    >
    > >giving Apache and Nginx a lot of competition?
    > >I buy the arguments about C++ being better than C

    >
    >
    >
    > C++ is _not_ better than C, just different.
    >
    > >and figure eventually a C++-based web server will
    > >be taking market share away from Apache and Nginx.

    >
    > will never happen.
    >
    > Why? Because the language a web-server is written
    > in _DOESN'T MATTER TO ANYONE_ (except, apparently, you).


    A C++ program is potentially better organized than a
    C program -- more robust, easier to maintain, more
    efficient ...

    So at least from a developer perspective I think what
    language is used matters to a lot of people. Some
    people, myself included, are happy to use C++ 2011
    and 2014 in their work and would not want to have to
    go back to an earlier version.

    Brian
    Ebenezer Enterprises - Unless the L-RD builds the house,
    they labor in vain that build it. Psalms 127:1
    http://webEbenezer.net
     
    , May 31, 2014
    #33
  14. K. Frank

    Öö Tiib Guest

    On Saturday, 31 May 2014 19:20:49 UTC+3, wrote:
    > On Friday, May 30, 2014 10:49:55 AM UTC-5, Scott Lurndal wrote:
    > > writes:
    > > >and figure eventually a C++-based web server will
    > > >be taking market share away from Apache and Nginx.

    > >
    > > will never happen.
    > >
    > > Why? Because the language a web-server is written
    > > in _DOESN'T MATTER TO ANYONE_ (except, apparently, you).

    >
    > A C++ program is potentially better organized than a
    > C program -- more robust, easier to maintain, more
    > efficient ...


    How so? "Program" in both languages is pile of text
    files loosely arranged with some make script. How can
    C++ program be better organized than C program?

    C++ contains big number of convenience features above
    C but all of those can be misused to achieve fragility
    and non-maintainability. Engineers by whom product is
    made have to put special effort into achieving robustness
    and maintainability in both languages.

    > So at least from a developer perspective I think what
    > language is used matters to a lot of people. Some
    > people, myself included, are happy to use C++ 2011
    > and 2014 in their work and would not want to have to
    > go back to an earlier version.


    Perhaps I do not understand what are you talking about.
    The topic was web development?

    On client side there is web browser. Code base from what
    browser was compiled possibly contained both C and C++.
    No one cares. As web developer you have to run on browser
    what runs on web browser. C++, Java, C# or C do not run on
    browser. Javascript runs. So browser apps are coded in
    Javascript.

    AJAX is technology for the web browser apps (written in
    Javascript) to communicate with servers asynchronously
    on background. AJAX has again nothing to do with C, C++,
    Java or C#.

    The popular web servers are mostly written in C and some
    in C++. Again no one cares, like with browsers. These are
    ready-made products. Back end services for these web
    servers are programs installed together with that server
    and (like all programs) are writable in outright anything.
     
    Öö Tiib, May 31, 2014
    #34
  15. K. Frank

    Stefan Ram Guest

    Öö Tiib <> writes: How can
    >C++ program be better organized than C program?


    Because C++ supports OOP, and OOP programs are better
    organized than procedural programs.

    In C++, one can extend the meaning of operators, such as »+«
    or »<<« for new types without the need to modify existing
    code. This means that C++ suppors the open-closed principle.
    One can use natural wordings such as »m + m1« for the
    addition of two matrices, which is impossible in C. So, C++
    programs are easier to read and to maintain, which is the
    actual goal of all attempts at being »organized«.

    In C, one has to call »sinh« for the hyperbolic sine of a
    double, but »sinhf« for the hyperbolic sine of a float, and
    »sinhl« for the hyperbolic sine of a long double. Now, what
    kind of language is this?
     
    Stefan Ram, May 31, 2014
    #35
  16. K. Frank

    Öö Tiib Guest

    On Saturday, 31 May 2014 23:42:54 UTC+3, Stefan Ram wrote:
    > �� Tiib <> writes: How can
    > >C++ program be better organized than C program?

    >
    > Because C++ supports OOP, and OOP programs are better
    > organized than procedural programs.


    C++ does support various programming paradigms better than C.
    For example it is more convenient to follow OOP with C++. That
    does not turn programs written in C into "procedural programs"
    and programs written in C++ into "OOP programs".

    I myself like OOP, but it is not such a clear silver bullet.
    Some, (like the original architect of STL, Alexander Stepanov)
    have been always rather strongly critical against OOP.

    > In C++, one can extend the meaning of operators, such as �+�
    > or �<<� for new types without the need to modify existing
    > code. This means that C++ suppors the open-closed principle.


    Most programs in the world use modules written in C. Often as
    closed source modules. C supports that principle well enough.
    C++ modules often enwrap themselves under C interface to
    break loose and to be independent and still useful.

    > One can use natural wordings such as �m + m1� for the
    > addition of two matrices, which is impossible in C. So, C++
    > programs are easier to read and to maintain, which is the
    > actual goal of all attempts at being �organized�.


    Operator overloading is syntax sugar. It can make things
    easier to read. Everybody have had that fun "to read
    and to maintain" the classical problem of default copy
    assignment operator not doing deep copy. My favorite
    wtf of C++ of all times was also that operator, in that
    code:

    x = x;

    The line was written to break the mutexes open in that
    object 'x'.

    > In C, one has to call �sinh� for the hyperbolic sine ofa
    > double, but �sinhf� for the hyperbolic sine of a float,and
    > �sinhl� for the hyperbolic sine of a long double. Now, what
    > kind of language is this?


    'sinhl' is perfectly expressive name to indicate that floating point
    is not for kids. Rudimentary support, if you are capable then make
    the tools you need, otherwise run. It is same for both C and C++.

    Every aspect of the floating point types is "implementation-defined".
    Some aspects can be modified dynamically runtime with some functions
    like 'clearfp' and 'controlfp'. Lot of fun.
     
    Öö Tiib, Jun 1, 2014
    #36
  17. K. Frank

    Rosario193 Guest

    On Sun, 25 May 2014 16:48:52 -0700 (PDT), "K. Frank"
    <> wrote:

    >Hello Group!
    >
    >Could people suggest a simple program that illustrates using
    >console input with basic error handling that would be suitable
    >for rank beginners?


    i had the same think but with the C language....

    so i wrote one library for include the right functions
    and so the exercise program can be link with library function

    free for use
    i not know if they are right
    _____________________________________________
    /*
    libreria.dll
    per costruire la .dll e la .lib usare
    usare:
    >bcc32 -v -WD libreria.c
    >implib libreria.lib libreria.dll


    */


    #include <stdio.h>
    #include <stdlib.h>
    #include <float.h>
    #include <limits.h>
    #include <math.h>
    #include <windows.h>

    // ritorna un puntatore da liberarsi tramite free()
    // 0 per errore
    char* __export GetLine(char* testo)
    {unsigned s, i, vMax=UINT_MAX-1032; // 1024+4+1 [+ 3]=1032
    int c;
    char *p,*t;

    s=1024; p=malloc(s+4);
    if(p==0) return 0;
    if(testo!=0 && printf("%s", testo)<0)
    {la: free(p); return 0;}
    if(fflush(stdout)!=0)
    goto la;
    for(i=0; (c=getchar())!=EOF ; )
    {if(i>=s)
    {if(s>=vMax)
    goto la;
    s=s+1024;
    t=realloc(p, s+4);
    if(t==0)
    goto la;
    p=t;
    }
    p=c; ++i;
    if(c=='\n')
    break;
    }
    if(ferror(stdin))
    goto la;
    p=0;
    return p;
    }

    // fa apparire il testo nello schermo e prende una stringa
    // da stdin
    // ritorna un puntatore a una *stringa* da liberare con free().
    // ritorna 0 per errore.
    char* __export GetLineI(char* testo)
    {unsigned s, i;
    int c, r;
    char *p,*t;

    s=1024; p=malloc(s+4); if(p==0) return 0;
    r=printf("%s", testo); if(r< 0) goto li;
    r=fflush(stdout); if(r==EOF) goto li;

    for(i=0; (c=getchar())!=EOF ; )
    {if(i>=s)
    {s=s+1024;
    if(s+4>(unsigned) INT_MAX )
    goto li;
    t=realloc(p, s+4);
    if(t==0)
    {li: free(p); return 0;}
    p=t;
    }
    p=c; ++i;
    if(c=='\n') break;
    }
    p=0;
    if(ferror(stdin))
    goto li;
    return p;
    }

    // par: res: pointer to string of input
    // testo: string to write to the screen
    // return
    // (unsigned)-1 for error
    // or if ok return the len of string in res
    unsigned __export GetLineII(char** res, char* testo)
    {unsigned s, i;
    int c, r;
    char *p,*t;

    if(res==0) return -1;
    s=1024; p=malloc(s+4); if(p==0) return -1;
    r=printf("%s", testo); if(r < 0) goto li;
    r=fflush(stdout); if(r==EOF) goto li;

    for(i=0; (c=getchar())!=EOF ; )
    {if(i>=s)
    {s=s+1024;
    if(s+4>(unsigned) INT_MAX )
    goto li;
    t=realloc(p, s+4);
    if(t==0)
    {li: free(p); return -1;}
    p=t;
    }
    p=c; ++i;
    if(c=='\n') break;
    }
    p= 0;
    *res= p;
    if(ferror(stdin))
    return -1;
    return i;
    }


    // ritorna INT_MIN per errore
    int __export GetInt(char* testo)
    {int i,c;
    long v;
    char arr[1028], *p=0;

    printf("%s", testo); fflush(stdout);
    for(i=0; i<1024; ++i)
    {c=getchar();
    if(c==EOF||c=='\n')
    break;
    arr=(char)c;
    }
    arr=0;
    if(i==1024) // elimina la linea fino a '\n'
    {while((c=getchar())!=EOF)
    if(c=='\n') break;
    return INT_MIN;
    }
    v=strtol(arr, &p, 10);
    if(v<INT_MIN||v>INT_MAX||p==arr)
    return INT_MIN;
    return (int) v;
    }

    // ritorna UINT_MAX per errore
    unsigned __export GetUns(char* testo)
    {int i,c;
    unsigned long v;
    char arr[1028], *p=0;

    printf("%s", testo); fflush(stdout);
    for(i=0; i<1024; ++i)
    {c=getchar();
    if(c==EOF||c=='\n')
    break;
    arr=(char)c;
    }
    arr=0;
    if(i==1024) // elimina la linea fino a '\n'
    {while((c=getchar())!=EOF)
    if(c=='\n') break;
    return UINT_MAX;
    }
    v=strtoul(arr, &p, 10);
    if(v>UINT_MAX||p==arr)
    return UINT_MAX;
    return (unsigned) v;
    }


    // ritorna DBL_MIN per errore
    double __export GetDouble(char* testo)
    {int i,c;
    double v;
    char arr[1028], *p=0;

    printf("%s", testo); fflush(stdout);
    for(i=0; i<1024; ++i)
    {c=getchar();
    if(c==EOF||c=='\n')
    break;
    arr=(char)c;
    }
    arr=0;
    if(i==1024) // elimina la linea fino a '\n'
    {while((c=getchar())!=EOF)
    if(c=='\n') break;
    return DBL_MIN;
    }
    v=strtod(arr, &p);
    if(v==HUGE_VAL||p==arr)
    return DBL_MIN;
    return v;
    }

    void __export err(char* i)
    {printf("Errore: ");
    if(i) printf("%s", i);
    printf("\nEsco\n");
    exit(0);
    }

    int WINAPI
    DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*
    lpReserved)
    {(void) hinst; (void) lpReserved;
    if(reason == DLL_PROCESS_ATTACH){}
    else if(reason == DLL_PROCESS_DETACH){}
    return 1;
    }
    -----------------
    libreria.h
    #include <float.h>
    #include <limits.h>
    char* __import GetLineI (char* testo);
    int __import GetInt (char* testo);
    unsigned __import GetUns (char* testo);
    double __import GetDouble(char* testo);
    void __import err(char* i);
    -------------------

    use as
    double b;
    b=GetDouble("insert a double: ");
    if(b==DBL_MIN) error123();
    etc

    >Some context and more detail:
    >
    >The question comes up from time to time as to whether it makes sense
    >to use C++ to teach programming to new programmers. (I think the
    >answer is yes, but this particular issue is not really my question.)
    >
    >If you want to do so, you need to teach them basic programming
    >concepts, good programming habits, and good C++ programming
    >habits before you teach them all, or most, a lot, or even some
    >of the intricacies of C++.
    >
    >Imagine that you are teaching a beginning programming class that
    >uses C++, and that the students have written their "hello world"
    >program, have a text editor or ide, and have made their peace with
    >whatever compile / build process they are using. Now you want them
    >to start writing simple programs that perform various simple tasks
    >on input data, and you want them to be able to read in data from the
    >console (cin), and write the results to the console (cout). (For the
    >sake of argument, let's stick to using cin rather than argc / argv
    >for input.)
    >
    >But you want your students to be able to input their data in some
    >reasonably convenient way, and not have their program exit (or crash)
    >every time they mis-type some input data on the console (and you also
    >want your students to start facing the reality that the outside world
    >is not so accommodating as to make programming trivial, and you need to
    >have some kind of validation and error checking for input data, and,
    >ideally, need to give your users some support with thier data input).
    >
    >So "cin >> var1 >> var2 >> var3; do_computation (var1, var2, var2);"
    >is out.
    >
    >One approach might be to write a message to cout such as "enter var1,
    >var2, and var3", enter a loop in which you read in the data, verify,
    >and either print out a "try-again" message or accept it. You might do
    >this a couple of times if there were a couple of different chunks of
    >data you needed to read in. (But maybe there are other good approaches.)
    >
    >What sample program would you suggest to give your students a basic
    >framework for this kind of simple data input? They wouldn't necessarily
    >be able to use the code unchanged from program to program, but it should
    >be suitable for use in similar, but distinct applications, if only through
    >copy-paste-modify.
    >
    >Ideally, you should be able to walk the students through the sample in
    >one class period, and they should be able to understand the code and the
    >basic logic, even if they don't understand all of the nuances.
    >
    >(For example, you would probably use std:string. If so, the students
    >presumably won't know (yet) that std::string is a specialized template,
    >or even that std::string is a library class, rather than a primitive type.
    >But they should know -- after you tell them -- that std::string is a type
    >that holds the value of a string, and is a good first choice for working
    >with strings.)
    >
    >The idea is to give the students something that -- within the context of
    >being one of the earliest programs they encounter, and subsequently write
    >on their own -- starts them on the path of using modern C++ idioms (specifically
    >C++11), and good C++ habits.
    >
    >There is clearly a trade-off between truly good code and code simple enough
    >for rank beginners. But within the constraint of the necessary simplicity
    >we want to start illustrating good programming habits.
    >
    >
    >Any comments and suggestions would be very welcome.
    >
    >
    >K. Frank
     
    Rosario193, Jun 4, 2014
    #37
  18. K. Frank

    Rosario193 Guest

    On Wed, 04 Jun 2014 21:48:21 +0200, Rosario193
    <> wrote:

    >On Sun, 25 May 2014 16:48:52 -0700 (PDT), "K. Frank"
    ><> wrote:
    >
    >>Hello Group!
    >>
    >>Could people suggest a simple program that illustrates using
    >>console input with basic error handling that would be suitable
    >>for rank beginners?

    >
    >i had the same think but with the C language....
    >
    >so i wrote one library for include the right functions
    >and so the exercise program can be link with library function
    >
    >free for use


    >i not know if they are right


    this a simple program that use library

    #include <stdio.h>
    #include <stdlib.h>
    #include "libreria.h"

    // bcc32 -v thisfile.c libreria.lib
    // commento semplice
    // include per le funzioni printf(), exit() e per GetDouble()

    /*
    commento a piu' linee
    la funzione errore esce dal programma
    */
    void errore(void)
    {printf("Errore di input Esco\n"); exit(0);}


    int main(void)
    {double a=0.0, b=1.0, r;
    printf("Programma che fa la somma di due numeri a e b\n");
    printf("Digitare per b *zero* per finire il ciclo\n");
    while(b!=0.0)
    {a=GetDouble("Inserisci a: ");
    if(a==DBL_MIN) errore();

    b=GetDouble("Inserisci b: ");
    if(b==DBL_MIN) errore();
    r=a+b;
    printf("Il risultato e': %.10f\n", r);
    }
    return 0;
    }
     
    Rosario193, Jun 4, 2014
    #38
    1. Advertisements

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. W. D. Allen Sr.

    Simple HTML User Input and Process Output

    W. D. Allen Sr., Sep 12, 2005, in forum: HTML
    Replies:
    2
    Views:
    760
    Toby Inkster
    Sep 12, 2005
  2. Ben Finney
    Replies:
    2
    Views:
    1,322
    Egor Bolonev
    Jun 30, 2003
  3. Miktor
    Replies:
    7
    Views:
    12,910
    Joe Bacigalupa
    Aug 9, 2005
  4. pratap
    Replies:
    3
    Views:
    772
    James Kanze
    Jul 19, 2007
  5. Replies:
    5
    Views:
    3,874
    James Kanze
    Mar 4, 2008
  6. Replies:
    42
    Views:
    3,777
    Richard Bos
    Aug 31, 2009
  7. Kris Rudin

    console app w/o seeing console?

    Kris Rudin, Nov 11, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    408
    Randy Charles Morin
    Nov 11, 2003
  8. Stefan Ram

    Teaching Java, teaching what?

    Stefan Ram, Dec 8, 2013, in forum: Java
    Replies:
    57
    Views:
    1,226
    Ali Sh
    Dec 18, 2013
Loading...