Home > Net >  simple currency converter using switch c
simple currency converter using switch c

Time:10-08

Hello I need help with this simple calculator using switch when I type in the currency I want to convert into and then the amount after that nothing happens .Ive tried using if but i cant get it to work

#include <iostream>
using namespace std;

int main() {
  char zlote;
  char E, D, F;
  char euro;
  char dolar;
  char funt;
  char currency;

  E=euro*0.25;
  D=dolar*0.19;
  F=funt*0.22;

  cout << "what currency do you want to convert into  ?" << endl;
  cin >> currency;

  cout<<"Type in the amount you wish to convert"<<endl;
  cin >>zlote;

  switch (zlote) {
    
     {case 1 :
       cout<<zlote<<" zlote is equal "<<E<<" Euro"<<endl;
       break;}

      {case 2 :
      cout<< zlote<<" zlote is equal "<<D<<" Dolarow"<<endl;
      break;}

      {case 3 :
      cout<< zlote<<" zlote is equal "<<F<<" Funtow"<<endl;
      break;}
  }
      return 0;
}

CodePudding user response:

Try using case 'E' instead of case 1
using case 'D' instead of case 2
using case 'F' instead of case 3
Also when you take input from the user make sure You enter the letters in uppercase

CodePudding user response:

switch (zlote) {

should be switching on the currency choice, not the amount, right? That's why nothing happens.

But you have other problems: you seem to be computing stuff before you have the values input!

I would suggest following one important code-review issue in particular, to prevent that issue: Don't declare your variables at the top in one clump; declare them when first used and ready to be given an initial value.

The types don't make any sense either. Why are you multiplying characters by floating-point numbers? READ THE WARNINGS; they actually are telling you things.

You switch won't work anyway, even if you used the right variable, because you read a char but you are switching on a numeric value, so that will be the character code: '1' is not the same as 1 (it's actually 49 if memory serves).

CodePudding user response:

You need to do the calculations after catch the information becasuse if you not do befor the variable are empty. The type of variable are wrong because you are operating with numbers with decimals.

float zlote;
float E, D, F;  
int currency;
cout << "what currency do you want to convert into  ?" << endl;
cin >> currency;

cout<<"Type in the amount you wish to convert"<<endl;
cin >>zlote;

   E=zlote*0.25;
   D=zlote*0.19;
   F=zlote*0.22;

   switch (currency)

   {

       {case 1 :
         cout<<zlote<<" zlote is equal "<<E<<" Euro"<<endl;
         break;}

        {case 2 :
        cout<< zlote<<" zlote is equal "<<D<<" Dolarow"<<endl;
        break;}

        {case 3 :
        cout<< zlote<<" zlote is equal "<<F<<" Funtow"<<endl;
        break;}


   }
  • Related