Bool statement not working, PLEASE HELP!! PROJECT DUE 8/5/15

Hey guys i need help on my project. So everything runs smoothly until the user inputs a different answer other than the ones asked for (the choices are a-i). So if the user puts a different answer its suppose to runt he program again. I do not know what I am doing wrong.

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <iostream>
#include <string>
#include <cctype >
using namespace std;

const float RUSSIAN_RUBLE = 31.16840f;
const float NORTH_KOREAN_WON = 135.00f;
const float CHINESE_YUAN = 6.83200f;
const float CANADIAN_DOLLAR = 1.1137f;
const float CUBAN_PESO = 1.00f;
const float ETHIOPIAN_BIRR = 9.09f;
const float EGYPTIAN_POUND = 5.6275f;
const float TUNISIAN_DINAR = 1.3585f;
const float THAI_BAHT = 34.40f;


float getDollarAmt();
void displayCurrencies();
char getCurrencySelection();
float calcExchangeAmt(char letter, float dollar);
void displayResults(float dollar, float sum, char letter);
bool isSelectionValid(char letter);

int main()
{
	float dollar;
	char letter;
	float sum;
	char answer = 'Y';
	while (toupper(answer) == 'Y')
	{
		dollar = getDollarAmt();
		displayCurrencies();
		letter = getCurrencySelection();
		calcExchangeAmt(letter, dollar);
		sum = calcExchangeAmt(letter, dollar);
		displayResults(dollar, sum, letter);

		cout << "\nDo you wish to continue (Y for yes - N for no): ";
		cin >> answer;
		system("cls");
	}
}
float getDollarAmt()
{
	float dollar;
	cout << "Please enter the total dollar amount to exchange: $";
	cin >> dollar;
	return dollar;
}
void displayCurrencies()
{
	cout << "Please select a target currenc y:" << endl << endl
		<< "A Russian Ruble" << endl
		<< "B North Korean Won" << endl
		<< "C Chinese Yuan" << endl
		<< "D Cuban Peso" << endl
		<< "E Ethiopian Birr" << endl
		<< "F Thai Baht" << endl
		<< "G Canadian Dollars" << endl
		<< "H Tunisian Dinar" << endl
		<< "I Egyptian Pound" << endl << endl;
}
char getCurrencySelection()
{
	char letter;
	bool letterValid = false;

	while (letterValid == false)
	{
		cout << "Please enter your selection: ";
		cin >> letter;

		letterValid = isSelectionValid(letter);
	}
	return letter;
}
bool isSelectionValid(char letter)
{
	if ((toupper(letter) >= 'A') && (toupper(letter) <= ' I'))
	{
		return true;
	}
	else
	{
		return false;
	}
}
float calcExchangeAmt(char letter, float dollar)
{
	float sum;
	switch (toupper(letter))
	{
	case 'A': sum = RUSSIAN_RUBLE * dollar;
		break;
	case 'B': sum = NORTH_KOREAN_WON * dollar;
		break;
	case 'C': sum = CHINESE_YUAN * dollar;
		break;
	case 'D': sum = CUBAN_PESO * dollar;
		break;
	case 'E': sum = ETHIOPIAN_BIRR * dollar;
		break;
	case 'F': sum = THAI_BAHT * dollar;
		break;
	case 'G': sum = CANADIAN_DOLLAR * dollar;
		break;
	case 'H': sum = TUNISIAN_DINAR * dollar;
		break;
	case ' I': sum = EGYPTIAN_POUND * dollar;
		break;
	}
	return sum;
}
void displayResults(float dollar, float sum, char letter)
{
	string word;
	string word2;
	string word3;
	switch (toupper(letter))
	{
	case 'A': word = "Russian";
		word2 = "Ruble";
		break;
	case 'B': word = "North";
		word2 = "Korean";
		word3 = "Won";
		break;
	case 'C': word = "Chinese";
		word2 = "Yuan";
		break;
	case 'D': word = "Cuban";
		word2 = "Peso";
		break;
	case 'E': word = "Ethiopian";
		word2 = "Birr";
		break;
	case 'F': word = " Thai";
		word2 = "Baht";
		break;
	case 'G': word = "Canadian";
		word2 = "Dollars";
		break;
	case 'H': word = "Tunisian";
		word2 = "Dinar";
		break;
	case ' I': word = "Egyptian";
		word2 = "Pound";
		break;
	}
	cout << "$" << dollar << " is " << sum << " " << word << " "
		<< word2 << " " << word3;
} // end of displayResults() 
Last edited on
can you repost using the [ code ] [ /code ] tag?
Sorry I am new to this, what exactly or how?
but anyway, try adding:
1
2
3
4
5
case ' I': sum = EGYPTIAN_POUND * dollar;
break;
default:
    cout << "Please enter your selection: ";
    cin >> letter;
put your code inside the code tags

for example:
[ code ]
//your code here
[ /code ]

