Sentinel Value and If statements

Hello! I am a beginner coder and I am stuck on sentinel values. The problem is that I cannot get the code to move on to the else part of the if statement when the if statement is false. I have marked the code that is giving me the problem. Any tip on going forward would be helpful. Thank 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>
#include <string>


using namespace std;


const int USD = 0;
const int EUR = 1;
const int GBP = 2;
const int INR = 3;
const int AUD = 4;
const int CAD = 5;
const int ZAR = 6;
const int NZD = 7;
const int JPY = 8;


//Outputs a menu of currency types for user, returning a constant value
// as defined above representing the selected currency.
int getUserSelection()
{
	int selection;

	cout << "Available currencies for conversion: " << endl;

	cout << "(1) Euros" << endl;
	cout << "(2) Great Britan Pounds" << endl;
	cout << "(3) Indian Rupees" << endl;
	cout << "(4) Australian Dollars" << endl;
	cout << "(5) Canadian Dollars" << endl;
	cout << "(6) South African Rands" << endl;
	cout << "(7) New Zealand Dollars" << endl;
	cout << "(8) Japanese Zen" << endl;

	cout << "Enter the number of the desired currency to convert: ";

	cin >> selection;

	return selection;
}

//Converts the numeric type to a String representation.
//Use this to output a String representation of the type.
string convertTypeToString(int type)
{
	switch (type)
	{
	case USD:
		return "US Dollars";

	case EUR:
		return "Euros";

	case GBP:
		return "Great Britan Pounds";

	case INR:
		return "Indian Rupees";

	case AUD:
		return "Australian Dollars";

	case CAD:
		return "Canadian Dollars";

	case ZAR:
		return "South African Rands";

	case NZD:
		return "New Zealand Dollars";

	case JPY:
		return "Japanese Zen";
	}

	return "";
}


int main(int argc, char* argv[])

{
	double amount{};
	while (amount != -1)
		{
			int userSelection = getUserSelection();
			string selectionAsAString = convertTypeToString(userSelection);
			cout << "You entered " << selectionAsAString << endl;
			double usDollars{};
			cout << "Please enter an amount to be converted: (enter -1 to stop)" "\n";
			cin >> amount;
			if (-1) // This is what is giving me problems.
	        	{
				return 0;
			}
			else
			{
				cout << "Thank you. " "\n";
			}
			double conversionFactor{};
			if (userSelection == EUR)
				conversionFactor = 1.08611;
			if (userSelection == GBP)
				conversionFactor = 1.44476;
			if (userSelection == INR)
				conversionFactor = 0.01495;
			if (userSelection == AUD)
				conversionFactor = 0.69864;
			if (userSelection == CAD)
				conversionFactor = 0.70112;
			if (userSelection == ZAR)
				conversionFactor = 0.05999;
			if (userSelection == NZD)
				conversionFactor = 0.65371;
			if (userSelection == JPY)
				conversionFactor = 0.00850;
			if (userSelection == EUR)
				usDollars = amount * conversionFactor;
			if (userSelection == GBP)
				usDollars = amount * conversionFactor;
			if (userSelection == INR)
				usDollars = amount * conversionFactor;
			if (userSelection == AUD)
				usDollars = amount * conversionFactor;
			if (userSelection == CAD)
				usDollars = amount * conversionFactor;
			if (userSelection == ZAR)
				usDollars = amount * conversionFactor;
			if (userSelection == NZD)
				usDollars = amount * conversionFactor;
			if (userSelection == JPY)
				usDollars = amount * conversionFactor;
			cout << "Your converted currency is: " << usDollars << " US dollars." "\n";
			cout << "Continue? (Y/N)" "\n";
			if ('Y')
			{
				getUserSelection;
			}
			else
			{
				return 1;
			}

			cin.ignore();
			cin.get();
		
		}
}
if ('Y')
says "if the letter Y is not zero"
Y is not zero. It is always true.
you want if (something == 'Y')
whatever variable you read in for yes/no

all c++ conditions are explicit, unlike math. And the default of single value terms is whether they are zero or not.
Last edited on
L93

 
if (amount == -1)


Note that == means equality comparison and = means assignment.

Also similar at L136

You need to specify what it is being compared with.
Last edited on
Thank you so much guys, this was very helpful!
Topic archived. No new replies allowed.