photon
photon

Reputation: 616

Checking function parameter data type in C++

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;
        }
    }
}

Upvotes: 0

Views: 1712

Answers (2)

C. M.
C. M.

Reputation: 100

You declared the parameter x to be an int so it's an int

void CLASS::READWRITE(int x)

When you pass in a double or a char, the compiler implicitly casts them to an integer value. For doubles this means truncating after the decimal and for chars, the compiler treats the ascii representation as an integer.

Assuming, as others here have, that the parameters x are coming from user/file input, you have to check each input string manually to be sure that it's a valid integer. If the input is binary then there's really no way to tell since data is context sensitive.

Upvotes: 1

Refugnic Eternium
Refugnic Eternium

Reputation: 4291

This isn't exactly what you're looking for, but if you want to make sure that you're only transferring integers, you'll have to check the strings. And while you specifically asked for C++, I'm just more comfortable with C file handling, so please bear with me.

FILE *fp = fopen("input.file", "rb"), op = fopen("output.file", "wb");
if(fp && op) {
     while(!feof(fp)) {
         char buffer[20], *ptr = buffer;
         fscanf(fp, "%20[^\s\n]", buffer);
         while(ptr && *ptr != 0) {
             if(*ptr >= '0' && *ptr <= '9') ++ptr;
             else ptr = 0;
         }
         if(ptr) fprintf(op, "%s", buffer);
     }
     fclose(fp);
     fclose(op);
}

In short, this code scans the entire file for blocks, that are delimited by spaces or linefeeds (Assuming that any one number is not greater than 20 characters.) In the next step, it detects, whether the character in question is a number (in which case the pointer is moved up one step), or not. In the latter case, the string is invalid as integer and discarded (the pointer is set to 0, which in turn voids the 'write' after the loop.

I believe this should do what you are looking for.

Upvotes: 2

Related Questions