• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

Need help with writing program:

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
JTatWVU
Asked:
JTatWVU
  • 12
  • 7
  • 5
  • +6
1 Solution
 
ExceterCommented:
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
 
GulaCommented:
>>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
 
akshayxxCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
JTatWVUAuthor Commented:
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
 
ssnkumarCommented:
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
 
JTatWVUAuthor Commented:
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
 
ssnkumarCommented:
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
 
alexanderthegreatCommented:
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
 
ExceterCommented:
alexanderthegreat, what is the purpose of that statement?
0
 
alexanderthegreatCommented:
Just giving some advise.
Whats with you and with that homework thing exceter?,
if you dont want to answer a question just dont.



0
 
GulaCommented:
>>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
 
alexanderthegreatCommented:
Why is it bad to say what i think?,
by the way i think exceter is a male.
0
 
ExceterCommented:
>> 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
 
JTatWVUAuthor Commented:
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
 
ExceterCommented:
>> 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
 
JTatWVUAuthor Commented:
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
 
ExceterCommented:
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
 
JTatWVUAuthor Commented:
Uhhh yeah actually I am executing it through windows. I use windows xp, how do I execute through a command prompt? -JT
0
 
ExceterCommented:
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
 
JTatWVUAuthor Commented:
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
 
ssnkumarCommented:
>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
 
JTatWVUAuthor Commented:
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
 
Kyle AbrahamsSenior .Net DeveloperCommented:
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
 
Kyle AbrahamsSenior .Net DeveloperCommented:
PS.  Make sure you click the submit button only once, no need to make it repost number of times.
0
 
ExceterCommented:
>> >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
 
JTatWVUAuthor Commented:
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
 
Computer101Commented:
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
 
JTatWVUAuthor Commented:
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
 
ssnkumarCommented:
Use:
cout << setprecision(3) << endl;
before printing the float values.

-Narendra
0
 
JTatWVUAuthor Commented:
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
 
alexanderthegreatCommented:
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
 
JTatWVUAuthor Commented:
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
 
alexanderthegreatCommented:
#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
 
JTatWVUAuthor Commented:
Alright I got the project finished and turned in, thanks for all your help guys! -JT
0
 
bcladdCommented:
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

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 12
  • 7
  • 5
  • +6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now