write small currency converter

I

Ivan Vecerina

mereba said:
OK. I just compiled and run Erik's rewrite, and it works perfectly. I
guess I'm now left to figure out how to write a GUI for this small
program. Earlier posters have advised that I should join other forums
for topics about GUIs. However, I would very much like for the people
who pointed me to all the right directions to suggest GUI tools that I
should try out and select. I would want to be able to let this program
run on both windows and linux systems.

Someone already mentioned Qt as a GUI framework, which can be used
on both Windows and Linux. It is free if you accept to use the
GPL licence ( make your source code available as well ).
See http://trolltech.com/products/qt/

Other cross-platform GUI framework with more liberal licenses
include wxWidgets (www.wxwidgets.org) or the lightweight
FLTK (www.fltk.org).

Another tip: a classic example application is a temperature
converter (Celsius to Fahrenheit). It would be easy to modify
such an application to do what you need. You should be able
to find a few examples in C++, as well as in other languages.

Finally, unless there is a reason for you to use C++, you may
want to consider using another for this job. For instance, if
a javascript-enabled browser is available on all your target
computers (be it Windows, MaxOSX, Linux, BSD, or whatever),
a converter could be provided as a simple HTML file:

<html> <head>
<title>Currency Converter</title>
</head> <body>
<font size=+1>Enter a value in either field and press tab
<br /><br />
<form>
Old currency: <input type="text" name="Old" value="0"
onChange="New.value = (this.value/100).toFixed(2) ">
<br /><br />
New currency: <input type="text" name="New" value="0"
onChange="Old.value = (this.value*100) ">
</form
</body> </html>

Save the few lines above as convert.html and open it in
a web browser. Easy to edit and improve...


