# What's wrong with this C++ program...

##### Diamond Member
Having trouble getting this C++ program to run properly. If anyone has any advice I would appreciate it greatly.

Here's the assignment:

1. Write a program that converts between Dollars, Euros and Pounds. The program reads input from the user in the following format: Convert amount currency_1 to currency_2 and prints results in the obvious way. Here are a couple of sample runs:

Command: Convert 3.50 Euros to Dollars
Results: 3.43679

Command: Convert 3.50 Euros to Pounds
Results: 2.21707

2. Here are the conversion rates you'll need: 1.00 Dollar is 1.01838 Euros and 1.00 Dollar is 0.64510 Pounds.
3. Extend your program from Part 1 to allow for Canadian dollars as well (1.00 Dollar US is 1.55870 Dollars Canadian). Now the user can't simply put "Dollar" in the input, it must be either "Dollar US" or "Dollar Canadian". Here are a couple of sample runs:

Command: Convert 3.50 Euros to Dollars US
Results: 3.43679

Command: Convert 11.72 Dollars US to Dollars Canadian
Results: 18.26796

Turn-in: Source code and a screen shot of your program executing the four sample commands listed above.

here's the code:
------------------------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <string>
#include <cmath>
using namespace std;

float amnt;
double currency;
string bogus1, bogus2, mon1, mon2, mon3;
const double USeu = 1.01838, USuk = 0.64510, UScd = 1.55870;

int main ()
{

if (cin >> bogus1 >> amnt >> mon1 >> bogus2 >> mon2)
{
if (mon1 == "Euros" && mon2 == "Dollars")
{
currency = amnt / USeu;
}
else if (mon1 == "Euros" && mon2 == "Pounds")
{
currency = amnt * (USuk/USeu);
}
else if (mon1 == "Dollars" && mon2 == "Euros")
{
currency = amnt * USeu;
}
else if (mon1 == "Dollars" && mon2 == "Pounds")
{
currency = amnt * USuk;
}
else if (mon1 == "Pounds" && mon2 == "Dollars")
{
currency = amnt / USuk;
}else if (mon1 == "Pounds" && mon2 == "Euros")
{
currency = amnt / USuk * USeu;
}
}

else if (cin >> bogus1 >> amnt >> mon1 >> bogus2 >> mon2 >> mon3)
{
if (mon1 == "Euros" && mon2 == "Dollars" && mon3 == "Canadian")
{
currency = amnt * UScd/USeu;
}
else if (mon1 == "Euros" && mon2 == "Dollars" && mon3 == "US")
{
currency = amnt / USeu;
}
else if (mon1 == "Dollars" && bogus2 == "US" && mon3 == "Euros")
{
currency = amnt * USeu;
}
else if (mon1 == "Dollars" && bogus2 == "Canadian" && mon3 == "Euros")
{
currency = amnt / UScd * USeu;
}
else if (mon1 == "Dollars" && bogus2 == "US" && mon3 == "Pounds")
{
currency = amnt * USuk;
}
else if (mon1 == "Dollars" && bogus2 == "Canadian" && mon3 == "Pounds")
{
currency = amnt / UScd * USuk;
}
}

cout << "Result: " << currency << endl;

return 0;
}

/*

Command: Convert 3.50 Euros to Dollars US
Results: 3.43679

Command: Convert 11.72 Dollars US to Dollars Canadian
Results: 18.26796

/*Convert 3.50 Euros to Pounds

1.00 Dollar is 1.01838 Euros
1.00 Dollar is 0.64510 Pounds
1.00 Dollar US is 1.55870 Dollars Canadian
*/

#### Glitchny

##### Diamond Member
damn thats ugly code try cleaning it up, you shouldnt need all those if statments. just use a case statment, and have the user enter which operation they want to do (which conversion)

#### Codewiz

##### Diamond Member
Originally posted by: Glitchny
damn thats ugly code try cleaning it up, you shouldnt need all those if statments. just use a case statment, and have the user enter which operation they want to do (which conversion)

