This programs seems to run perfectly fine. but there is a bug in the get_number function I tried to find for hours but I couldnt. See if you guys can help me. Any help is greatly appreciated. Thanks
fconvert.h
// This is the header file for the functions used in
// the character string to real number conversion
// program (CharToFloat).
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <iomanip>
using namespace std;
void get_number(ifstream& numbers, char cfloat[]);
// This function reads a real number as chars from
// file numbers and returns it as a string. This
// function ignores any characters other than digits
// and periods.
float convert_number(char cfloat[]);
// This function takes as a parameter the real number
// as a character string (cfloat), converts it to a
// floating-point number (a float) and returns the
// float.
void print_number(char cfloat[],float fnumber);
// This function takes the real number character string
// (cfloat) and its floating-point counterpart (fnumber)
// and prints them with appropriate labels.
fconvert.cpp
// Definition file for the string to real number
// convertion program (CharToFloat).
#include "fconvert.h"
using namespace std;
void get_number(ifstream& numbers, char cfloat[])
// This function reads a real number as chars from
// file numbers and returns it as a string. This
// function ignores any characters other than digits
// and periods.
{
char ch;
int index = 0;
while (!numbers.eof() && ((ch = numbers.get()) != '.'))
if (isdigit(ch))
cfloat[index++] = ch;
if (ch == '.')
{
cfloat[index++] = ch;
while (!numbers.eof() && ((ch = numbers.get()) != '\n'))
if (isdigit(ch))
cfloat[index++] = ch;
cfloat[index] = '\0';
}
else if (!numbers.eof() && (ch = numbers.get()) == '\n')
{
cout << "** Error - no '.' detected.\n";
cfloat = "";
cfloat[index] = '\0';
}
}
float convert_number(char cfloat[])
// This function takes as a parameter the real number
// as a character string (cfloat), converts it to a
// floating-point number (a float) and returns the
// float.
{
int count = 0;
int integer = 0;
int fraction = 0;
int fdigits = 0;
integer = (cfloat[count++] - '0');
while (cfloat[count] != '.')
// Since the ASCII digit codes are in order, subtracting an
// ASCII zero char from a digit char gives the digit value.
integer = (integer * 10) + (cfloat[count++] - '0');
count++;
fraction = (cfloat[count++] - '0');
fdigits++;
while (count < strlen(cfloat))
{
fraction = (fraction * 10) + (cfloat[count++] - '0');
fdigits++;
}
return float(integer + (fraction / (pow(10.0,fdigits))));
}
void print_number(char cfloat[],float fnumber)
// This function takes the real number character string
// (cfloat) and it floating-point counterpart (fnumber)
// and prints them with appropriate labels.
{
cout << endl << "The string " << cfloat << " converts to the "
<< "floating-point number: " << setprecision(8) << fnumber << endl;
}
main.cpp
// driver routine for a program to convert a real number
// read from a file as chars to a float number.
#include "fconvert.h"
int main()
{
ifstream numbers; // handle for input file
char cfloat[20]; // real number read as a string
float fnumber; // converted real number
cout << "\n** Character String to float number Program ** \n\n";
numbers.open("numbers.dat");
if (numbers.fail())
{
cout << "** Error - bad file open. \n";
system("pause");
exit(1);
}
get_number(numbers, cfloat);
while (numbers)
{
fnumber = convert_number(cfloat);
print_number(cfloat, fnumber);
get_number(numbers, cfloat);
}
numbers.close();
system("PAUSE");
return 0;
}