?
Solved

Need help with writing program:

Posted on 2003-02-24
35
Medium Priority
?
216 Views
Last Modified: 2010-04-02
Hi all,
I'm new to c++, and I have to write a program to convert a monetary amount from one country's currency to another country's currency. The three currencies to be used are dollars, deutsche marks, and francs.

In the program I have to prompt the user to continue with another conversion when the programs finished, or quit. My first question is to do this will I have to use a loop, or something else?

Also I need to know basically how I need to approach this program. I have to first prompt the user on which currency they want to convert from, and then what currency they want to convert to. The only way I see to do it is with a if then statement for all of it, but I don't see any way to keep the program from having to be a mile long to include all the conversion formula's.

I would imagine I would have to definine the conversions in one part of the program, but I have no idea what kind of statements I'd use to do this. If someone could tell me or point me in the direction of a program similiar to the one I need to write, it would be much apreciated.

Thanks in advance,
JT
0
Comment
Question by:JTatWVU
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 7
  • 5
  • +6
35 Comments
 
LVL 8

Expert Comment

by:Exceter
ID: 8013553
It is against EE policy for experts to complete homework problems.

Please read the membership agreement,
http://www.experts-exchange.com/jsp/infoMemberAgreement.jsp

If you really want help, post some code and ask a specific question about your problem.

Exceter
0
 
LVL 1

Expert Comment

by:Gula
ID: 8013590
>>My first question is to do this will I have to use a loop, or something else?

--yes, loop, with condition to keep on looping as long as person wants to continue
I think do...while is the best loop for that

>>Also I need to know basically how I need to approach this program. I have to first prompt the user on which currency they want to convert from, and then what currency they want to convert to.

--doesn't really matter which comes first as long as you will have both of them before calculating

>>The only way I see to do it is with a if then statement for all of it, but I don't see any way to keep the program from having to be a mile long to include all the conversion formula's.

--if..else is good, I don't think it will be too long


>>I would imagine I would have to definine the conversions in one part of the program, but I have no idea what kind of statements I'd use to do this.

--I didn't clearly understand here what you are asking,

I'm not sure how much of programming you know, so I will try to provide as simple solution as possible,

try this:

you will have 6 possible cases of conversion...
so you can give menu for that, like
1. dollars to marks
2. dollars to francs
3. francs to dollars
....so on

then ask them to enter the menu choice and amount to be converted
and depending on the choice, use appropriate formula,
this part you can implement with switch...case or if

0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8013704
no source codes ..there can be so many ways to do..
 but here is a very simple one...

typedef struct _table{
char *currency;
float factor;
}table;
#define MAXCURRENCYTYPE 4
table tab[MAXCURRENCYTYPE]={
{"dollar",1.0},
{"euro", 0.98},
{"frac",0.60},
{"Rupees ",0.02}
};

// we have initialised this table that each entry contains the value of particulr currency's one  unit in  US dollars.. u can take any currency as base currency.

now ask user the amount :lets say  'srcamt'
ask source currency  'src'
ask target currency 'trgt'

look up the index of source currency .. srcidx
look up theindex of target currency..   trgtidx
( figure out urself )

target amount
tgtamt=srcamt*tab[srcidx].factor/tab[trgtidx].factor;

i'll leave you now on implementing this idea.. put some effort and let us know..
since this is cplusplus area .. ur tutor wud like you to have some sort of Converter class..

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:JTatWVU
ID: 8014132
Thanks for responding guys. Like I said I'm very new to C++ so a lot of what was posted I don't understand or know how to use. I'm just gonna post what I have of the program so far, and you guys can tell me what I did wrong so far, and where I need to go from here to do the conversions.

Please note: My includes are probably not correct. I don't even know how to figure out which ones to use. Also, one of the requirements for my program is that I prompt the user asking if they want to do a conversion or not, and if not, then the program just needs to terminate. I don't know how to do this, as you can see below.


// All that stuff

#include <iostream>
#include <string>
#include <iomanip>

