0

I've been attempting to create a working program that converts other forms of global currencies (such as yen, kroner and pound) to US dollars. I've tried setting up the currency values (converted to US dollar), with reference to the economic exchange rates on Google. The program makes use of constexprs to initialize the numerical values that correspond to the different currencies, as well as chars to represent the different currencies for conversion using a switch statement. However, I haven't been able to get it to work as intended.

Upon runtime, after the project build is compiled, any values automatically refer to the "default:" segment of the switch statement. Any help as to how I would be able to get this to work correctly is appreciated.

My includes come from the standard C++ library headers, whereby the main header includes: #include iostream #include fstream #include sstream #include cmath #include cstdlib #include string #include list #include vector #include algorithm #include stdexcept

Here is my code:

    int main()

    {

    constexpr double yuan_to_dollar = 0.15; // conversion to USD -- values cannot be modified at runtime

constexpr double kroner_to_dollar = 0.15;

constexpr double pound_to_dollar = 1.42;

char currency;
char yuan = 'a';
char kroner = 'b';
char pound = 'c';

double amount = 1;

cout << "Please enter an integer amount in currency: \n";

cin >> currency >> amount >> yuan >> kroner >> pound; // inputs currency double values

switch (currency) {

case 'a':
    cout << yuan << "is == " << yuan_to_dollar * 'a' * amount << "currency \n";

case 'b':
    cout << kroner << "is == " << kroner_to_dollar * 'b' * amount << "currency \n";

case 'c':
    cout << pound << "is == " << pound_to_dollar * 'c' * amount << "currency \n";

default:
    cout << "Sorry, I could not determine a suitable form of: " << currency << "currency \n";
}

return 0;

}

2 Answers 2

1

The correct form for switch/case is:

switch(currency) {
    case 'a':
        // code in case of a here
        break;
    case 'b':
        // code for b here
        break;
    default:
        // default case
}

otherwise you just fall through all the statements.

Also don't multiply by 'a', 'a' has an integer value of 97, so you're multiplying by 0.15 * 97 in case of a.

Your input also doesn't seem to be what you're looking for.

The way you're written it: cin >> currency >> amount >> yuan >> kroner >> pound;

will take inputs of character (currency), amount (double), and three more chars (yuan, kroner, pound). You're overwriting the chars by doing that.

0
0
#include <iostream>
#include<string.h>
#include <ctime>
using namespace std;
int main()
{
                                                                                                 
double amount,amount2;
string cur2,cur;


cout<<"Which currency do you want to convert\n";

cout<<"the currency are:USD, EURO, PKR, INR, AED: ";

cin>>cur;
  
cout<<"Enter your amount you want to convert: ";
  
cin>>amount;

cout<<"Which currency do you want to convert to\n";

cout<<"The currency are:USD, EURO, PKR, INR, AED: ";

cin>>cur2;

  if(cur=="usd")
{

    
  if(cur2=="euro"){
  amount2=amount*.85;
  cout<<amount<<" USD is equal to "<<amount2<<" EURO";
  }
  if(cur2=="pkr"){
  amount2=amount*170.40;
  cout<<amount<<" USD is equal to "<<amount2<<" PKR";
  }
if(cur2=="aed"){
  amount2=amount*3.67;
  cout<<amount<<" USD is equal to "<<amount2<<" AED";
  }
  if(cur2=="inr"){
  amount2=amount*73.86;
  cout<<amount<<" USD is equal to "<<amount2<<" INR";
  }
}
  if(cur=="euro"){


    
  if(cur2=="usd"){
  amount2=amount*1.17;
  cout<<amount<<" EURO is equal to "<<amount2<<" USD";
}
  if(cur2=="pkr"){
  amount2=amount*197.94;
  cout<<amount<<" EURO is equal to "<<amount2<<" PKR";
  }
if(cur2=="aed"){
  amount2=amount*4.30;
  cout<<amount<<" EURO is equal to "<<amount2<<" AED";
  }
  if(cur2=="inr"){
  amount2=amount*86.43;
  cout<<amount<<" EURO is equal to "<<amount2<<" INR";
  }
}
  if(cur=="aed"){


    
  if(cur2=="usd"){
  amount2=amount*.27;
  cout<<amount<<" AED is equal to "<<amount2<<" USD";
}
  if(cur2=="pkr"){
  amount2=amount*46.04;
  cout<<amount<<" AED is equal to "<<amount2<<" PKR";
  }
if(cur2=="euro"){
  amount2=amount*.23;
  cout<<amount<<" AED is equal to "<<amount2<<" EURO";
  }
  if(cur2=="inr"){
  amount2=amount*20.10;
  cout<<amount<<" AED is equal to "<<amount2<<" INR";
  }
}
  if(cur=="pkr"){

    
  if(cur2=="usd"){
  amount2=amount*.0059;
  cout<<amount<<" PKR is equal to "<<amount2<<" USD";
}
  if(cur2=="aed"){
  amount2=amount*.022;
  cout<<amount<<" PKR is equal to "<<amount2<<" AED";
  }
if(cur2=="euro"){
  amount2=amount*.0051;
  cout<<amount<<" PKR is equal to "<<amount2<<" EURO";
  }
  if(cur2=="inr"){
  amount2=amount*.44;
  cout<<amount<<" PKR is equal to "<<amount2<<" INR";
  }   
}
  if(cur=="inr")
{

    
  if(cur2=="usd"){
  amount2=amount*.014;
  cout<<amount<<" INR is equal to "<<amount2<<" USD";
}
  if(cur2=="aed"){
  amount2=amount*.050;
  cout<<amount<<" INR is equal to "<<amount2<<" AED";
  }
if(cur2=="euro"){
  amount2=amount*.012;
  cout<<amount<<" INR is equal to "<<amount2<<" EURO";
  }
  if(cur2=="pkr"){
  amount2=amount*2.30;
  cout<<amount<<" INR is equal to "<<amount2<<" PKR";
  } }

}

2
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Sep 28, 2021 at 15:32
  • The question is specifically about using switch statements; I don't see any in your code, or an explanation about why you have not used any. Sep 28, 2021 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.