I have run into a unsolvable runtime error, I always get the message invalid floating point error, with the following C++ program. This program reads floats from a file(float.txt)
1.56
.98
5.78

Then sorts to put the largest float on top. But when I try to read this I get the ERROR Invalid floating point error. I am not a student in fact I am in scrap metal currently recovering from a broken wrist. needed is the above file float.txt program below.

I have struggled with this for a week, float.txt attached.

#include <ctype.h>
 #include <conio.h>
 #include <fstream.h>
 #include <iomanip.h>
 #include <iostream.h>
 #include <math.h>
 #include <ostream.h>
 #include <stdlib.h>
 #include <stdio.h>
  ifstream infile1,infile2,infile6,infile7;
  int Gnum,amountRead = 0,max_read = 99,i = 0,sn,ii;
  const int maxn = 4200;
  float array[maxn] = {0.00000};
  float num,x = 0,xx;
 void getfloat(),read();
 float sort();
 float sort()
 {
  int j;
  float temp;
  for(i=0;i<amountRead;i++);
  for(i=0;i<amountRead;i++)
  for(j=0;j<amountRead-i;j++)
  if(array[j]<array[j+1])
  {
   temp = array[j];
   array[j] = array[j+1];
   array[j+1] = temp;
  }
   amountRead++;
  for(ii = 0;ii<amountRead;ii++)
  {
  if(array[ii] != 0)
  {
   cout<<array[ii]<<endl;
   xx = array[ii];
   ofstream output14("sort2.txt",ios::app);
   output14<<xx<<endl;
   output14.close();
   }
  }
  }
  void getfloat()
 {
  infile7.open("sort2.txt");
   infile7>>x;///  ERROR HERE
   while(infile7)
   {
    infile7>>x;///  ERROR HERE ALSO
    ofstream output15("sort3.txt",ios::app);
    output15<<x<<endl;
    output15.close();
   }
   infile7.close();
  }
 void main()
 {
  infile6.open("float.txt");
  while(infile6>>array[amountRead]&& amountRead < max_read)
  {
    amountRead++;
  }
  for(i = 0; i < amountRead; i++)
  {
    cout<<array[i]<<endl;
  }
  infile6.close();
  sort();
  getfloat();
  getch();
 } ///End of main
Attachments
1.56
.98
5.78

Well first off, you must be using some ancient fossil compiler.

What was the error message exactly? Something like "floating point formats not linked"?

Anyway, cleaned up for the modern world, your code seems fine.
It's just the include files renamed, and int main at the end.

#include <fstream>
#include <iomanip>
#include <iostream>
#include <cmath>
#include <ostream>
using namespace std;

ifstream infile1,infile2,infile6,infile7;
  int Gnum,amountRead = 0,max_read = 99,i = 0,sn,ii;
  const int maxn = 4200;
  float array[maxn] = {0.00000};
  float num,x = 0,xx;
 void getfloat(),read();
 float sort();
 float sort()
 {
  int j;
  float temp;
  for(i=0;i<amountRead;i++);
  for(i=0;i<amountRead;i++)
  for(j=0;j<amountRead-i;j++)
  if(array[j]<array[j+1])
  {
   temp = array[j];
   array[j] = array[j+1];
   array[j+1] = temp;
  }
   amountRead++;
  for(ii = 0;ii<amountRead;ii++)
  {
  if(array[ii] != 0)
  {
   cout<<array[ii]<<endl;
   xx = array[ii];
   ofstream output14("sort2.txt",ios::app);
   output14<<xx<<endl;
   output14.close();
   }
  }
  }
  void getfloat()
 {
  infile7.open("sort2.txt");
   infile7>>x;///  ERROR HERE
   while(infile7)
   {
    infile7>>x;///  ERROR HERE ALSO
    ofstream output15("sort3.txt",ios::app);
    output15<<x<<endl;
    output15.close();
   }
   infile7.close();
  }
 int main()
 {
  infile6.open("float.txt");
  while(infile6>>array[amountRead]&& amountRead < max_read)
  {
    amountRead++;
  }
  for(i = 0; i < amountRead; i++)
  {
    cout<<array[i]<<endl;
  }
  infile6.close();
  sort();
  getfloat();
 } ///End of main

