This class function CLASS::READWRITE is supposed to read data from an input file and write to an output file only if its input value is of type int. When I enter a floating point value is passed as x, the decimal places are truncated and the function executes normally instead of outputting a warning. If a lowercase letter like 'b' is passed as x, the value is apparently read as "0" so the function executes normally instead of outputting a warning.
How can I enforce the check (x != int)? I've seen suggestions for using cin.fail(), but I am not using cin here.
#include <iostream>
#include <fstream>
using namespace std;
ifstream input_file("in_file", ios::in);
ofstream output_file("out_file", ios::out);
int main(void)
{
CLASS object(n); // n is the number of values held in in_file
object.READWRITE(x); // x is some test value
}
void CLASS::READWRITE(int x)
{
int i;
for(i = 0; i < n; i++)
{
input_file >> number[i];
}
for(i = 0; i < n; i++)
{
if((x != int) || (x < 0))
{
out_file << "Error" << endl;
break;
}
else
{
out_file << num[i] << endl;
}
}
}
typeoffunction. While it's not in the C standard, it's still widely used. Check this link stackoverflow.com/questions/12081502/typeof-operator-in-c. Also, regarding your other problem...you're suffering from 'implicitely conversion'. Most simple data types (char, int, float, etc.) can be implicitely converted to each other. You'll lose precision, but that's. If you REALLY want to know what the stuff in your file is, you need to check the string, not the number.