• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

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

abracadabra1

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
*/
 
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)
 
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.
 
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.
 
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.
 
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.
 
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)
 
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.
 
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:

1. U.S to Canada.
2. Canada to U.S.
3. Canada to Euro.

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.
 
Don't get too crazy. If you are only learning it the teacher will not like u using things u haven't learned yet.
 
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.
 
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! 🙂
 
Back
Top