I am now writing a new program, a modification of a program I have done before but now with arrays. It is a currency conversion program that converts four currencies. User inputs the starting currency and the program will display all four of the currency conversions for the amount the user input. I have the arrays set up, and it works on USD for dollars fine, but something is wrong with it when I try to test it using the latter three currencies. It does not display the preceding columns of the array. (i.e. when I test JPY for yen, it only displays three conversions, and does not display the first, EUR for euros displays only two conversions, etc.) Can someone help point me in the right direction? Thank you in advance for your expertise.

And I am sorry about not having line numbers, my program I use for school has line numbers on it, but they wont let me copy them with the code for some reason. If there is anything such as

type thing when I post code to the site, plz let me know. Thanks.

// currency4.cpp
// Takes an amount in U.S. dollars and converts it to euros, yens, and rupees, or vice versa
// The user inputs the symbol of the starting currency and the amount to be converted


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

using namespace std;



int main()
{

string symbol1 = "USD", symbol2 = "USD";
const int NUMOFROWS = 4;
const int NUMOFCOL = 4;
double matrix[NUMOFROWS][NUMOFCOL] = {
							{1, 109.427, 0.688021, 39.3299},
							{0.00913794, 1, 0.00628664, 0.359449},
							{1.45313, 158.907, 1, 57.1616},
							{0.0254259, 2.78235, 0.0174925, 1}
						  };
int row;
int col;
double newamnt;



    double amount, result, rate;
	{
    cout << "This is a conversion program for U.S. dollars into euros,yens, and rupees or vice versa" << endl;
    
    // get input	
    cout << "Please enter the three-letter symbol of the original currency: " << endl;
	 cout << "USD for Dollars" << endl;
	 cout << "JPY for Yen"  << endl;
	 cout << "EUR for Euros" << endl;
	 cout << "INR for Rupees" << endl;
    cin >> symbol1;
    cout << endl;


    cout << "Please enter the amount to be converted: ";
    cin >> amount;
	 
	 if (symbol1 == "USD" || symbol1 == "usd")
	 	{

			for (row = 0; row < NUMOFROWS; row++)
				{
				
				
				for (col = 0; col < NUMOFCOL; col++)
					newamnt = (amount * matrix[row][col]);
					cout << newamnt << endl;
				}
	 	}
	 if (symbol1 == "JPY" || symbol1 == "jpy")
	 	{

			for (row = 1; row < NUMOFROWS; row++)
				{
				
				
				for (col = 0; col < NUMOFCOL; col++)
					newamnt = (amount * matrix[row][col]);
					cout << newamnt << endl;
				}
	 	}
	 if (symbol1 == "EUR" || symbol1 == "eur")
	 	{

			for (row = 2; row < NUMOFROWS; row++)
				{
				
				
				for (col = 0; col < NUMOFCOL; col++)
					newamnt = (amount * matrix[row][col]);
					cout << newamnt << endl;
				}
	 	}
	 if (symbol1 == "INR" || symbol1 == "inr")
	 	{

			for (row = 3; row < NUMOFROWS; row++)
				{
				
				
				for (col = 0; col < NUMOFCOL; col++)
					newamnt = (amount * matrix[row][col]);
					cout << newamnt << endl;
				}}


	 	}
return 0;
}

Recommended Answers

All 4 Replies

1. After entering symbol1 you could transform the string to all upper case characters so that the rest of the if statements do not need to check for lower case or any other case mixture. transform(symbol1.begin(),symbol1.end(),symbol1.begin(),toupper); You need to include <algorithm> to compile the above line

2. The problem you report caused by all those row loops. If I were you I'd create four different functions that perform the conversion for each currency, then they can be easily and efficiently called as required after line 49. For example

if( symbol1 == "USD")
{
    ConvertJPY(amount);
    ConvertEUR(amount);
    ConvertINR(amount);
}
else if( symbol1 == "JPY")
{
    ConvertUSD(amount);
    ConvertEUR(amount);
    ConvertINR(amount);
}
else if( symbol1 == "EUR")
{
    ConvertUSD(amount);
    ConvertJPR(amount);
    ConvertINR(amount);
}

After looking at that a little more I see you don't need to use any loops at all -- the program can be written without loops. I assume the output should look like this: Using loops like you did causes too many conversions.

This is a conversion program for U.S. dollars into euros,yens, and rupees or vice versa
Please enter the three-letter symbol of the original currency:
USD for Dollars
JPY for Yen
EUR for Euros
INR for Rupees
jpy

Please enter the amount to be converted: 1
109.427 USD
158.907 Euro
2.78235 Rupees
Press any key to continue . . .

You are correct on the output of the program on how it should look. I usually have no problems getting a programs output the way it should look after I get the program to work.

I am not seeing how to do this program without using loops in it though.

The code snipped (function calls) need to be modified slightly to add another parameter, which column of the matrix to use, for example

if (symbol1 == "USD")
	 	{
            ConvertJPY(amount, USDCol);
            ConvertEUR(amount, USDCol);
            ConvertINR(amount, USDCol);
	 	}
	 else if (symbol1 == "JPY")
	 	{
            ConvertUSD(amount, JPYCol);
            ConvertEUR(amount, JPYCol);
            ConvertINR(amount, JPYCol);
	 	}
	 else if (symbol1 == "EUR")
	 	{
            ConvertUSD(amount, EURCol);
            ConvertJPY(amount, EURCol);
            ConvertINR(amount, EURCol);

	 	}
	 else if (symbol1 == "INR")
	 {
            ConvertUSD(amount, INRCol);
            ConvertJPY(amount, INRCol);
            ConvertEUR(amount, INRCol);
     }

Then each of those four functions need only one line of code

void _inline ConvertUSD(float amount,int col)
{
	cout << (amount * matrix[USDRow][col]) << " USD\n";
}

Make the array matrix global, add some const ints to define those I used (or your own col and row names) and you are home free.

commented: Awesome, not the first time you've helped me out. Much abliged. +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.