Input/Output files and floating-point numbers

When I try to input a file I get negative crazy numbers

Page 1 of 1

9 Replies - 2903 Views - Last Post: 18 June 2009 - 08:51 AM Rate Topic: -----

#1 turner2108  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 28-January 09

Input/Output files and floating-point numbers

Post icon  Posted 17 June 2009 - 12:48 PM

How come when you save a floating-point number to file saves correctly but when I went to run another program to open the file and display the numbers they looked CRAZY!

I save: 25.65
76.99
101.89
43.78
12.45


but when I open another program to display the numbers it shows
-925596313493178000000000000000000000000000000000000.00

and numbers like that for all of them!!!

// Setup standard output.
#include <iostream>
// Required to use stream manipulators.
#include <iomanip>
// Required to use files code.
#include <fstream>
using namespace std;

// Marks standard function.
int main()
{
	ifstream inputFile;

// Declaration of variables and initialization.
 double num1,			// Number one.
		num2,			// Number two.
		num3,			// Number three.
		num4,			// Number four. 
		num5;			// Number five.
  double sum;

inputFile.open("storingfile.txt");
	cout << "Reading information from the file.";

	// Seperates lines for easier readability.
	cout << endl;


cout << fixed << showpoint << setprecision(2);

inputFile >> num1;
cout << num1 << endl;		// Number one.
inputFile >> num2;
cout << num2 << endl;		// Number two.
inputFile >> num3;
cout << num3 << endl;		// Number three.
inputFile >> num4;
cout << num4 << endl;		// Number four.
inputFile >> num5;
cout << num5 << endl;		// Number five.


 // Seperates lines for easier readability.
	cout << endl;

// Closes file.
inputFile.close();
cout << "Done retrieving numbers." << endl; 

// Seperates lines for easier readability.
	cout << endl;

sum = num1 + num2 + num3 + num4 + num5;
cout << "Sum of numbers:   "; 
cout << sum << endl;

	 // Stops the program from closing automatically.
	system("pause");
	return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Input/Output files and floating-point numbers

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Input/Output files and floating-point numbers

Posted 17 June 2009 - 01:08 PM

View Postturner2108, on 17 Jun, 2009 - 11:48 AM, said:

How come when you save a floating-point number to file saves correctly but when I went to run another program to open the file and display the numbers they looked CRAZY!

I save: 25.65
76.99
101.89
43.78
12.45


but when I open another program to display the numbers it shows
-925596313493178000000000000000000000000000000000000.00

and numbers like that for all of them!!!

// Setup standard output.
#include <iostream>
// Required to use stream manipulators.
#include <iomanip>
// Required to use files code.
#include <fstream>
using namespace std;

// Marks standard function.
int main()
{
	ifstream inputFile;

// Declaration of variables and initialization.
 double num1,			// Number one.
		num2,			// Number two.
		num3,			// Number three.
		num4,			// Number four. 
		num5;			// Number five.
  double sum;

inputFile.open("storingfile.txt");
	cout << "Reading information from the file.";

	// Seperates lines for easier readability.
	cout << endl;


cout << fixed << showpoint << setprecision(2);

inputFile >> num1;
cout << num1 << endl;		// Number one.
inputFile >> num2;
cout << num2 << endl;		// Number two.
inputFile >> num3;
cout << num3 << endl;		// Number three.
inputFile >> num4;
cout << num4 << endl;		// Number four.
inputFile >> num5;
cout << num5 << endl;		// Number five.


 // Seperates lines for easier readability.
	cout << endl;

// Closes file.
inputFile.close();
cout << "Done retrieving numbers." << endl; 

// Seperates lines for easier readability.
	cout << endl;

sum = num1 + num2 + num3 + num4 + num5;
cout << "Sum of numbers:   "; 
cout << sum << endl;

	 // Stops the program from closing automatically.
	system("pause");
	return 0;
}


to output to a file you use:
inputfile<<"some text"<<endl;


to read from a file, you dont
simply just say:
inputfile>>num1 // wrong


a text file isnt going to
remeber all of your variables,
its just a text file.
you have to read line by line
with the getline function.
string line; //stores contents in this string
ifstream inputfile("storingfile.txt");
while(getline(inputfile,line))
{
// Everything in the file is stored into a string
cout<<line<<endl; // displays file contents
// you need to convert num1 to a string
//then you can do something like this.
	if(line == num1str)
	{
	cout<<line<<endl; // line containing num1
	}

}


Was This Post Helpful? 0
  • +
  • -

#3 turner2108  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 28-January 09

Re: Input/Output files and floating-point numbers

Posted 17 June 2009 - 02:35 PM

View PostImaSexy, on 17 Jun, 2009 - 12:08 PM, said:

View Postturner2108, on 17 Jun, 2009 - 11:48 AM, said:

How come when you save a floating-point number to file saves correctly but when I went to run another program to open the file and display the numbers they looked CRAZY!

I save: 25.65
76.99
101.89
43.78
12.45


but when I open another program to display the numbers it shows
-925596313493178000000000000000000000000000000000000.00

and numbers like that for all of them!!!

// Setup standard output.
#include <iostream>
// Required to use stream manipulators.
#include <iomanip>
// Required to use files code.
#include <fstream>
using namespace std;

// Marks standard function.
int main()
{
	ifstream inputFile;

// Declaration of variables and initialization.
 double num1,			// Number one.
		num2,			// Number two.
		num3,			// Number three.
		num4,			// Number four. 
		num5;			// Number five.
  double sum;

inputFile.open("storingfile.txt");
	cout << "Reading information from the file.";

	// Seperates lines for easier readability.
	cout << endl;


cout << fixed << showpoint << setprecision(2);

inputFile >> num1;
cout << num1 << endl;		// Number one.
inputFile >> num2;
cout << num2 << endl;		// Number two.
inputFile >> num3;
cout << num3 << endl;		// Number three.
inputFile >> num4;
cout << num4 << endl;		// Number four.
inputFile >> num5;
cout << num5 << endl;		// Number five.


 // Seperates lines for easier readability.
	cout << endl;

// Closes file.
inputFile.close();
cout << "Done retrieving numbers." << endl; 

// Seperates lines for easier readability.
	cout << endl;

sum = num1 + num2 + num3 + num4 + num5;
cout << "Sum of numbers:   "; 
cout << sum << endl;

	 // Stops the program from closing automatically.
	system("pause");
	return 0;
}


to output to a file you use:
inputfile<<"some text"<<endl;


to read from a file, you dont
simply just say:
inputfile>>num1 // wrong


a text file isnt going to
remeber all of your variables,
its just a text file.
you have to read line by line
with the getline function.
string line; //stores contents in this string
ifstream inputfile("storingfile.txt");
while(getline(inputfile,line))
{
// Everything in the file is stored into a string
cout<<line<<endl; // displays file contents
// you need to convert num1 to a string
//then you can do something like this.
	if(line == num1str)
	{
	cout<<line<<endl; // line containing num1
	}

}








i wish i could understand that code but i just havent got that far yet in my book to understand that yet.
Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Input/Output files and floating-point numbers

Posted 17 June 2009 - 02:45 PM

string line; // a blank string used to store text file in
ifstream inputfile("storingfile.txt"); // reads in text file
while(getline(inputfile,line)) // loops to read every line
{
// you need to convert num1 to a string (google itoa() function)
//then you can do something like this.
	if(line == num1str)
	{
	cout<<line<<endl; // line containing num1 //displays current line
	}

}


This post has been edited by ImaSexy: 17 June 2009 - 02:45 PM

Was This Post Helpful? 0
  • +
  • -

#5 Notorion  Icon User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 378
  • Joined: 17-February 09

Re: Input/Output files and floating-point numbers

Posted 17 June 2009 - 04:59 PM

You forgot to test if the file has actually been opened.

if(!inputFile)
{
cout << "File could not be opened";
return 0;
}


Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Input/Output files and floating-point numbers

Posted 17 June 2009 - 05:06 PM

View PostNotorion, on 17 Jun, 2009 - 03:59 PM, said:

You forgot to test if the file has actually been opened.

if(!inputFile)
{
cout << "File could not be opened";
return 0;
}



thats not what the problem is
Was This Post Helpful? 0
  • +
  • -

#7 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Input/Output files and floating-point numbers

Posted 17 June 2009 - 06:20 PM

View PostImaSexy, on 17 Jun, 2009 - 07:06 PM, said:

View PostNotorion, on 17 Jun, 2009 - 03:59 PM, said:

You forgot to test if the file has actually been opened.

if(!inputFile)
{
cout << "File could not be opened";
return 0;
}



thats not what the problem is


Actually, that probably is the problem, because there is nothing wrong with the code that turner2108 posted. It works correctly as long as the input file exists and actually contains what the OP says.

You should add the code that Notorion posted, right after your "inputFile.open()" line. And you should open the file with a text editor & make sure it contains what you think it does.
Was This Post Helpful? 0
  • +
  • -

#8 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1431
  • View blog
  • Posts: 4,966
  • Joined: 19-February 09

Re: Input/Output files and floating-point numbers

Posted 17 June 2009 - 06:51 PM

Your original code works perfectly with my compiler.

Two things to try:-
1) Check file is opened, maybe spelling mistake.

