Closing program when user inputs wrong data

Hi, this is my first post here so if I make some mistake feel free to let me know. I'm in the process of working through a beginners C++ book ("I've had one class in high school and one in college so far, both C++ so I know a little more than I should for where I'm at in the book) and it gave me a program and told me to modify the program so if the user inputs wrong data it will shut the program down right then. I used a simple if statement for this. I will attach my code for the program. I've tested it a few times, seems to work fine. But I wanna know if there is some issue with how I did it that I'm not seeing
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
//Converts American dollars to British pounds, Mexican pesos, or Japanese yen
//Using else/if multiple path selection structure
//July 6th 2011

#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
using std::fixed;
using std::setprecision;

int main()
{
	//declare constants and variables
	double BRITISH_RATE     = .571505;
	double MEXICAN_RATE     = 10.7956;
	double JAPANESE_YEN     = 112.212;
	int choice              = 0;
	double americanDollars   = 0.0;
	double convertedDollars = 0.0;

	//get input items
	cout << "1 British pounds" << endl;
	cout << "2 Mexican pesos" << endl;
	cout << "3 Japanese yen" << endl;
	cout << "Enter 1, 2, or 3: ";
	cin >> choice;

	//MY VALIDATION
	//Close program if user inputs wrong data
	if (choice < 1 || choice > 3)
	{
		cout << "ERROR: Invalid data" << endl;
		return 0;
	}
	//END OF MY VALDATION

	cout << "Enter number of American dollars: ";
	cin >> americanDollars;

	//display output in fixed-point notation with two decimal places
	cout << fixed << setprecision(2) << endl;

	//Conversions
	if (choice == 1)
	{
		convertedDollars = americanDollars * BRITISH_RATE;
		cout << "British pounds: " << convertedDollars << endl;
	}
	else if (choice == 2)
	{
		convertedDollars = americanDollars * MEXICAN_RATE;
		cout << "Mexican pesos: " << convertedDollars << endl;
	}
	else
	{
		convertedDollars = americanDollars * JAPANESE_YEN;
		cout << "Japanese yen: " << convertedDollars << endl;
	}	//end ifs
	return 0;
}	//end of main function 
closed account (GbX36Up4)
"I've tested it a few times, seems to work fine. But I wanna know if there is some issue with how I did it that I'm not seeing" You said it works fine so why would there be an issue?

**EDIT**

1
2
3
4
5
if (choice < 1 || choice > 3)
	{
		cout << "ERROR: Invalid data" << endl;
		return 0;
	}


choice cant be less than one AND greater than three, that means it is two different numbers, try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
if (choice < 1){
cout << endl << "ERROR: Invalid data" << endl; // you didn't put an endl anywhere between 
// cin choice and the ERROR, thus they would be on the same line, AND you didnt put an endl
// between line 28 and 29
return 0;
}

if (choice > 3){
cout << endl << "ERROR: Invalid data" << endl;
return 0;
}


Last edited on
@logart:
|| is the OR (inclusive OR) operator (the AND operator is &&). The OP's code is right. There is no need to repeat the same code with 2 seperate if statements.

EDIT: operators and how they work: http://cplusplus.com/doc/boolean/
Last edited on
Thanks, I just didn't know if having two return statements in there could cause some odd issue
closed account (GbX36Up4)
Oh yea, stupid me i just used && for my allegro code :3
Topic archived. No new replies allowed.