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

Recommended Answers

All 4 Replies

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.

The mind just boggles!

See if you guys can help me. Any help is greatly appreciated. Thanks

We could, but you didn't give us anything to work with. What makes you think there's a bug in a perfectly running program?

Details....

Sorry about that. The condition where it takes input from a file with strings for example

12.2
3.589
6.78
9.85

it works fine with the above numbers but if the file has

5.24
2353
5.24
265
2.21212

it doesnt work

what compiler are you using? Did you try using your compiler's debugger to find the problem? If you don't know how to do that then right now is a good time to start learning how to use it.

Can't you just call the standard C function atof() to convert a string to float? Or are you required to write your own function?

Well, your file contains 2 numbers that do not have '.' in them, and the code has an error message that says "Error - no '.' detected.". Therefore I wouldn't expect the file to work.

Making the assumption there's even more you haven't mentioned (like there's a real bug in the code) I'd suggest the following things:

1) format your code consisently. It's very hard to follow with the haphazard indentation you're using
2) add output statements to follow your code as it executes. Then you can see where your code deviates from what you expect to happen.
3) more details!!!! We need to understand the problem. "it doesn't work" tells us nothing. Explain in detail what it does, what it doesn't do, why you expect different results, etc...

Remember,
A) we did not write the code - therefore we don't know what's supposed to happen
B) we cannot run your code - therefore we can't see what actually happened. Most of us are probably not going to create a project in our compiler just to execute your code.
C) so all we can do is look over your code. Therefore, if we don't know what we're looking for, it's very hard to find.

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.