reading from a file and using the data HELP!

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 1327 Views - Last Post: 20 December 2009 - 01:26 PM Rate Topic: -----

Topic Sponsor:

#1 Omegaclass  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 01-November 09

reading from a file and using the data HELP!

Post icon  Posted 13 December 2009 - 06:40 PM

Hi Gentlemen and Ladies,

i am trying to find a way to read a file and perform mathematics on the numbers dose anyone know how i should proceed on this.




#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

struct car 
{
	
	int car_no;   
	int  milesDr;		
	int  gallons;  
};

int main()
{
	const int reports = 5;
	int temp_mpg = 0;
	int goallonsTotal = 0;
	int miles_total = 0;

	car records[reports] = 
	{
		{25, 1450, 62},
		{36, 3240, 136},
		{44, 1769, 67},
		{52, 2360, 105},
		{68, 2115, 67}
	};


cout << endl;

cout << setiosflags(ios::left);
for (int i = 0; i < reports; i++)
{
	cout << setw(8)<<"Car ID: " << records[i].car_no;
	cout << setw(18)<<"  Miles per gallon: "<< records[i].milesDr/records[i].gallons << endl;
	temp_mpg += records[i].milesDr/records[i].gallons;
}
cout << "Average miles per gallon of fleet is: " << temp_mpg/reports << endl;


system ("pause");

	return 0;
}




the above data in this program will need to be read from a file and do the same thing.

where should i go to find the answers to this solution, i don't think i need any code from anyone but a good resource to educate myself will be of great help and much appreciated.

thanks guys.

This post has been edited by Omegaclass: 13 December 2009 - 08:11 PM


Is This A Good Question/Topic? 0
  • +

Replies To: reading from a file and using the data HELP!

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 983
  • View blog
  • Posts: 5,125
  • Joined: 28-September 06

Re: reading from a file and using the data HELP!

Posted 13 December 2009 - 09:47 PM

Have a read here for info on File I/O
http://www.cplusplus...tutorial/files/

Feel free to ask any questions you have after reading that.
Was This Post Helpful? 0
  • +
  • -

#3 Omegaclass  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 01-November 09

Re: reading from a file and using the data HELP!

Posted 13 December 2009 - 10:43 PM

i don't understand how to convert an element in a file to a int could you give an example code that could help?


#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;


string filecars = "cars.dat";
int num;
long offset, last;
ifstream infile(filename.c_str())

infile.seekg(0L, ios::end);
last = infile.tellg();
for (offset = 4L; offset <= last; offset++)
{
infile.seekg(-offset, ios::end);
num = infile.get();
cout << num;
}
infile.close();
return 0;
}



This post has been edited by Omegaclass: 13 December 2009 - 10:59 PM

Was This Post Helpful? 0
  • +
  • -

#4 Omegaclass  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 01-November 09

Re: reading from a file and using the data HELP!

Posted 13 December 2009 - 11:00 PM

sorry i mean "int" so i edited the post from char to int
Was This Post Helpful? 0
  • +
  • -

#5 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 80
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: reading from a file and using the data HELP!

Posted 13 December 2009 - 11:01 PM

You need to post some of the contents of the data file. We can't really help a whole lot if we don't know what the file contains. If the file just contains numbers, then you probably don't have to use seekg() but just use >> to read the numbers, such as
int num;
while( inFile >> num ) // read all the numbers in the file
{
	// do something with the number
}


Was This Post Helpful? 0
  • +
  • -

#6 Omegaclass  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 01-November 09

Re: reading from a file and using the data HELP!

Posted 13 December 2009 - 11:22 PM

Create a file containing the following: car numbers, miles driven, and gallons of gas used
in each car (do not include the readings):
Car Number, Miles Driven, Gallons Used
25 1450 62
36 3240 136
44 1769 67
52 2360 105
68 2115 67

reads the data in the file and displays the car number, miles driven,
gallons used and the miles per gallon for each car. The output should also contain the
total miles drive, total gallons used, and average miles per gallon for all the cars. These
totals should be displayed at the end of the output report.

here is the data i need to manipulate thanks for your input on this, i don't plan to sleep till this is done, i knovv you understand i am sure you have been there more then once, lol
Was This Post Helpful? 0
  • +
  • -

#7 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 80
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: reading from a file and using the data HELP!

Posted 13 December 2009 - 11:29 PM

You need three integers, so just read all three in a loop
int carno, miles, gas;
while( infile >> carno >> miles >> gas)
{

}


This post has been edited by Ancient Dragon: 13 December 2009 - 11:29 PM

Was This Post Helpful? 1
  • +
  • -

#8 Omegaclass  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 01-November 09

Re: reading from a file and using the data HELP!

Posted 13 December 2009 - 11:37 PM

Thank you very much, this hint may have saved me, thanks!
Was This Post Helpful? 0
  • +
  • -

#9 Omegaclass  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 01-November 09

Re: reading from a file and using the data HELP!

Posted 13 December 2009 - 11:52 PM

sorry i just don't see how to make the while loop work and the program didn't open the file i placed in the folder any suggestion?