remove the spaces in the tags
so it will become like this:
 
//your code here 
Okay thanks got it, I posted it again
I'm new to c++ as well but I don't think you can use >= and <= on char.
Or you're not doing it correctly.
I'm new to c++ as well but I don't think you can use >= and <= on char.

Yes you can, char is converted to ascii int and compared.

if ((toupper(letter) >= 'A') && (toupper(letter) <= ' I'))

Notice the space before the I, this is messing things up.

It is still probably useful to have a default: case for your switch statement where you set sum equal to 0 and print out some sort of error, just in case.
Removing the boolean gives you:

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>
#include <string>
#include <cctype >
using namespace std;

const float RUSSIAN_RUBLE = 31.16840f;
const float NORTH_KOREAN_WON = 135.00f;
const float CHINESE_YUAN = 6.83200f;
const float CANADIAN_DOLLAR = 1.1137f;
const float CUBAN_PESO = 1.00f;
const float ETHIOPIAN_BIRR = 9.09f;
const float EGYPTIAN_POUND = 5.6275f;
const float TUNISIAN_DINAR = 1.3585f;
const float THAI_BAHT = 34.40f;


float getDollarAmt();
void displayCurrencies();
char getCurrencySelection();
float calcExchangeAmt(char letter, float dollar);
void displayResults(float dollar, float sum, char letter);
bool isSelectionValid(char letter);

int main()
{
	float dollar;
	char letter;
	float sum;
	char answer = 'Y';
	while (toupper(answer) == 'Y')
	{
		dollar = getDollarAmt();
		displayCurrencies();
		cin>>letter;
		calcExchangeAmt(letter, dollar);
		sum = calcExchangeAmt(letter, dollar);
		displayResults(dollar, sum, letter);

		cout << "\nDo you wish to continue (Y for yes - N for no): ";
		cin >> answer;
	}
}
float getDollarAmt()
{
	float dollar;
	cout << "Please enter the total dollar amount to exchange: $";
	cin >> dollar;
	return dollar;
}
void displayCurrencies()
{
	cout << "Please select a target currenc y:" << endl << endl
		<< "A Russian Ruble" << endl
		<< "B North Korean Won" << endl
		<< "C Chinese Yuan" << endl
		<< "D Cuban Peso" << endl
		<< "E Ethiopian Birr" << endl
		<< "F Thai Baht" << endl
		<< "G Canadian Dollars" << endl
		<< "H Tunisian Dinar" << endl
		<< "I Egyptian Pound" << endl << endl;
}

float calcExchangeAmt(char letter, float dollar)
{
	float sum;
	switch (toupper(letter))
	{
	case 'A': sum = RUSSIAN_RUBLE * dollar;
		break;
	case 'B': sum = NORTH_KOREAN_WON * dollar;
		break;
	case 'C': sum = CHINESE_YUAN * dollar;
		break;
	case 'D': sum = CUBAN_PESO * dollar;
		break;
	case 'E': sum = ETHIOPIAN_BIRR * dollar;
		break;
	case 'F': sum = THAI_BAHT * dollar;
		break;
	case 'G': sum = CANADIAN_DOLLAR * dollar;
		break;
	case 'H': sum = TUNISIAN_DINAR * dollar;
		break;
	case ' I': sum = EGYPTIAN_POUND * dollar;
		break;
    default:
        cout << "Please enter your selection: ";
        cin >> letter;
	}
	return sum;
}
void displayResults(float dollar, float sum, char letter)
{
	string word;
	string word2;
	string word3;
	switch (toupper(letter))
	{
	case 'A': word = "Russian";
		word2 = "Ruble";
		break;
	case 'B': word = "North";
		word2 = "Korean";
		word3 = "Won";
		break;
	case 'C': word = "Chinese";
		word2 = "Yuan";
		break;
	case 'D': word = "Cuban";
		word2 = "Peso";
		break;
	case 'E': word = "Ethiopian";
		word2 = "Birr";
		break;
	case 'F': word = " Thai";
		word2 = "Baht";
		break;
	case 'G': word = "Canadian";
		word2 = "Dollars";
		break;
	case 'H': word = "Tunisian";
		word2 = "Dinar";
		break;
	case ' I': word = "Egyptian";
		word2 = "Pound";
		break;
	}
	cout << "$" << dollar << " is " << sum << " " << word << " "
		<< word2 << " " << word3;
} // end of displayResults() 


Will prompt the user to reenter if the entered value is invalid.
But after 3 times of invalid inputs the program will return nan(not a number).
Can anyone fix this?
But then again, like what james2250 said, removing the space before the I from:

if ((toupper(letter) >= 'A') && (toupper(letter) <= ' I'))

and
case ' I': word = "Egyptian";


fixes things.
Last edited on
Yeah the space bar was the problem.. THANKS SOO MUCH!!!
Last edited on
Topic archived. No new replies allowed.