int main()
{
    float intro;
    float srcamt;
    float trgtamt;
    float amount;
       
    cout << "Do you want to convert an amount? (y or n)";
    cin >> intro;
   
    if (intro == n)
    TERMINATE
   
else
    if (intro == y)
   
    cout << "What currency do you want to convert from? ($, DM, Fr):";
    cin >> srcamt;
   
    cout << "What currency do you want to convert to? ($, DM, Fr):";
    cin >> trgtamt;
   
    cout << "Enter the amount:";
    cin >> amount;
   
WHERE TO GO FROM HERE?
   
    return 0;
    }
   
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 8014218
I have changed the declaration of variables. Now it must compile without errors. But, you have to write code for conversion. What is the formula you are using for doing currency conversion. If you can give it to us, I think we can suggest something:

#include <iostream>
#include <string>
#include <iomanip>

int main()
{
   char intro;
   string srcamt;
   string trgtamt;
   float amount;
       
   cout << "Do you want to convert an amount? (y or n)";
   cin >> intro;
   
   if (intro == 'n') exit(0);
   else if (intro == 'y')
   {
      cout << "What currency do you want to convert from? ($, DM, Fr):";
      cin >> srcamt;
   
      cout << "What currency do you want to convert to? ($, DM, Fr):";
      cin >> trgtamt;
   
      cout << "Enter the amount:";
      cin >> amount;
   }

//WHERE TO GO FROM HERE? - Do the conversions here...
 
   return 0;
}
0
 

Author Comment

by:JTatWVU
ID: 8014319
This is the formula I've been given:

$1.00  =  5.05 Francs
$1.00  =  1.47 Deutsche marks
1.00 DM  =  3.4354 Francs
0
 
LVL 8

Accepted Solution

by:
ssnkumar earned 200 total points
ID: 8014354
Then this should do:

#include <iostream>
#include <string>
#include <iomanip>

int main()
{
   char intro;
   string srcamt;
   string trgtamt;
   float amount, conv;

   cout << "Do you want to convert an amount? (y or n)";
   cin >> intro;

   if (intro == 'n') exit(0);
   else if (intro == 'y')
   {
      cout << "What currency do you want to convert from? ($, DM, Fr):";
      cin >> srcamt;

      cout << "What currency do you want to convert to? ($, DM, Fr):";
      cin >> trgtamt;

      cout << "Enter the amount:";
      cin >> amount;
   }

        if (srcamt == "$")
        {
                if (trgtamt == "$") conv = amount;
                else if (trgtamt == "DM") conv = amount * 1.47;
                else if (trgtamt == "Fr") conv = amount * 5.05;
                else
                {
                                cout << "Wrong input!!!\n";
                                exit(0);
                }
        }
        else if (srcamt == "DM")
        {
                if (trgtamt == "$") conv = amount / 1.47;
                else if (trgtamt == "DM") conv = amount;
                else if (trgtamt == "Fr") conv = amount * 3.4354;
                else
                {
                                cout << "Wrong input!!!\n";
                                exit(0);
                }
        }
        else if (srcamt == "Fr")
        {
                if (trgtamt == "$") conv = amount / 5.05;
                else if (trgtamt == "DM") conv = amount / 3.4354;
                else if (trgtamt == "Fr") conv = amount;
                else
                {
                                cout << "Wrong input!!!\n";
                                exit(0);
                }
        }
        else
        {
                cout << "Wrong input!!!\n";
                exit(0);
        }

        cout << amount << srcamt << " = " << conv << trgtamt << endl;

//WHERE TO GO FROM HERE? - Do the conversions here...

   return 0;
   }
0
 

Expert Comment

by:alexanderthegreat
ID: 8015910
Man you gotta be kidding right?.
I think the program language is not for you, when you want to write programs you have to have a creative mind
and as far as i see you dont have that, a small formula like this should be very easy to do, you should have seen how to do this.




 
0
 
LVL 8

Expert Comment

by:Exceter
ID: 8016480
alexanderthegreat, what is the purpose of that statement?
0
 

