read modify write

Hello,
I am just beginning to learn how to program. I am trying to write a program that reads in a file and then uses several functions ( that I am trying to write as well) to analyze the characters in the input file. Then I want to write the results to an output file, labeling the contents of the file as; a number, a letter, or a special character ect. Here's what I have so far. I am having trouble calling my functions and getting them to discriminate between letters and numbers.


#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

// Prototypes
int IsDigit (int c);
int IsOdd ( int n );
//int IsEven ( char a );
int main()
{
ifstream fIn;
fIn.open ( "stuff.in", ios::in );
if( !fIn )
{
cerr << "Can't open input file." << endl;
exit (-1);
}

ofstream fOut;
fOut.open ( "stuff.out", ios::out );
if ( !fOut)
{
cerr << "Can't open output file." << endl;
exit(-1);
}


char ch;

while (fIn >> ch)
{
if ( IsDigit(ch))
{
fOut << ch << " Digit";
}
if ( IsOdd(ch))
{
fOut << "Odd";
}


fOut << endl;

}



fIn.close();
fOut.close();


return 0;
}
// Defining function prototype
int IsDigit ( int c)
{
int iRetVal = '0';
if( '0' <= c && c <= '9')

iRetVal = '1';
return iRetVal;
}

int IsOdd ( int n )
{
int iRetVal = '0';
if ( n % '2' )
{
iRetVal = '1';
}
return iRetVal;
}
Last edited on
The value '2' is the character code for the picture of a 2. If you are using an ISO 8859 subset (like ASCII) the value is the number 50.

The value 2 is just the number 2.

Unrelated:
main() should return positive (non-zero) exit codes for errors, and zero for success.

If you #include <cctype> you get a predefined isdigit() function so you don't have to write your own.

Hope this helps.

PS. Please use [code] tags.
Last edited on
Topic archived. No new replies allowed.