Encoding Decoding program

My assignment is to make a program that uses fstream functions to gather integers from one file, isolate the significant digits from each integer, cast them as characters, and output them to another file ( we haven't gotten around to arrays yet and aren't allowed to use them.) This is what I have so far, and though it compiles, the program doesn't end. I think I'm stuck in a loop somewhere and I'm not sure how or where. Help me out, please?

// Header Files

#include <fstream>
#include <iostream>

using namespace std;

// Global Constants

const char SPACE = ' ';
const char END_OF_LINE = '\n';

// Function Prototypes

/*
Name: inputBothFileNames
Process: prompt user for input and output file names,
acquire, pass back to calling function
Function Input/Parameters: none
Function Output/Parameters: two file names (string)
Function Output/Return: none
Device Input: keyboard
Device Output: monitor
Dependencies: iostream
*/
void inputBothFileNames ( string &inFileName, string &outFileName );

/*
Name: processData
Process: acquire integer data from one file, decode it,
and output as text in another file
Function Input/Parameters: two file names (string)
Function Output/Parameters: none
Function Output/Return: none
Device Input: harddrive, integer data
Device Output: harddrive, text data
Dependencies: fstream, getNextEvenValue
*/

void processData ( const string &inFileName, const string &outFileName);

/*
Name: showErrorMessage
Process: displays error message to user, showing
that input file name does not work
Function Input/Parameters: input file name (string)
Function Output/Parameters: none
Function Output/Return: none
Device Input: none
Device Output: monitor, error message
Dependencies: iostream
*/
void showErrorMessage ( const string &inFileName);

/*
Name: testValidFile
Process: open one file, test for access,
return results
Function Input/Parameters: input file name (string)
Function Output/Parameters: none
Function Output/Return: boolean result of test
Device Input: none
Device Output: hard drive, open file attempt
Dependencies: iostream
*/
bool testValidFile ( const string &fileName);

/*
Name: getNextEvenValue
Process: acquires individual value from file,
if even, acquires, if odd and file still
acessible, continues to acquire
Function Input/Parameters: input stream (ifstream & )
Function Output/Parameters: none
Function Output/Return: next even value (int)
Device Input: hard drive, data acquisition
Device Output: none
Dependencies: fstream
*/
int getNextEvenValue ( ifstream &inf);

/*
Name: convertNumTOChar
Process: decodes large integer to ASCII value,
returns integer as character
Function Input/Parameters: encoded integer
Function Output/Parameters: none
Function Output/Return: decoded character
Device Input: none
Device Output: none
Dependencies: none
*/
char convertNumToChar ( int encodedVal );

// Main Function
int main()
{
// initialize program/function
string inFileName, outFileName;

// input both file names
// function: inputBothFileNames
inputBothFileNames ( inFileName, outFileName );

// check input file for existance
// function: testValidFile
if ( testValidFile (inFileName))
{

// display processing message to user
// function: iostream
cout << "Data is being processed............" << endl;

// process encrypted data to output file
// function: processData
processData ( inFileName, outFileName);

// display processing completed message
// function: iostream
cout << "...............Processing completed" << endl;

}

// otherwise, if input file not available
// operation: else statement
else
{

// show error message
// function: show error message
showErrorMessage (inFileName);
}

// shut down the program

// hold program for user
// function: system/pause
system ("PAUSE");

// return success
return 0;
}

// Supporting Function Implementation

void inputBothFileNames ( string &inFileName, string &outFileName )
{
// intialize function/variables

// prompt for and accept input file name
// function: iostream <<, >>
cout << "Enter Input File Name: ";
cin >> inFileName;

// prompt for and accept output file name
// function: iostream <<, >>
cout << "Enter Output File Name: ";
cin >> outFileName;
}

void processData ( const string &inFileName, const string &outFileName)
{
// intialize function/variable

// set a space counter to zero
int spaceCounter = 0;

// declare other variables
ifstream fin;
ofstream fout;
int nextEvenVal;
char outputCharacter;

// clear and open input file
// functon: fstream .clear, .open
fin.clear ();
fin.open ( inFileName.c_str() );


// open output file
// function: fstream .open
fout.open ( outFileName.c_str() );

// acquire first even value
// function: getNextEvenValue
nextEvenVal = getNextEvenValue ( fin );

// loop across input file
// function: fstream .good
while ( fin.good())
{

// convert number to character
// function: convertNumToChar
outputCharacter = convertNumToChar ( nextEvenVal );

// output character to output file
// function: fstream <<
fout << outputCharacter << SPACE;

// check character for space
if ( outputCharacter == SPACE)
{
// increment space counter
spaceCounter ++;
}

// check for tenth space or end of line character
if ( spaceCounter == 10 || outputCharacter == END_OF_LINE )
{
// output end line
// function: fstream <<
fout << endl;

// reset space counter
spaceCounter = 0;

// get next even value
// function: getNextEvenValue
nextEvenVal = getNextEvenValue ( fin );
}

// end loop
}

// output end line
// function: fstream <<
fout << endl;

// close both files
// function: fstream .close
fin.close();
fout.close();

}

void showErrorMessage ( const string &inFileName)
{
// intialize function/variables

// display error message with file name
// function: iostream I/O tools
cout << "ERROR: File " << inFileName << " Not Found. Program Aborted." << endl;

// end program
system ("PAUSE");
exit (1);
}

bool testValidFile ( const string &fileName)
{
// intialize function/variables
ifstream fin;

// clear and open file
// function: fstream .clear, .open
fin. clear();
fin.open ( fileName.c_str());


// test file for succesful open
// function: fstream .good
if ( fin.good() )
{

// close file
// function: fstream .close
fin.close();

// return true
return true;
}

// return false
else
{
return false;
}
}

int getNextEvenValue ( ifstream &inf)
{
// intialize function/variables
int inputVal;

// get next value
inf >> inputVal;


// loop while value is odd
// and while data is still available
// function: fstream .good
while ( inf.good() && inputVal % 2 == 1 )
{

// get next value
// function: fstream >>
inf >> inputVal;

// end loop
}

// return even value
return inputVal;
}

char convertNumToChar ( int encodedVal )
{
// initialize function/variables
char decodedValue;

// divide number by 1000
decodedValue = encodedVal / 1000;

// return number as character
// operation: cast
return decodedValue;
}
Please use code tages: [code]Your code[/code]

your code is hard to read. The comments don't add information just bloats your code

The reason why you stuck in the loop you can find in processData():

This

nextEvenVal = getNextEvenValue ( fin );

is only executed when this

if ( spaceCounter == 10 || outputCharacter == END_OF_LINE )

is true. Once it's not true your not going to read values anymore.

Solution: take the getNextEvenValue() out of the if scope.
Topic archived. No new replies allowed.