Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C++
#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

int main ()
{
    int A,B,C,D,E,F,bndollar,usdollar,eurodollar,myrdollar,bnd,usd,eur,myr,currency,n,N,y,Y;
    char choice;
    double number;
    while (choice);
    
    
    {
         
    cout << "CURRENCY CONVERTER\n" <<endl;
    cout << "Choose a foreign currency\n" <<endl;
	cout << "1. BND to USD\n"  <<endl;
	cout << "2. USD to BND\n"  <<endl;
	cout << "3. BND to EUR\n"  <<endl;
	cout << "4. EUR to BND\n"  <<endl;
	cout << "5. BND to MYR\n"  <<endl;	
	cout << "6. MYR to BND\n"  <<endl;
	{
    cout << "\nPlease enter the number of the currency you want to convert: ";
    cin >> currency;

    }
    if (choice == 1)
    {  
       cout << "Please enter the amount of BND you would like to convert to USD: ";
       cin >> bndollar;
       cout << "\nYou have entered " << bndollar << "bnd is equal to " << bndollar*0.714950 << "usd." <<endl; 
    }
    
    else if (choice == 2)
    {
         cout << "Please enter the amount of USD you would like to convert to BND: ";
   	     cin >> usdollar;
         cout << "\nYou have entered " << usdollar << "usd is equal to " << usdollar*1.39870 << "usd." <<endl;
    }   
       
    else if (choice == 3)
    {
         cout << "Please enter the amount of BND you would like to convert to EUR: ";
         cin >> bndollar;
         cout << "\nYou have entered " << bndollar << "bnd is equal to " << bndollar*0.661654 << "bnd." <<endl;	    
    }         
       
       
    else if (choice == 4)
    {
         cout << "Please enter the amount of EUR you would like to convert to BND: ";
     	 cin >> eurodollar;
         cout << "\nYou have entered " << eurodollar << "euro is equal to " << eurodollar*1.51136 << "euro." <<endl;	  
    }         
       
    else if (choice == 5)
    {
          cout << "Please enter the amount of BND you would like to convert to MYR: ";
   	      cin >> bndollar;
          cout << "\nYou have entered " << bndollar << "bnd is equal to " << bndollar*3.16401 << "bnd." <<endl;	   
    }   
          
    else if (choice == 6)
    {
            cout << "Please enter the amount of MYR you would like to convert to BND: ";
            cin >> myrdollar;
            cout << "\nYou have entered " << myrdollar << "myr is equal to " << bndollar*0.316055 << "myr." <<endl;	
    }
    
 
    cout << "\n\n\n\nDo you wish to convert again? (Y/N) ";
    cin >> choice;
}

    if (choice != n && choice !=N)
    {
    cout << "\n\n\n\n\n\n\n\nThank You and Goodbye\n";
}
    
    {
   if (choice == y && choice == Y)
     
            cout << "\n";
     }

    system ("pause");
	return 0;  

}


PLease help me what's the problem here?

What I have tried:

When i try to run it, it becomes like this:

CURRENCY CONVERTER

Choose a foreign currency

1. BND to USD

2. USD to BND

3. BND to EUR

4. EUR to BND

5. BND to MYR

6. MYR to BND


Please enter the number of the currency you want to convert: 1




Do you wish to convert again? (Y/N) n








Thank You and Goodbye
Press any key to continue . . .
Posted
Updated 27-Mar-17 22:16pm
v4
Comments
PIEBALDconsult 28-Mar-17 0:09am    
Three things...

0) This is likely wrong:
cin >> currency

1) The users' input is a character, yet you test for a numeric value -- you need to correct that.

2) This isn't likely to do you any good:
if (choice == y && choice == Y)
Sunasara Imdadhusen 28-Mar-17 1:56am    
Don't show your urgency, everybody here are for helping the community based on their time!

First off, indent your code properly: that is hard to read and work out what is going on. This is particularly a problem when you do things like this:
C++
    if (choice != n && choice !=N)
    {
    cout << "\n\n\n\n\n\n\n\nThank You and Goodbye\n";
}
    
    {
   if (choice == y && choice == Y)
     
            cout << "\n";
     }


Second, this test is silly:
if (choice != n && choice !=N)
because choice is char, and both n and N are integers, neither of which gets a value. The same applies to your "Yes" test. Probably, you should dump the variables n , N, y, and Y and use tests like this:
if (choice != 'n' && choice != 'N')
    {
    cout << "\n\n\n\n\n\n\n\nThank You and Goodbye\n";
    }


Thirdly, the AND text in your "Yes" question won't work, even if you fix the characters:
if (choice == 'y' && choice == 'Y')
a variable cannot have two different values at the same time. You want to use "||" as an OR operation instead of "&&" as an AND.

Fourthly, where do you think your while loop should end? Fixing the indentation should help you see what is wrong there...

And please, have a look at that code: you are duplicating a load of stuff that you could easily refactor into more readable - and maintainable - form. Also consider using a switch instead of loads inf if comparisons.
 
Share this answer
 
Comments
Stefan_Lang 30-Mar-17 5:23am    
Actually,
if (choice == 'n' || choice == 'N')
would be even more useful
;-)

Of course, if you like the look of the original expression, you can also minimally change it to
if (!(choice != 'n' && choice != 'N'))
;-p
Quote:
PLease help me what's the problem here?

The problems are numerous, you have uninitialized variables
The semicolon here prevent the loop from doing anything because the loop is empty.
C++
while (choice);

and you are not stuck in this line just by luck because you didn't set the value of choice.
For further problems, use the debugger to see what you code is doing.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
What about:
C++
#include <iostream>
#include <string>
#include <array>
using namespace std;

struct ConversionInfo
{
  string currency[2];
  double ratio;
};

int prompt_user( const array<ConversionInfo,3> & cia)
{
  int count=1;
  cout << "please enter your choice" << endl;
  for (auto ci : cia)
  {
    cout << count << ") " << ci.currency[0] << " to " << ci.currency[1] << endl;
    ++count;
    cout << count << ") " << ci.currency[1] << " to " << ci.currency[0] << endl;
    ++count;
  }

  int choice;
  cin >> choice;
  return choice;
}

int main()
{
  array<ConversionInfo, 3> cia {{{"bnd", "usd", 0.714950}, {"bnd","eur", 0.661654}, {"bnd", "myr", 3.16401}}};

  int choice;

  for(;;)
  {
    choice = prompt_user( cia );

    if ( choice < 1 || choice > 6) break;

    int index = (choice-1) >> 1; // index of the 'cia' array item
    int from_index = (choice-1) & 1; // index of the 'cia.currency' array item
    int to_index = from_index == 0 ? 1 : 0; // index of the 'cia.currency' array item
    double from_amount, to_amount;

    cout << "please enter the amount in " << cia[index].currency[from_index] << endl;
    cin >> from_amount;
    to_amount = from_index == 0 ? from_amount * cia[index].ratio : from_amount / cia[index].ratio;
    cout << "the amount in " << cia[index].currency[to_index] << " is " << to_amount << endl;
  }

  cout << "goodbye" << endl;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900