Simple console input / output framework for teaching beginners

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

  1. K. Frank

    Stefan Ram Guest

    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!

    I certainly agree with that.
    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.

    Further thoughts would be very welcome.


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

  3. K. Frank

    Ian Collins Guest

    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.
    I agree there.
    But not here! With the current fad for Ajax style web applications, C++
    is a fine choice for the back end.
     
    Ian Collins, May 27, 2014
    #23
  4. K. Frank

    Stefan Ram Guest

    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

    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, May 28, 2014
    #25
  6. K. Frank

    Jorgen Grahn Guest

    .
    "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, May 28, 2014
    #26
  7. K. Frank

    Guest

    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

    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

    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

    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

    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, May 30, 2014
    #31
  12. K. Frank

    David Brown Guest

    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

    What OSes? I worked on Taligent years ago.
    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

    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.
    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

    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

    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.
    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.
    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'.
    '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

    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:
    */


    #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
     
    Rosario193, Jun 4, 2014
    #37
  18. K. Frank

    Rosario193 Guest

    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

Ask a Question

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.