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 ?
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
unsignedshort 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;
}
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.