2) Take comments out from variable initialisation statement
- very wild idea.

double num1, 
		num2,  
		num3,   
		num4,  
		num5; 


I'm not confident of either of these working, sorry couldn't be of more help.
Was This Post Helpful? 0
  • +
  • -

#9 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Input/Output files and floating-point numbers

Posted 18 June 2009 - 07:53 AM

View Postr.stiltskin, on 17 Jun, 2009 - 05:20 PM, said:

View PostImaSexy, on 17 Jun, 2009 - 07:06 PM, said:

View PostNotorion, on 17 Jun, 2009 - 03:59 PM, said:

You forgot to test if the file has actually been opened.

if(!inputFile)
{
cout << "File could not be opened";
return 0;
}



thats not what the problem is


Actually, that probably is the problem, because there is nothing wrong with the code that turner2108 posted. It works correctly as long as the input file exists and actually contains what the OP says.

You should add the code that Notorion posted, right after your "inputFile.open()" line. And you should open the file with a text editor & make sure it contains what you think it does.

then explain to me how this works?
inputFile >> num1;
cout << num1 << endl;		// Number one.
inputFile >> num2;
cout << num2 << endl;		// Number two.
inputFile >> num3;
cout << num3 << endl;		// Number three.
inputFile >> num4;
cout << num4 << endl;		// Number four.
inputFile >> num5;
cout << num5 << endl;   


Was This Post Helpful? 0
  • +
  • -

#10 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Input/Output files and floating-point numbers

Posted 18 June 2009 - 08:51 AM

View PostImaSexy, on 18 Jun, 2009 - 09:53 AM, said:

then explain to me how this works?
inputFile >> num1;
cout << num1 << endl;		// Number one.
inputFile >> num2;
cout << num2 << endl;		// Number two.
inputFile >> num3;
cout << num3 << endl;		// Number three.
inputFile >> num4;
cout << num4 << endl;		// Number four.
inputFile >> num5;
cout << num5 << endl;   


I don't have time to explain what you can read in any textbook or tutorial.

Here:
http://www.cplusplus...operator%3E%3E/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1