Hi I was trying to do a type checking in my input file while reading it. I am getting some compile time errors.
Compile time error report is :-

NEW.cpp:26: error: invalid conversion from ‘char*’ to ‘int’
NEW.cpp:26: error:   initializing argument 1 of ‘int isdigit(int)’
Line 26 is :-                if(isdigit(token))

Here is my code:-

#include<iostream>
#include<fstream>
#include<vector>
#include<string>
using namespace std;

int main() {
 
         FILE *in;
         char line[1000];
         char *token;
         in = fopen("sample.txt","rt+");
         if( in == NULL) exit(1);

     while(! feof(in)) {
         fgets(line, 1000, in);
        if (! feof(in))
        {
           int count=0;
           token = strtok(line, " \t\n");

        while (token != NULL)
        {
           if(count++==1) puts(token);
           token = strtok(NULL, " \t\n");
               if(isdigit(token))

               {

                  cout<<"Is a digit!" << endl;

               }

               else

               {

                 cout<<"Is not a digit!" << endl;

               }
        }
     }
  }
    return 0;
}

Here is my sample input file:-

1	323323	BBBBBB	CCCCC    
2	456557	EEEEEE	FFFFF
3	768588	HHHHHH	IIIII
2	097760	paste	insert
1	895564	mathew	horto

I am trying to validate whether the input file has int type in its 2nd field. If not then file format is in correct.

Can somebody help me out ?

Thanks

Recommended Answers

All 9 Replies

isdigit expects only 1 character, but you're giving it an char-array. That's because strtok returns an array of characters. See this page

Hi I have made it work now by checking with a function.
Now the only problem is that its not printing the last line of the file after checking:-

Like suppose I have 3 lines in my text file:-

123     hhkh   32132
788     hjjjl     8880
900    sbjs     080

This code below prints all lines except the last line in the file. Why is it happening ?

123     hhkh   32132
788     hjjjl     8880
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
using namespace std;
bool isnum(char *str);

int main() {
 
         FILE *in;
         char line[1000];
         char *token;
         in = fopen("textfile.txt","rt+");
         if( in == NULL) exit(1);

     while(! feof(in)) {
         fgets(line, 1000, in);
        if (! feof(in))
        {
           int count=0;
           token = strtok(line, " \t\n");

        while (token != NULL)
        {
           if(count++==1){ puts(token);
           isnum(token);}
           token = strtok(NULL, " \t\n");
        }
     }
  }
    return 0;
}

bool isnum(char *str)
{
       if(str==NULL)
            return false;
       while(*str)
       {
           if(!isdigit(*str))
            cout << "Invalid format" << endl;
            return false;
           str++;
       }
            return true;
}

Hi .. Sorry my code works :)
I cross checked and found that my file was not tab delimited and had some extra spaces..
So this is solved by me myself :)

Thanks for all your help :)

There is nothing C++ in this code apart from "using namespace std","cout","bool".

You aren't using the isnum function at all as its not used in any decision statements even when it returns true and false.

Your isnum function itself id flawed.

if(!isdigit(*str))
cout << "Invalid format" << endl;
return false;
str++;

return false is left out of the if block and would be executed irrespective of the result of the condition isdigit(*str).

Even with all this your if your code works then its definite that you wanted to go somewhere but have landed somewhere.

Ok .. Thanks for point out this. What corrections should I make then ?

thanks

There is nothing C++ in this code apart from "using namespace std","cout","bool".

You have a strange idea of what is and is not C++. The headers are all messed up, but if those are fixed to match what is actually used, it is all C++. Here are the right headers:

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cctype>

@Tom Gunn : ;)

You have a strange idea of what is and is not C++. The headers are all messed up, but if those are fixed to match what is actually used, it is all C++. Here are the right headers:

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cctype>

What you say is definitely one view of the code,I agree. But any language has its essence in its implementation rather than the headers it uses.

Op's code has declared <vector> header but no use of it has been made.It has the header <fstream> but instead of the various easy to use file handlers file pointers have been used as in C.

So at one point I felt that this has more of C rather than C++ and thats what I told OP because this is a C++ forum.

@Web_Sailor : The mistakes are clearly specified in my previous post and none of the errors are very difficult to resolve, the probem within the isnum function can be solved by :

if(!isdigit(*str))
{
cout << "Invalid format" << endl;
return false;
}

And similarly others too...

So at one point I felt that this has more of C rather than C++ and thats what I told OP because this is a C++ forum.

That is kind of like saying a woman is a little bit pregnant. The code is either C or not C. If there are any features of C++ that cause a C compiler to choke, the code is not C. Anyway, I will not send the thread any more off topic. ;)

Thanks guys :) appreciate all your help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.