Simple console input / output framework for teaching beginners

S

Stefan Ram

Jorgen Grahn said:
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++.
 
K

K. Frank

Hi Jorgen!

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

Ian Collins

Jorgen said:
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.
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.
 
S

Stefan Ram

Ian Collins said:
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«.
 
J

Jorgen Grahn

Jorgen Grahn wrote: ....


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
 
J

Jorgen Grahn

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

woodbrian77

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
 
D

David Brown

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

woodbrian77

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
 
D

David Brown

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

Ian Collins

David said:
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++.
 
D

David Brown

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

woodbrian77

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



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




C++ is _not_ better than C, just different.


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
 
Ö

Öö Tiib

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

Stefan Ram

Öö Tiib said:
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?
 
Ö

Öö Tiib

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

Rosario193

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
 
R

Rosario193

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;
}
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,091
Messages
2,570,605
Members
47,225
Latest member
DarrinWhit

Latest Threads

Top