Results

$ g++ foo.cpp
$ ./a.out 
1.56
0.98
5.78
5.78
1.56
0.98
$ cat sort2.txt 
5.78
1.56
0.98
$ cat sort3.txt 
1.56
0.98
0.98

First the compiler is from 1999, Its a Borland I can try to hunt around for what version it is. The ERRor is. "Project test.exe raised exception classEInvalidOp with message 'Invalid floating point operation'." after this message I hit reset and I can start over

Forgot to add It creates the file sort2.txt, also I needed the include files to end with dot.h

Tiverton

Taking your code to the 21st century, and doing correct indentation (which helps understanding the code), we get:

#include <fstream>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <conio.h>

using namespace std;

int amountRead = 0,max_read = 99;
const int maxn = 4200;
float array[maxn] = {0.00000};

void getfloat();
void read();
float sort();

float sort()
{
  // you could also use the standard sort function:
  //sort( array, array + amountRead, greater<float>() );
  for(int i=0;i<amountRead;i++)      //prefer declaring 'i' in the for-loop.
    for(int j=0;j<amountRead-i;j++)  //prefer declaring 'j' in the for-loop.
      if(array[j]<array[j+1])
        swap(array[j], array[j+1]
        
  amountRead++; //what is this?

  ofstream outfile("sort2.txt"); //don't do open-close at each iteration.
  for(int i = 0; i < amountRead; ++i)
  {
    //if(array[i] != 0)  //this comparison is useless of 'float' variables, they are rarely exactly 0.
    //{
    cout << array[i] << endl;
    outfile << array[i] << endl;
    //}
  }
  //Where is the return value???
}

void getfloat()
{
  ifstream infile("sort2.txt"); //declare variables when you use them first.
  ofstream outfile("sort3.txt"); //don't open-close the file for no reason.
  float x = 0.0; //prefer local variables, try to avoid global variables when unnecessary.
  while(infile >> x)  //check the input at the same time as reading.
    outfile << x << endl;
}

int main()  //the C++ standard requires that main() returns an integer.
{
  ifstream infile("float.txt");
  while((amountRead < max_read) && (infile >> array[amountRead])) //invert the conditions to use the "short-cut" feature of '&&'.
    ++amountRead; //pre-increment is preferred in general.
  
  for(int i = 0; i < amountRead; ++i)
    cout << array[i] << endl;

  sort();
  getfloat();
  getch();
  return 0; //everything went well.
} ///End of main

Except for being old in style, and having certain bad-style choices (like using too many unnecessary global variables), there is nothing erroneous in your code. It should work, in theory (and in practice, as Salem showed).

The only reasons I can think of for the run-time error you get is that you either you try to read past the end of the file, or use an invalid float obtained from error past the end, or your platform does not consider that ".98" is a valid floating point value, try "0.98" instead (also note that some implementations use the system-wide notation for the "dot", which is, for some, a comma instead of a dot (e.g. most of Europe)).

Edited 5 Years Ago by mike_2000_17: n/a

swap(array[j], array[j+1]);

Had to add a closing brace, semi colon when I cut and pasted it actually creates sort3.txt but nothing is in sort3.txt. Alot of why its written the way it is is from "The Beginners guide to C++" by Oleg Yaroshenko WROX press ltd 1994.

>>"The Beginners guide to C++" by Oleg Yaroshenko WROX press ltd 1994.

Get something that is more up-to-date. This book predates the first C++ standard (1998). Prefer a book that is post-2003. Here are recommendations.

I will try on Amazon, but I will miss Yaroshenko helped me out of alot of jams. forgot to add that in sort2.txt it for some reason adds a zero, could this be the problem.

> forgot to add that in sort2.txt it for some reason adds a zero, could this be the problem.
I didn't see anything like that when I ran your code.
And nothing in the code you've posted so far suggests that it would add a zero either.

What's your latest code?

Pretty much what you gave but I renamed it test2, I have had float sorts before that have worked fine, one thing I can say is that the float.txt file is for an example. This is part of alarger program that generates floats only ending in five. Program calculates NFL coaches Won loss record where ties are counted as half a win so 7-1-1 would be 7.5 wins.

#include <fstream>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <conio.h>

using namespace std;

int amountRead = 0,max_read = 99;
const int maxn = 4200;
float array[maxn] = {0.00000};

void getfloat();
void read();
float sort();

float sort()
{
  // you could also use the standard sort function:
  //sort( array, array + amountRead, greater<float>() );
  for(int i=0;i<amountRead;i++)      //prefer declaring 'i' in the for-loop.
    for(int j=0;j<amountRead-i;j++)  //prefer declaring 'j' in the for-loop.
      if(array[j]<array[j+1])
        swap(array[j], array[j+1]);
        
  amountRead++; //what is this?

  ofstream outfile("sort2.txt"); //don't do open-close at each iteration.
  for(int i = 0; i < amountRead; ++i)
  {
    //if(array[i] != 0)  //this comparison is useless of 'float' variables, they are rarely exactly 0.
    //{
    cout << array[i] << endl;
    outfile << array[i] << endl;
    //}
  }
  //Where is the return value???
}

void getfloat()
{
  ifstream infile("sort2.txt"); //declare variables when you use them first.
  ofstream outfile("sort3.txt"); //don't open-close the file for no reason.
  float x = 0.0; //prefer local variables, try to avoid global variables when unnecessary.
  while(infile >> x)  //check the input at the same time as reading.
    outfile << x << endl;
}

int main()  //the C++ standard requires that main() returns an integer.
{
  ifstream infile("float.txt");
  while((amountRead < max_read) && (infile >> array[amountRead])) //invert the conditions to use the "short-cut" feature of '&&'.
    ++amountRead; //pre-increment is preferred in general.
  
  for(int i = 0; i < amountRead; ++i)
    cout << array[i] << endl;

  sort();
  getfloat();
  getch();
  return 0; //everything went well.
} ///End of main

When I change everything to ints everything works fine for some reason.

The extra 0 in sort2.txt is caused by this line:

amountRead++; //what is this?

in the sort() function, and thus the "What is this?" comment that I made.

Nope actually error is where it reads::

cin>>x;
  while(infile >> x)

I added the above cin and the error then shows up there, I change it to a int and no problems.

Here is my heat humidity program where I cin two floats and calculates the Heat index
everything here works fine of course there is no sort();

#include <iostream.h>
#include <conio.h>
#include <ostream.h>
#include <fstream.h>
#include <math.h>
#include <time.h>
ofstream output;
float H,T;
float A,B,C,D,E,F,G,I;
float a,b,c,d,e,f;
float result;


int main()
{
 char dateStr [9];
          char timeStr [9];
          _strdate( dateStr);
          printf( "The current date is %s \n", dateStr);
         _strtime( timeStr );
         printf( "The current time is %s \n", timeStr);
         ofstream output("HI.txt",ios::app);
     output<<dateStr<<" "<<timeStr<<" ";
     output.close();

 cout<<"Enter temperature "<<endl;
 cin>>T;
 if(T < 80)
 {
  cout<<" Formula will not give accurate reading with temp below 80 "<<endl;
 }
 cout<<"Enter humidity "<<endl;
 cin>>H;

 A =  2.04901523 * T;
 B = 10.14333127 * H;
 C =  (.22475541 * T * H);
 a = T * T;
 b = pow(10,-3);
 D = 6.83783 * b * (a);
 c = pow(10,-2);
 d = H * H;
 E = 5.481717 * c * d;
 F = 1.22874 * b * a * H;
 e = pow(10,-4);
 G = 8.5282 * e * T * d;
 f = pow(10,-6);
 I = 1.99 * f * a * d;

   result = -42.379 + A + B - C - D - E + F + G - I;


   cout<<"Heat index is "<<result<<" ";
   ofstream output2("HI.txt",ios::app);
    output2<<"Temp "<<T<<" Hum "<<H;
   output2.close();
   if(result <= 79)
   {
     cout<<"Not a accurate reading "<<endl;
     ofstream output("HI.txt",ios::app);
     output<<" Not a accurate reading HI "<<result<<endl;
     output.close();
   }
   if(result >= 80 && result <= 90)
   {
   cout<<"Use caution today, for direct sunlight add 14 to heat index "<<endl;
   ofstream output("HI.txt",ios::app);
     output<<" Caution day HI "<<result<<endl;
     output.close();
   }
   if(result >= 91 && result <= 105)
   {
   cout<<"Use extreme caution today, for direct sunlight add 14 to heat index "<<endl;
   ofstream output("HI.txt",ios::app);
     output<<" Ext caution day HI "<<result<<endl;
     output.close();
   }
   if(result >= 106 && result <= 130)
   {
   cout<<"Dangourous today, for direct sunlight add 14 to heat index "<<endl;
   ofstream output("HI.txt",ios::app);
     output<<" Danger day HI "<<result<<endl;
     output.close();
   }
   if(result >= 131)
   {
   cout<<"Extreme danger today, for direct sunlight add 14 to heat index "<<endl;
   ofstream output("HI.txt",ios::app);
     output<<" Extdanger day HI "<<result<<endl;
     output.close();
   }


 getch();
      return 0;
}

TO: Mikael Persson

In your modern code you had a commented cout omment, asking "Where is your return value" THAT WAS THE PROBLEM. THERE was no return value.

I checked my Quarterback rating program and there it was.

so I added just before the fuction closing brace

return 1;

I wont close this thread just yet to get some replys

Here is the solved program, with much assitance from mike 2000 17

#include <fstream>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <conio.h>

using namespace std;
int l;
int amountRead = 0,max_read = 99;
const int maxn = 4200;
float array[maxn] = {0.00000};

void getfloat();
void read();
float sort();

float sort()
{
  // you could also use the standard sort function:
  //sort( array, array + amountRead, greater<float>() );
  for(int i=0;i<amountRead;i++)      //prefer declaring 'i' in the for-loop.
    for(int j=0;j<amountRead-i;j++)  //prefer declaring 'j' in the for-loop.
      if(array[j]<array[j+1])
        swap(array[j], array[j+1]);
        
  amountRead++; //what is this?

  ofstream outfile("sort2.txt"); //don't do open-close at each iteration.
  for(int i = 0; i < amountRead; ++i)
  {
    //if(array[i] != 0)  //this comparison is useless of 'float' variables, they are rarely exactly 0.
    //{
    cout << array[i] << endl;
    outfile << array[i] << endl;
    //}
  }
  //Where is the return value???
  //// The below line must be added 
  return 1;
//// The above line must be there for floats
}

void getfloat()
{
  ifstream infile("sort2.txt"); //declare variables when you use them first.
  ofstream outfile("sort3.txt"); //don't open-close the file for no reason.
  float x = 0.0; //prefer local variables, try to avoid global variables when unnecessary.
  //cin>>x;
  while(infile >> x)  //check the input at the same time as reading.
    outfile << x << endl;
}

int main()  //the C++ standard requires that main() returns an integer.
{
  ifstream infile("float.txt");
  while((amountRead < max_read) && (infile >> array[amountRead])) //invert the conditions to use the
  //"short-cut" feature of '&&'.
    ++amountRead; //pre-increment is preferred in general.
  
  for(int i = 0; i < amountRead; ++i)
    cout << array[i] << endl;

  sort();
  getfloat();
  getch();
  return 0; //everything went well.
} ///End of main
This question has already been answered. Start a new discussion instead.