It is ugly because the forums don't play nice with white space.

##### Diamond Member
Originally posted by: Glitchny
damn thats ugly code try cleaning it up, you shouldnt need all those if statments. just use a case statment, and have the user enter which operation they want to do (which conversion)

Sorry, but I don't know what a case statement is.
Thanks again.

#### Barnaby W. Füi

##### Elite Member
switch(expression) {

case 5: // expression returned 5
dosomething();
break;

case 12345: // expression returned 12345
dosomethingelse();
break;

default: // if all else fails..
cout << "expression wasn't 5 or 12345" << endl;
break;

// not reached

}

Also.. nest your ifs; there's no sense in checking the same condition more than once if you can avoid it.

First batch of ifs could become something like:

if(mon1 == "Euros") {

if(mon2 == "Dollars")
currency = amnt / USeu;

else if(mon2 == "Pounds")
currency = amnt * (USuk/USeu);

} else if(mon1 == "Dollars") {

if(mon2 == "Euros")
currency = amnt * USeu;

else if(mon2 == "Pounds")
currency = amnt * USuk;

} else if(mon1 == "Pounds") {

if(mon2 == "Dollars")
currency = amnt / USuk;

else if(mon2 == "Euros")
currency = amnt / USuk * USeu;

}

Have you learned functions or oop or anything yet? It would be ideal to put each of the conversion calculations into its own functions, e.g. euro2pound(), dollar2euro(), etc.

#### BFG10K

##### Lifer
Having trouble getting this C++ program to run properly.
What is the problem(s) the program is exhibiting?

if (mon1 == "Euros" && mon2 == "Dollars")
Can you do that with strings?

And like the others say, you should use a switch statement to clean up your code.

#### Barnaby W. Füi

##### Elite Member
Originally posted by: BFG10K
if (mon1 == "Euros" && mon2 == "Dollars")
Can you do that with strings?

I can't tell if you mean that rhetorically, but I'm pretty sure you have to use strcmp() to compare strings in C++ just like you do in C (now that I think about it, didn't even realize it in my previous post)

#### manly

##### Lifer
Originally posted by: BFG10K

if (mon1 == "Euros" && mon2 == "Dollars")
Can you do that with strings?
I believe so w/ C++ strings.
And like the others say, you should use a switch statement to clean up your code.
Actually, I don't believe that'll work unless he reworks all his selection cases to integer expressions.

#### BFG10K

##### Lifer
I haven't done much with String classes in C++ so I was just checking. It's possible (and logical) that they've overloaded the == operator to handle a comparison but I wanted to check just to be sure.

#### BFG10K

##### Lifer
Actually, I don't believe that'll work unless he reworks all his selection cases to integer expressions.
Yes, that's what I think he should do. Instead of inputting strings he should have a menu eg:

Enter choice:

etc.

Then he can switch on the base of the integer that was entered. This'll cut down the size and complexity of the code and make it easier for the user as well.

#### alocurto

##### Platinum Member
Don't get too crazy. If you are only learning it the teacher will not like u using things u haven't learned yet.

#### Barnaby W. Füi

##### Elite Member
Originally posted by: alocurto
Don't get too crazy. If you are only learning it the teacher will not like u using things u haven't learned yet.

Is this true? If so, that kinda sucks.

#### notfred

##### Lifer
Originally posted by: alocurto
Don't get too crazy. If you are only learning it the teacher will not like u using things u haven't learned yet.

Only if he's a sh!tty teacher.

#### alocurto

##### Platinum Member
Just looking through old posts and figured i'd respond. Teachers suck. They don't like when students try to be smarter. At least in my experience. But I went to crappy Marist College in poughkeepsie. Teachers there are mostly old and not in touch with technology.

#### gregor7777

##### Platinum Member
I currently have a program handed in that uses STL lists instead of using lists with pointers and I think I am going to get fryed for that!