#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
int main () 
{

	string carfile = "fleet.txt";
	int nums, mpg, miles, carno, milesd, gas;
	long offset, last;
	ifstream infile (carfile.c_str());
	if (infile.fail())
	{ 
	cout << "error"<<endl;
	exit(1);
	}
	//infile.seekg(0L,ios::end);
	//last = infile.tellg();
		
while( infile >> carno >> miles >> gas)
{
cout << miles/gas;
}


	infile.close();
	system("pause");
	return 0;

}


Was This Post Helpful? 0
  • +
  • -

#10 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 983
  • View blog
  • Posts: 5,125
  • Joined: 28-September 06

Re: reading from a file and using the data HELP!

Posted 14 December 2009 - 03:44 AM

View PostOmegaclass, on 13 Dec, 2009 - 10:52 PM, said:

any suggestion?


Yes! I suggest you follow the advice given to you by Ancient Dragon here:

View PostAncient Dragon, on 13 Dec, 2009 - 10:01 PM, said:

You need to post some of the contents of the data file. We can't really help a whole lot if we don't know what the file contains.


Please share the first 5 lines of the text file you are working from.
Was This Post Helpful? 0
  • +
  • -

#11 Omegaclass  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 01-November 09

Re: reading from a file and using the data HELP!

Posted 14 December 2009 - 05:17 AM

25 1450 62
36 3240 136
44 1769 67
52 2360 105
68 2115 67


this is the first 5 lines
Was This Post Helpful? 0
  • +
  • -

#12 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 983
  • View blog
  • Posts: 5,125
  • Joined: 28-September 06

Re: reading from a file and using the data HELP!

Posted 14 December 2009 - 05:29 AM

When I compile your code I get these warnings that you should have a look at:
DIC.cpp: In function ‘int main()’:
DIC.cpp:10: warning: unused variable ‘nums’
DIC.cpp:10: warning: unused variable ‘mpg’
DIC.cpp:10: warning: unused variable ‘milesd’
DIC.cpp:11: warning: unused variable ‘offset’
DIC.cpp:11: warning: unused variable ‘last’



If I run your code (using the data you posted) I get this output:
2323262231

Is that what you expected?

Since I am having no problem opening the data file the first thing I suggest you check exactly where you saved the data file and exactly what name you gave it.

Can I also suggest that using "last" as a variable name is a bad idea.
Choose another name that isn't used by the C++ language for it's own purposes because using reserved words is a silly risk to take.
Was This Post Helpful? 0
  • +
  • -

#13 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 80
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: reading from a file and using the data HELP!

Posted 14 December 2009 - 06:31 AM

I suspect he needs to add '\n' to separate the output cout << miles/gas << '\n';
Was This Post Helpful? 0
  • +
  • -

#14 barnwillyb  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 108
  • Joined: 22-May 07

Re: reading from a file and using the data HELP!

Posted 16 December 2009 - 05:21 AM

View PostOmegaclass, on 13 Dec, 2009 - 10:52 PM, said:

sorry i just don't see how to make the while loop work and the program didn't open the file i placed in the folder any suggestion?


#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
int main () 
{

	string carfile = "fleet.txt";
	int nums, mpg, miles, carno, milesd, gas;
	long offset, last;
	ifstream infile (carfile.c_str());
	if (infile.fail())
	{ 
	cout << "error"<<endl;
	exit(1);
	}
	//infile.seekg(0L,ios::end);
	//last = infile.tellg();
		
while( infile >> carno >> miles >> gas)
{
cout << miles/gas;
}


	infile.close();
	system("pause");
	return 0;

}




You might have to tell your computer where the file is using the folder name and then a slash line and then the name of the file like below:

//——————————————————————————————————————————————————————————
//						  includes
//——————————————————————————————————————————————————————————

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

/*__________________________________________________________*/
/*																													*/
/*											 main listing												*/
/*__________________________________________________________*/

int main (int argc, char * const argv[]) {
	int carno = 0;
	double mileage = 0.0, miles = 0.0, gas = 0.0;
	ifstream infile;

	// set precision to 2 decimals
	cout << setprecision( 2 ) << setiosflags( ios::fixed | ios::showpoint );

	infile.open("/Developer/Applications/Mac OS X Books/C++ Programming/Read Car Mileage/CarData.txt");

	if (infile.fail()) { 
	cout << "error" << endl;

	exit( 1 );
	}

	cout << endl;
	while (infile >> carno >> miles >> gas) {
		// do mileage calculations
		mileage = miles/gas;
		cout << "\t" << carno << "\t" << miles << "\t" << gas << endl;
		cout << "\tCar #" << carno << " drives " << miles << " miles and uses " << gas << " gallons of gas, " << "which is " << mileage << " miles per gallon." << endl << endl;
	}
	infile.close();
	
	return 0;
}


Hope that helps!
Was This Post Helpful? 1

#15 Omegaclass  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 01-November 09

Re: reading from a file and using the data HELP!

Posted 18 December 2009 - 06:11 PM

thanks for the explanation this relay helped, but what dose the "\t" mean? and where do i go to find the references to this type of notation for example "%d" i sometimes see this in code and don't know understand it.

thanks guys

This post has been edited by Omegaclass: 18 December 2009 - 06:12 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2