This program works fine but I'm wondering if the value in the switch statement in question

Is the value in the switch statement is correctly placed? The program works fine and any changes to the value in the switch statement results in run-time errors.

#include <iostream>

using namespace std;

int main()
{
cout << "This is a Dollar, Peruvian Sol, and YEN to Euro conversion calculator, please select which currency (Dollars, Sols, or Yen) you wish to convert to Euros with the corresponding keys: d, s, or y " << endl;

const double d2euro {.88}; // established dollar to euro conversion
const double s2euro {.23}; // established sol to euro conversion
const double y2euro {.0076}; // established yen to euro conversion

char currencyinputd = 'd'; // our currency selection
char currencyinputs = 's';
char currencyinputy = 'y';

char currencyinput = 'a' ;
double dollarinput {}; //amount entered based on currency
double solinput {};
double yeninput {};

double converteurototald {}; // amount to be calculated after conversion equation
double converteurototals {};
double converteurototaly {};
cin >> currencyinputd || currencyinputs || currencyinputy;
//need to make some if statements for different keys pressed (you have to do cases)
switch (currencyinputd){
case 'd':
cout << "Enter in Dollar amount " << endl;
cin >> dollarinput;

converteurototald = d2euro * dollarinput;
cout << "Total amount of Euros: " << converteurototald << endl;
break;

case 's':
cout << "Enter in Sol amount " << endl;
cin >> solinput;
converteurototals = s2euro * solinput;

cout << "Total amount of Euros: " << converteurototals << endl;
break;
case 'y':
cout << "Enter in Yen amount " << endl;
cin >> yeninput;
converteurototaly = y2euro * yeninput;

cout << "Total amount of Euros: " << converteurototaly << endl;
break;
default:
cout << "That is not a valid key" << endl;
}
/*if (dollarinput >= 0)

cout << "Total amount of Euros: " << converteurototal << endl;

else
cout << "That is not a valid dollar" << endl;**/
return 0;
}
what is it you want to do that gives errors?
Its unclear what exact thing you are saying you changed that broke it.

note that switches fall through, so multiple cases can be stacked. That is *really* handy for this program!

case 's': //no break, keeps going
cass 'S':
//code for s will happen for both upper and lower case.

not sure if that is something you wanted -- case being a thing in both switches and capital letters... but just in case you wanted to see it
This
 
cin >> currencyinputd || currencyinputs || currencyinputy;
is the same as this
 
cin >> currencyinputd;


It reads a char from the user and stores it in the variable currencyinputd (without caring what value it had before).
Last edited on
Hi Jonnin! thanks for your response! Perhaps I don't fully understand switches. I'm just unsure about making in value in the switch (currencyinputd), should the value be based on an abstract value char like for example currencyinput char = 'a'; instead of one of the inputs 'd'? because I have 3 different inputs. I hope I make sense haha.
Hi Peter87, thanks for the response as well! So I can just eliminate cin >> currencyinputd || currencyinputs || currencyinputy; and simplify it for cin >> currencyinputd;?
Yes, but I think there's still a gap in understanding here.

You never use your 'currencyinputs', ' currencyinputy', or 'currencyinput' variables for anything. They serve no purpose in your program as it is right now.

You don't need a different variable for each type of input/money. I can show a simplified version of your program that factors out the differences in each branch.
Last edited on
switch (currencyinputd)
the variable being switched off of should be an integer. it only works off ONE value. If you need to switch off 3 values, you need 3 switches or some other idea. you can use an if statement in a switch, or you can use the same switch in a function and send it multiple variables.

you can also 'combine' multiple values into one integer. That depends on the data and can get advanced in a hurry, lets skip it for now.

here is my take on it:
switches can be powerful due to their fall-through capability and the fact that the compiler tends to implement them as lookups instead of branches. So they can be very efficient and elegant for specific problems. If you are not getting either performance or elegance from the switch, it may be better to simply use if/else statements.

all that said, here is a tutorial:https://www.cplusplus.com/doc/tutorial/control/ (find the section on switches)
or, I will give you this:
a switch is checking the state of a single variable.
that variable will be in one of a few states that you define (eg a character that is either 'a', 'm', or 'x') or a state you did not define (provide a default behavior).
that is it. When it is in one of the above states (defined or default), you do specific things (your code block).
it is exactly like an if statement,
if (input == 'm')
{
do your special m stuff
}
else if (input == 'x')
{
do your special x stuff
}
...
else
{
do your default stuff.
}

