Newbie C++ programmer again... (sorry its so late!)

rusty rustyc@inficad.com
Sun, 27 Aug 2000 21:40:54 -0700


Eric Samson wrote:
> 
> Ok, I have about gone nuts trying to figure this out...
> 
> This is my assignment that I am having trouble with..
> 
> It's a simple currency converter, the user enters a float which indicates
> how many US dollars they want to convert to a specified currency, in my case
> Deutchmarks... ....  This seems like something that
> should be painfully simple...  Is this something too complex for a beginning
> programmer?  I should think that it would be a basic part of programming,
> but I could be wrong...  I notice a few people on here saying they don't use
> C++ much, so what is the language of choice for programming in Linux?
> 
Well, the C++ gurus have had at it, now let me, a lowly C guru have a
shot, in C.

In C, we have the (in)famous scanf that can come to our rescue:

main()
{
  char foobuf[1024]; /* bad practice - buffer overflow exploit! */
  float foofloat;
  puts("Gimme a number, please");
  gets(foobuf);
  if (1 != sscanf(foobuf, "%f", &foofloat))
    {
      printf("'%s' is not a valid float, according to
sscanf\n",foobuf);
      /* if ya really wanted to get cute, you could do some checks */
      /* here and say what actually went worng */
    }
  else
    {
      printf("%f converts to %f, for some value of 'convert' \n",
	     foofloat, foofloat * 1.23);
    }
}

if you compile this under mandrake 7.1, the compiler tells you that
gets is 
dangerous and should not be used - that is because this program is a
security hole waiting to happen (buffer overflow expoit).

Otherwise, it works as requested...  and you can use other functions
to
instead of gets just as well, but i'm too lazy tonight to find them
;-)

rusty

as to the language of choice for Linux, I kinda agree with the comment
a while back about C++, lisp, and perl and/or awk (yes, I know they
are
not in the same class!).  I'd add 'plain old c' as well....

rc