Issue with conditions...

I have occured an issue. I need the following program to check if the result is integer, and exactly 1,2,3,4,5,6,7,8,9,10. I want it to return an error statement if I give 1.7 or 9.2 or whatever. Any ideas ?

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
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	unsigned short n, result;
	
	ofstream Scrie("Case.rtf");
	
	cout << "The result is (Please give an integer between 1 and 10):" << " ";
	cin >> n;
	
	if (n==1.0 || n==2.0 || n==3.0 || n==4.0 || n==5.0 || n==6.0 || n==7.0 || n==8.0 || n==9.0 || n==10.0)
	{
		if (!Scrie.is_open())
		{
			cout << "\n" << "Fatal: File could not be opened.";
			return (1);
		}
		else
		{
			cout << "\n" << "Check: File opened successfully.";
			
			switch(n)
			{
			case 10: Scrie << "That's an A.";
				break;
			case 9:  Scrie << "That's almost an A.";
				break;
			case 8:  Scrie << "That's a B.";
				break;
			case 7:  Scrie << "That's almost a B.";
				break;
			case 6:  Scrie << "That's a C.";
				break;
			default:   Scrie << "You didn't pass the exam.";
				break;
			}
		}
	}
	else
	{
		cout << "Fatal: You entered a non-integer number.";
		Scrie << "Fatal: An error occured.";
		return (1);
	}
	

	Scrie.close();
	return 0;
}


Thanks in advance.
If the input buffer (the location where the extracted data is placed) is an integral type, the fractional counterpart is truncated for compatibility. So, there's no need to check for such values.

Assuming std::ofstream behaves in the same way as std::istream, of course.

Wazzak
Thanks :)
Topic archived. No new replies allowed.