[ sorry for being OT, but even though I make my living
using C++, I wouldn't use it for that job ;) ]

Regards,
Ivan
 
J

James Kanze

GUI is beyond the scope of this newsgroup, it is platform dependant.

It's not necessarily platform dependant: wxWidgets, for example,
can be used pretty much everywhere. (Well, pretty much
everywhere that supports windowing. I doubt that it could be
ported to a PDP-11 with a teletype.)

More to the point, perhaps, is that GUI programming is at least
as complicated as C++. A question like "how do I write a GUI?"
is about as answerable as "how do I write a program in C++?".
There's a trivial but useless answer: use a text editor, but
anything useful would go well beyond the scope of a news
article. And while I think that the original charter does allow
for questions concerning portable libraries, like wxWidgets,
realistically, it's a language within a language, and everyone
(including the person with the question) is better off if the
question is asked in a dedicated group, like
comp.soft-sys.wxwidgets. But even there, only after having read
a good introductory tutorial, read the FAQ, etc., etc.
 
M

mereba

It's not necessarily platform dependant: wxWidgets, for example,
can be used pretty much everywhere. (Well, pretty much
everywhere that supports windowing. I doubt that it could be
ported to a PDP-11 with a teletype.)

More to the point, perhaps, is that GUI programming is at least
as complicated as C++. A question like "how do I write a GUI?"
is about as answerable as "how do I write a program in C++?".
There's a trivial but useless answer: use a text editor, but
anything useful would go well beyond the scope of a news
article. And while I think that the original charter does allow
for questions concerning portable libraries, like wxWidgets,
realistically, it's a language within a language, and everyone
(including the person with the question) is better off if the
question is asked in a dedicated group, like
comp.soft-sys.wxwidgets. But even there, only after having read
a good introductory tutorial, read the FAQ, etc., etc.

--
James Kanze (GABI Software) email:[email protected]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Thank you for your suggestion James. I have started with a wxwidgets
tutorial. Thank you all for the good suggestions you all have given me.
 
M

mereba

OK. I just compiled and run Erik's rewrite, and it works perfectly. I
guess I'm now left to figure out how to write a GUI for this small
program. Earlier posters have advised that I should join other forums
for topics about GUIs. However, I would very much like for the people
who pointed me to all the right directions to suggest GUI tools that I
should try out and select. I would want to be able to let this program
run on both windows and linux systems.

Someone already mentioned Qt as a GUI framework, which can be used
on both Windows and Linux. It is free if you accept to use the
GPL licence ( make your source code available as well ).
Seehttp://trolltech.com/products/qt/

Other cross-platform GUI framework with more liberal licenses
include wxWidgets (www.wxwidgets.org) or the lightweight
FLTK (www.fltk.org).

Another tip: a classic example application is a temperature
converter (Celsius to Fahrenheit). It would be easy to modify
such an application to do what you need. You should be able
to find a few examples in C++, as well as in other languages.

Finally, unless there is a reason for you to use C++, you may
want to consider using another for this job. For instance, if
a javascript-enabled browser is available on all your target
computers (be it Windows, MaxOSX, Linux, BSD, or whatever),
a converter could be provided as a simple HTML file:

<html> <head>
<title>Currency Converter</title>
</head> <body>
<font size=+1>Enter a value in either field and press tab
<br /><br />
<form>
Old currency: <input type="text" name="Old" value="0"
onChange="New.value = (this.value/100).toFixed(2) ">
<br /><br />
New currency: <input type="text" name="New" value="0"
onChange="Old.value = (this.value*100) ">
</form
</body> </html>

Save the few lines above as convert.html and open it in
a web browser. Easy to edit and improve...

[ sorry for being OT, but even though I make my living
using C++, I wouldn't use it for that job ;) ]

Regards,
Ivan

--http://ivan.vecerina.com/contact/?subject=NG_POST<- email contact form

Thank you Ivan for this javascript snippet. To be honest, I know only
intermediate HTML and no javascript at all. I've noted the code
nonetheless. I would really want to implement the same programme in as
many ways as possible (in the future), but for now, I'm bent on
accomplishing it with C++, since C++ is my first language.
 
P

Puppet_Sock

My country Ghana is changing its currency. I want to write a small
programme in C++ that can covert from the old currency into the new
one.

Some advice about solving this particular problem:
It's not as simple as just dividing by the rate and
reporting a result. You need to specify exactly what
to do with various amounts. For example, what do you
do with roundoff? Perhaps there are laws about what
an institution such as a bank has to do when they are
converting money? Otherwise, you are going to have
people angry at you because the roundoff values are
not done correctly.
Socks
 
B

BobR

mereba said:
Thank you Marcus Kwok for the wxWidgets link.
However, I'm confused about the command line options programme. What
does it do? Really I would like to know. I run your programme and this
is the output I got:

argc = 1
argv[0] = ./commandline

I guess "./commandline" without the quotes is showing up because I
named the object file commandline? Please explain further

Usually the first argv is the program being invoked. On windows, we usually
get the full pathname, 'D:\MyStuff\MyProgram'.

Try starting your program with some commandline arguments:

./commandline -aflag -aparm "my line of input"

What do you get for output now?
 
M

mereba

Some advice about solving this particular problem:
It's not as simple as just dividing by the rate and
reporting a result. You need to specify exactly what
to do with various amounts. For example, what do you
do with roundoff? Perhaps there are laws about what
an institution such as a bank has to do when they are
converting money? Otherwise, you are going to have
people angry at you because the roundoff values are
not done correctly.
Socks

Thank you Socks for your concerns. It's perfectly true that there are
laws that govern rounding up, but that's not a problem at all since
the amounts involved are very little. For example if you entered 20550
into the programme, you would get GHC2 and Gp5 . You notice that Gp0.5
has been rounded down to zero. The law says you may round up or round
down to zero your under Gp1 values. Besides, there is nothing like
Gp0.5 in the new currency. The least you can get is Gp1 . Just to make
things a little clearer, Gp100 = GHC1 .
 
J

James Kanze

BobR said:
Thank you Marcus Kwok for the wxWidgets link.
However, I'm confused about the command line options programme. What
does it do? Really I would like to know. I run your programme and this
is the output I got:
argc = 1
argv[0] = ./commandline
I guess "./commandline" without the quotes is showing up because I
named the object file commandline? Please explain further
Usually the first argv is the program being invoked.

According to the standard, argv[0] is the command used to invoke
the program. Which is kind of vague: with a Unix shell: before
or after alias and variable expansion? Before or after path
look up? And what about programs which are invoked from other
programs, using e.g. execl() under Unix? Or programs invoked by
clicking on an icon on the desktop? Similarly, the following
argv should be the command line arguments, but it's up to the
implementation to decide what is or is not an argument.

In practice, both Unix and Windows (I think---I'm not 100% sure
about Windows) allow an invoking program to put pretty much
whatever it wants in argv---some Unix utilities depend on very
strange argv[0] to tell them that they've been invoked in
special circumstances. For the most part, however, if the
commaand really is invoked from the command line of a command
interpreter, you can probably count on argv[0] having something
to do with the invocation command, and on the following argv
corresponding to the "arguments" according to the command
interpreters idea of how a command line should be broken up into
arguments.
On windows, we usually
get the full pathname, 'D:\MyStuff\MyProgram'.

Technically, that's not conform, even though it's probably the
most useful behavior. And when you say, "we usually get", do
you mean when the program is started by clicking on an icon, or
from the command line in a console window (using which command
interpreter), or both?

(I'm not trying to criticize your answer here. I know you gave
a simple answer to someone who is pretty much a beginner, not
wanting to confuse him with details. But I'm honestly curious;
I know that the behavior under Unix is in fact fairly complex,
although most users can ignore the complexity most of the time,
and I'm curious as to the details under Windows.)
 
M

mereba

mereba said:
Thank you Marcus Kwok for the wxWidgets link.
However, I'm confused about the command line options programme. What
does it do? Really I would like to know. I run your programme and this
is the output I got:
argc = 1
argv[0] = ./commandline
I guess "./commandline" without the quotes is showing up because I
named the object file commandline? Please explain further

Usually the first argv is the program being invoked. On windows, we usually
get the full pathname, 'D:\MyStuff\MyProgram'.

Try starting your program with some commandline arguments:

./commandline -aflag -aparm "my line of input"

What do you get for output now?

Bob R, I literally inserted "my line of input", and when I ran the
programme, this is the output I got:

argc = 4
argv[0] = ./commandline
argv[1] = -aflag
argv[2] = -aparm
argv[3] = my line of input

It doesn't appear to me that argv is doing anything fancy here -
storing strings into an array, of course according to what commandline
has been compiled to do. Is there more to this than meets the eye?
 
B

BobR

/* """
(I'm not trying to criticize your answer here. I know you gave
a simple answer to someone who is pretty much a beginner, not
wanting to confuse him with details. But I'm honestly curious;
I know that the behavior under Unix is in fact fairly complex,
although most users can ignore the complexity most of the time,
and I'm curious as to the details under Windows.)
""" */

Well, without throwing any more brain-dead ideas at you, maybe see Ralf
Browns list (is that puppy still around? :-} ).
Under DOS/Assembler we were assured to get a specific set of data, relative
to the address the program was loaded to. Under 'vista', 'ntfs', 'Reiserfs
(sp?)', etc., well I just don't know.
Every OS/filesystem seems to 'do it their way' (F.Sinatra). <G>
[ I guess it's a little late to ask for a standardization of the 'comand
line' args?
<G> (oops, there's that brain-dead' now) ]


This is from some *old* docs I have, GCC libC, "Program Arguments":
"
The value of the argc argument is the number of command line arguments. The
argv argument is a vector of C strings; its elements are the individual
command line argument strings.
** The file name of the program being run is also included in the vector as
the first element; the value of argc counts this element. **
A null pointer always follows the last element: argv[argc] is this null
pointer.
// .... later...
In Unix systems you can define main a third way, using three arguments:
int main (int argc, char *argv[], char *envp[])
"
Newer docs may have some amendments.

I know under wxWidgets(wxMSW, wxGTK,(others?)), the 'argc/argv' are provided
(but I have not examined their methods).
 
B

BobR

mereba said:
Try starting your program with some commandline arguments:

./commandline -aflag -aparm "my line of input"

What do you get for output now?

Bob R, I literally inserted "my line of input", and when I ran the
programme, this is the output I got:

argc = 4
argv[0] = ./commandline
argv[1] = -aflag
argv[2] = -aparm
argv[3] = my line of input

It doesn't appear to me that argv is doing anything fancy here -
storing strings into an array, of course according to what commandline
has been compiled to do. Is there more to this than meets the eye?

Next step, do something with the input.

#include <iostream>
#include <string>

int main(int argc, char* argv[]){
using std::cout;
cout << "argc = " << argc << '\n';
for (int i = 0; i != argc; ++i) {
cout << "argv[" << i << "] = " << argv << '\n';

std::string arg( argv );
if( arg.find("-aparm") != arg.npos ){
cout<<"\"-aparm\" found."<<'\n';
// - do stuff here -
}
if( arg.find("-aflag") != arg.npos ){
cout<<"\"-aflag\" found."<<'\n';
// - do stuff here -
}
// etc.
}

return 0;
}

There are different ways, but, I don't want to confuse you. Take it in small
steps. Do you know std::vector?
 
M

mereba

Bob R, I literally inserted "my line of input", and when I ran the
programme, this is the output I got:
argc = 4
argv[0] = ./commandline
argv[1] = -aflag
argv[2] = -aparm
argv[3] = my line of input
It doesn't appear to me that argv is doing anything fancy here -
storing strings into an array, of course according to what commandline
has been compiled to do. Is there more to this than meets the eye?

Next step, do something with the input.

#include <iostream>
#include <string>

int main(int argc, char* argv[]){
using std::cout;
cout << "argc = " << argc << '\n';
for (int i = 0; i != argc; ++i) {
cout << "argv[" << i << "] = " << argv << '\n';

std::string arg( argv );
if( arg.find("-aparm") != arg.npos ){
cout<<"\"-aparm\" found."<<'\n';
// - do stuff here -
}
if( arg.find("-aflag") != arg.npos ){
cout<<"\"-aflag\" found."<<'\n';
// - do stuff here -
}
// etc.
}

return 0;
}

There are different ways, but, I don't want to confuse you. Take it in small
steps. Do you know std::vector?


Thanks Bob R for your efforts. I'll play around with the code, and try
to grasp the argc and argv[] parameter concepts.

No. I don't yet know std::vector. I've googled around a little for it,
and so far all I know is that it is a standard header (#include
<vector>). I'm yet to understand it a little better. Please post links
that can help my understanding.
 
J

James Kanze

Well, without throwing any more brain-dead ideas at you, maybe see Ralf
Browns list (is that puppy still around? :-} ).
Under DOS/Assembler we were assured to get a specific set of data, relative
to the address the program was loaded to. Under 'vista', 'ntfs', 'Reiserfs
(sp?)', etc., well I just don't know.
Every OS/filesystem seems to 'do it their way' (F.Sinatra). <G>

It's worse, at least under Unix: every shell seems to to it
their way.
[ I guess it's a little late to ask for a standardization of the 'comand
line' args?
<G> (oops, there's that brain-dead' now) ]

I wonder. Maybe Posix does something like this, at least for
programs started from the command line in a Posix conformant
shell.
This is from some *old* docs I have, GCC libC, "Program Arguments":
"
The value of the argc argument is the number of command line arguments. The
argv argument is a vector of C strings; its elements are the individual
command line argument strings.
** The file name of the program being run is also included in the vector as
the first element; the value of argc counts this element. **
A null pointer always follows the last element: argv[argc] is this null
pointer.
// .... later...
In Unix systems you can define main a third way, using three arguments:
int main (int argc, char *argv[], char *envp[])
"
Newer docs may have some amendments.

That's what you see. It doesn't really explain how it gets
there. For argv[0], for example, under Unix (Solaris anyway),
Bash apparently gives exactly what I typed in the command line
(after expanding shell variables and aliases); under Windows, it
gives the full path name in Windows format (whereas the shell
does some really strange mappings, at least the CygWin version
of Bash). Different shells differ (at least slightly) as to how
to cut up the command line into arguments---I think that the
standard Windows command interpreter differs greatly here.

Under Unix, a child process is started via a system request such
as execl. Where the caller specifies all of the argv arguments,
independantly of the path to the executable. So a caller can
very easily specify something like "./toto" as the path, and
"../otherUser/titi" as argv[ 0 ]. There are even a few special
cases where argv[0] is used as a special hidden argument; login
will force the initial character to '-' when it starts a user
shell, for example.

What I'm curious about is how this all takes place under
Windows. Does the OS force argv[0] to be something specific, or
can the process which starts the program do what it likes?
I know under wxWidgets(wxMSW, wxGTK,(others?)), the
'argc/argv' are provided (but I have not examined their
methods).

They intercept main, and give you whatever the OS gives, I would
imagine.
 

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,291
Messages
2,571,479
Members
48,143
Latest member
Manuelevimb

Latest Threads

Top