again, the difference is the fall-through.
in a switch:
case 'm':
cout << "m";
case 'x':
cout << "X";
break;
...
this will print what? it prints M, then X, if you enter M, but only X if you entered X.
that is harder to express in if/else. Not unpossible, just not as simple:
if(input == 'm')
{
cout << "m"; //this represents repeating code, though. you would need
//a block of the same code in the M condition and the same block in the X condition
//for a more complex example, and repeating code is 'bad'.
cout << "x";
}
else (if input == 'x')
cout << "x";


Last edited on
Thanks Ganado, you are right. I would love to see a simpler version from you!
Wow thanks Jonnin for the information, it really helps! I appreciate your time!
If you are willing to give up the specific currency words it gets much simpler:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

int main()
{
cout << "This is a Dollar, Peruvian Sol, and YEN to Euro conversion calculator, please select which currency (Dollars, Sols, or Yen) you wish to convert to Euros with the corresponding keys: d, s, or y " << endl;
const double d2euro {.88}; // established dollar to euro conversion
const double s2euro {.23}; // established sol to euro conversion
const double y2euro {.0076}; // established yen to euro conversion
char currencyinput{};
double valuein{};
double valueout{};
cin >> currencyinput;
cout << "Enter the amount for the selected currency\n";
cin >> valuein;
switch(currencyinput)
{
  case 'd':
    valueout = valuein*d2euro; break; 
  case 'y':
	valueout = valuein*y2euro; break;
  case 's':	
	valueout = valuein*s2euro; break;
}

cout << "Total amount of Euros: " << valueout << endl;
}
I basically had the same thing as jonnin, but also factored out the specific currency words:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>

using namespace std;

int main()
{
	const double dollar2euro {0.88}; 
	const double sol2euro {0.23};
	const double yen2euro {0.0076};
	
	cout << "Enter currency type (d/s/y): ";
	char input_currency_type;
	cin >> input_currency_type;
	
	string currency_name;
	double exchange_rate;

	switch (input_currency_type){
	  case 'd':
		currency_name = "Dollar";
		exchange_rate = dollar2euro;
		break;
		
	  case 's':
		currency_name = "Sol";
		exchange_rate = sol2euro;
		break;

	  case 'y':
		currency_name = "Yen";
		exchange_rate = yen2euro;
		break;
		
	  default:
	    cout << "That is not a valid key" << endl;
	    return 1;
	}
	
	cout << "Enter in " << currency_name << " amount " << endl;
	double input_amount;
	cin >> input_amount;
		
	double euros = exchange_rate * input_amount;
	cout << "Total amount of Euros: " << euros << endl;

	return 0;
}
Last edited on
Thanks once again Jonnin and Ganado for all your help!
Maybe functions and while's are a bit ahead of your course ... but not too far off
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>

using namespace std;

void convert(string name, double rate)
{
    double amount_for_conversion{0};
    cout << "Enter amount to be converted: ";
    cin >> amount_for_conversion;
    
    cout
    << amount_for_conversion << ' '
    << name << " converts to "
    << amount_for_conversion * rate << " Euros\n\n";
}

int main()
{
    cout
    << "This is a Dollar, Peruvian Sol, and YEN to Euro conversion\n"
    << "calculator please select which currency (Dollars, Sols, or Yen) you\n"
    << "wish to convert to Euros with the corresponding keys: d, s, or y\n\n";
    
    char selection{'?'};
    
    while(
          cout << "Enter currency to be converted d,s or y (x to exit): "
          and cin >> selection
          )
        
    {
        switch (toupper(selection))
        {
            case 'D':
                convert("Dollars", 0.88);
                break;
                
            case 'S':
                convert("Peruvian Sol", 0.23);
                break;
                
            case 'Y':
                convert("YEN", 0.0076);
                break;
                
            case 'X':
                return 0;
                
            default:
                cout << "** That is not a valid key. Try again\n";
                break;
        }
    }
    
    return 0;
}


This is a Dollar, Peruvian Sol, and YEN to Euro conversion
calculator please select which currency (Dollars, Sols, or Yen) you
wish to convert to Euros with the corresponding keys: d, s, or y

Enter currency to be converted d,s or y (x to exit): g
** That is not a valid key. Try again
Enter currency to be converted d,s or y (x to exit): y
Enter amount to be converted: 678
678 YEN converts to 5.1528 Euros

Enter currency to be converted d,s or y (x to exit): s
Enter amount to be converted: 888
888 Peruvian Sol converts to 204.24 Euros

Enter currency to be converted d,s or y (x to exit): x
Program ended with exit code: 0
Thanks Againtry, that is an interesting way of using switch statements with loops!
Last edited on
Topic archived. No new replies allowed.