Expert Comment

by:alexanderthegreat
ID: 8016661
Just giving some advise.
Whats with you and with that homework thing exceter?,
if you dont want to answer a question just dont.



0
 
LVL 1

Expert Comment

by:Gula
ID: 8016903
>>Man you gotta be kidding right?.
I think the program language is not for you, when you want to write programs you have to have a creative mind
and as far as i see you dont have that, a small formula like this should be very easy to do, you should have seen how to do this.


This is bad...alexanderthegreat, How about "if you dont want to answer a question just dont." to you

and Exceter, all he(she) wants maybe more experts on this site and out there, and take the note he/she posted there as reminder to experts not to solve homeworks, because very often they do
0
 

Expert Comment

by:alexanderthegreat
ID: 8017058
Why is it bad to say what i think?,
by the way i think exceter is a male.
0
 
LVL 8

Expert Comment

by:Exceter
ID: 8017691
>> Whats with you and with that homework thing exceter?,
>> if you dont want to answer a question just dont.

Oh nothing, it's just that it is a direct violation of the EE membership agreement that EVERYONE on this site agreed to when they signed up. Violating it could get you banned from the site! Also, I beleive that doing other peoples' homework is unethical and selfish. Such activites undermine the asker's education for nothing more than a few points. That is clearly counterproductive. Don't you think?

>> Why is it bad to say what i think?,

Because that is not the purpose of his topic area. If you want to say what you think you should go to the lounge.

Exceter
0
 

Author Comment

by:JTatWVU
ID: 8021332
Thanks for the help guys, especially ssnkumar. One problem though.. I need to know a good free c++ compiler. The one I use right now (bloodshed dev c++) just closes after I execute the program rather than printing the output.

alexanderthegreat, like I said at the top of the post, I'm a beginner with C++. Your comment was just ignorant and uncalled for.

Thanks guys,
JT
0
 
LVL 8

Expert Comment

by:Exceter
ID: 8021832
>> I need to know a good free c++ compiler.

Check out this PAQ,
http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20518401.html

Exceter
0
 

Author Comment

by:JTatWVU
ID: 8022489
Alright I tried using a different compiler and the program is still closing after entering the amount and hitting enter. I don't see what's wrong with it. What should I do? -JT
0
 
LVL 8

Expert Comment

by:Exceter
ID: 8022636
Do you mean that the DOS window is closing? How are you executing the program? Are you executing it from a command prompt or are you executing it through windows? If you are executing it through windows the DOS box will disappear when the program terminates.

Exceter
0
 

Author Comment

by:JTatWVU
ID: 8022668
Uhhh yeah actually I am executing it through windows. I use windows xp, how do I execute through a command prompt? -JT
0
 
LVL 8

Expert Comment

by:Exceter
ID: 8022729
Go to the start menu and click on run. Type in cmd and press enter. This will bring up a DOS command window. You must then navigate to the appropriate directory and execute the program. For example, if your program was located at c:\cpp\Myprog.exe you would type,

cd\cpp
Myprog

Exceter
0
 

Author Comment

by:JTatWVU
ID: 8022773
The location of mine is C:\Documents and Settings\JT\Desktop

And the file is rough2.cpp

So wouldn't my location be: C:\Documents and Settings\JT\Desktop\rough2.cpp?

Am I missing something because it's not working.
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 8022775
>Type in cmd and press enter
On some systems you will need to give "command". The shortcut cmd wont work. And if the path is not set, then you will need to give the complete path with the command: C:\command.com
or c:\windows\command.com

It will work without giving the .com extension.

-Narendra
0
 

Author Comment

by:JTatWVU
ID: 8022783
The location of mine is C:\Documents and Settings\JT\Desktop

And the file is rough2.cpp

So wouldn't my location be: C:\Documents and Settings\JT\Desktop\rough2.cpp?

Am I missing something because it's not working.
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 8022792
It's okay to execute through windows.  What I normal do in a situation like this is declare
int done;


