Programming assignment

Ok, so i have an assignment due at midnight and we just learned this new concept about reading INFILES and creating OUTFILES but im having trouble understanding how this works, let alone how to do my assignment. I am not aking for anyone to do my work, but i do need to understand how it is done.

<Your full name>’s Space Travel Company does temperature conversion from Celsius to Fahrenheit for planets.
The company has a file named “PlanetCelsius.dat” which contains the following information:
 Planet numberinteger
 Planet namestring
 Diameterinteger
 Average Temperature in Celsiusdouble
The formula for converting a temperature from Celsius to Fahrenheit is F = 9C/5 + 32 Write a C++ program that does the following:
 Read in the file, converts Celsius temperature to Fahrenheit equivalent
 Display all information to the monitor including Planet number, Planet name, Diameter, Temperature in Celsius, and Temperature in Fahrenheit
 Find the planet with the highest average temperature, output the hottest planet name and temperature in Celsius
 Find the planet with the lowest average temperature, output the coolest planet name and temperature in Celsius
 Creates an output file called “PlanetFahrenheit.dat” that contains Planet number, Planet name, Diameter and Temperature in Fahrenheit.

PlanetCelsius.dat should look like this: 1 Mercury 4878 117 2 Venus 12104 480 3 Earth 12756 14 4 Mars 6794 -63 5 Jupiter 142984 17 6 Saturn 120536 -130 7 Uranus 51118 -197 8 Neptune 49532 -200 The display should look like this: Number Planet Name Diameter Celsius Fahrenheit 1 Mercury 4878 km 117.00 242.60 //9*(117)/5 + 32
2 Venus 12104 km 480.00 896.00 //9*(480)/5 + 32
3 Earth 12756 km 14.00 … //calculate F
4 Mars 6794 km -63.00 … //calculate F
5 Jupiter 142984 km 17.00 …
6 Saturn 120536 km -130.00 …
7 Uranus 51118 km -197.00 …
8 Neptune 49532 km -200.00 -328.00 //calculate F The hottest planet is …, the temperature is … in Celsius. The coolest planet is …, the temperature is … in Celsius.
infile and outfile may be, instream and outstreams.

read these:
http://www.cplusplus.com/reference/fstream/ifstream/?kw=ifstream
http://www.cplusplus.com/reference/fstream/ofstream/?kw=ofstream

here you would find some examples which help you in opening the files, reading data from files and so on.. let us know if that helped.

eg to read some data.
1
2
3
4
5
6
7
8
ifstream ifs("file path to open the file"); //open file

while(ifs.good())  //while the file is good, run the loop
{
   getline("put your parameters here");   //get one line at a time and do whatever you want with it.
}

ifs.close(); //close the file once the work is done. 



Thanks! now i know my file is being found. here is what i have now

#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;

int main()
{
//ifs and ints
ifstream planetTemperature("PlanetCelsius.dat");
string planets;
int planetNumber;
int planetSize;
double averageCelisus, averageFahrenheit;
//end ifs and ints

//Check to see if file if found

while (planetTemperature >> planets)
{
cout << planets <<endl;
}





system ("PAUSE");
return 0;

I dont understand this=

while(ifs.good()) //while the file is good, run the loop
{
getline("put your parameters here"); //get one line at a time and do whatever you want with it.
}
?
so far i have
how do i imply that?


#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;

int main()
{
//ifs and ints
ifstream planetTemperature("PlanetCelsius.dat");
string planets;
int planetNumber;
int planetSize;
double averageCelisus, averageFahrenheit;
//end ifs and ints

//My display
cout << "Number\t\t" << "Planet Name\t"<< "Diameter\t" << "Celsius\t\t" << "Fahrenheit\t"<<endl;
cout <<"________________________________________________________________________________"<<endl;
//end my display

//Check to see if file if found
while (planetTemperature >> planets)
{
cout << planets <<endl;
}
//end of file found



system ("PAUSE");
return 0;

this means if the file is good.. which includes if the file exists and can be opened, if the file has data.

add this line to your code:

1
2
3
4
5
6
std::string line;
while(ifs.good()) //while the file is good, run the loop
{
getline(ifs, line); //get one line at a time and do whatever you want with it.
std::cout << line;
}


the lines are read in line string object. Now you can manipulate the string according to your requirements.
Topic archived. No new replies allowed.