at the end of program after output
cout << "\npress enter key to exit\n";
cin >> done;

This will force the screen to stay up.  To run from a command prompt in xp, just run cmd, cd your way to the dir where the .exe is held, and run it from there.

As for Alexander, how do you know he doesn't have a creative mind, it's obvious (no offense to JTat) that he's new to the language, he just doesn't know the tools yet.  It's like handing you a whole bunch of heavy machinery and say, "Here, build me a tower that's structuarlly sound, and by the way, the manuals for these were developed for people who know how to use them and they're hidden everywhere, good luck."  Everyone has to start somewhere, and a comment like that does nothing but add stress and demean the person.  You should commend him for the fact that he's trying and he's asking for help to learn.

Jtat: Just a few more things:

1)    cout << "Do you want to convert an amount? (y or n)";
      cin >> intro;

When doing something like this, I usually like to use integers, being the fact that there's only one possibility.

If the user has caps lock on, or inputs 'Y' or 'N' it will jump right over the the if (intro == 'n') etc.  If you want to use characters, that's fine, but you should add an OR statement (noted by || [double pipes]) IE:

if (intro == 'n' || intro == 'N')
    terminate();

make sure you understand everything in ssnumkar's code.  This is the BASIS for C++ and if you don't understand that you'll get lost easily.  Any other questions as of yet?
or if you want to be lazy about it.  (Say you have a character menu with 20 options all of them lower case and you don't want to add the or's for all 20 "If-else's" or "Switch" statements: you can use the following:

 intro = tolower(intro);

That function forces the character to be a lower case letter, (IE if upper case, it's now lower.  If it was lower to begin with, it leaves it alone.)  then you can just check for the lower case.  

0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 8022794
PS.  Make sure you click the submit button only once, no need to make it repost number of times.
0
 
LVL 8

Expert Comment

by:Exceter
ID: 8022834
>> >Type in cmd and press enter
>> On some systems you will need to give "command". The
>> shortcut cmd wont work. And if the path is not set,
>> then you will need to give the complete path with the
>> command: C:\command.com
>> or c:\windows\command.com

cmd.exe is used on NT based platforms. command.com is used in Dos, Win 95, Win 98, and Win ME. I would have said FAT16 or FAT32 but NT systems can be installed on those types of hard disk partitions, doing so simply sacrifices the security afforded by NTFS file permissions.

Exceter
0
 

Author Comment

by:JTatWVU
ID: 8022836
The location of mine is C:\Documents and Settings\JT\Desktop

And the file is rough2.cpp

So wouldn't my location be: C:\Documents and Settings\JT\Desktop\rough2.cpp?

Am I missing something because it's not working.
0
 
LVL 1

Expert Comment

by:Computer101
ID: 8022848
alexanderthegreat,

Flaming of others is not allowed.  I will afford you the opportunity to succeed.  Abide by the members agreement or I will handle your account accordingly

Computer101
E-E Admin
0
 

Author Comment

by:JTatWVU
ID: 8023115
Alright I'm executing my program fine now, I just need to know one more thing (I think). My answer needs to be displayed up to 2 decimal places. Ex:

8823.92 Fr

I need to know the code to do this please. -JT
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 8023260
Use:
cout << setprecision(3) << endl;
before printing the float values.

-Narendra
0
 

Author Comment

by:JTatWVU
ID: 8027083
Alright one last thing, what can I put at the end of my program to make it start over. I want it to display the conversion, and at the same time below it, prompt the user with the initial "Would you like to do a monetary conversion?". Thanks -JT
0
 

Expert Comment

by:alexanderthegreat
ID: 8027168
do { //at the beginning of the program

cout << "Would you like to run the program again?: ";
cin >> yes;

}while(yes=='y'); //at the end of the program.
0
 

Author Comment

by:JTatWVU
ID: 8027226
I'm confused. I tried putting those statements in and I kept getting weird compiler errors. Here is my program so far. Where would I put the statements in to get it to work?:


//-------------------------------------------------------------
//--
//-- Author: Justin Martin
//-- CS 110 Section 001
//-- Lab Section: 004
//-- Lab Instructor:  Hemambika Payyappillil
//-- Last Modified: February 26th, 2003
//--
//-- This program inputs a monetary value in either dollars,
//-- francs, or deutsche marks, and converts the value into
//-- the equivalent in dollars, francs, or deutsche marks.
//------------------------------------------------------------

#include <iostream>
#include <string>
#include <iomanip>

int main()
{
  char intro;
  string srcamt;
  string trgtamt;
  float amount, conv;

  cout << "Do you want to convert an amount? (Y or N) ";
  cin >> intro;

  if (intro == 'N') exit(0);
  else if (intro == 'Y')
  {
     cout << "What currency do you want to convert from? (U, D, F): ";
     cin >> srcamt;

     cout << "What currency do you want to convert to? (U, D, F): ";
     cin >> trgtamt;

     cout << "Enter the amount: ";
     cin >> amount;
  }

       if (srcamt == "U")
       {
               if (trgtamt == "U") conv = amount;
               else if (trgtamt == "D") conv = amount * 1.47;
               else if (trgtamt == "F") conv = amount * 5.05;
               
       }
       else if (srcamt == "D")
       {
               if (trgtamt == "U") conv = amount / 1.47;
               else if (trgtamt == "D") conv = amount;
               else if (trgtamt == "F") conv = amount * 3.4354;
             
       }
       else if (srcamt == "F")
       {
               if (trgtamt == "U") conv = amount / 5.05;
               else if (trgtamt == "D") conv = amount / 3.4354;
               else if (trgtamt == "F") conv = amount;
             
       }
       
     
     
cout << amount << " " << srcamt << " is equivalent to " << conv << " " << setprecision(3) << trgtamt << endl;

  return 0;
 
  }
0
 

Expert Comment

by:alexanderthegreat
ID: 8027315
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
 char intro;
 string srcamt;
 string trgtamt;
 float amount, conv;
 char again;
 
 do{
 cout << "Do you want to convert an amount? (Y or N) ";
 cin >> intro;

 if (intro == 'N' || intro == 'n') exit(0);
 else if (intro == 'Y'|| intro == 'y')
 {
    cout << "What currency do you want to convert from? (U, D, F): ";
    cin >> srcamt;

    cout << "What currency do you want to convert to? (U, D, F): ";
    cin >> trgtamt;

    cout << "Enter the amount: ";
    cin >> amount;
 }

      if (srcamt == "U")
      {
              if (trgtamt == "U") conv = amount;
              else if (trgtamt == "D") conv = amount * 1.47;
              else if (trgtamt == "F") conv = amount * 5.05;
             
      }
      else if (srcamt == "D")
      {
              if (trgtamt == "U") conv = amount / 1.47;
              else if (trgtamt == "D") conv = amount;
              else if (trgtamt == "F") conv = amount * 3.4354;
             
      }
      else if (srcamt == "F")
      {
              if (trgtamt == "U") conv = amount / 5.05;
              else if (trgtamt == "D") conv = amount / 3.4354;
              else if (trgtamt == "F") conv = amount;
             
      }
     
     
     
cout << amount << " " << srcamt << " is equivalent to " << conv << " " << setprecision(3) << trgtamt << endl;
cout << "Would you like to run the program again?: ";
cin >> again;
 }while(again=='y' || again=='Y');
 return 0;
 
 }
0
 

Author Comment

by:JTatWVU
ID: 8029308
Alright I got the project finished and turned in, thanks for all your help guys! -JT
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9845962
No comment has been added lately, so it's time to clean up this TA. I will
leave a recommendation in the Cleanup topic area that this question is:

Answered: Points to ssnkumar: Grade A

Please leave any comments here within the next seven days.

Experts: Silence means you don't care. Grading recommendations are made in light
of the posted grading guidlines (http://www.experts-exchange.com/help.jsp#hi73).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question