Read In .dat file contents and

Quite lost on current school programming assignment. Looking for assistance to guide me to solve the problem. The assignment has me read in data from a .dat file (Planet number, name, diameter, temp in Celsius). Then I'm to output to display in aligned format and also convert Celsius to Fahrenheit. Lastly, determine hottest and coldest planets and obviously output to display and output to different file name.

When I've completed the major parts of the program I'll add the output file...etc and understand how to do that.

This is what I have so far. I'm not sure why the output isn't aligning exactly the way I want and a little lost how to test to find the coldest planet. Should i use if/else statements or a loop to determine this?

Thanks in advance for any pointers and help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>

using namespace std;
int main() { 

// Variables to hold values 
int planNbr;
string planetName;
int diameter = 0;
double celsius = 0;
double fahrenheit = 0; 

// Open .dat file that contains data for future display and calculations
ifstream inputFile;
inputFile.open("C:\\Users\\Snoopy\\Documents\\Pass7\\PlanetCelsius.dat"); // use double backslashes

// Output information to display. The rest of the read in file will align underneath

cout<<"Number"<<setw(15)<<"Planet Name"<<setw(15)<<"Diameter"<<setw(15)<<"Celsius"<<setw(15)<<"Fahrenheit"<<endl; 	

// Test for file open error
if(inputFile.fail()) {
cout<<"File did not open."<<endl; }

// Use while statement to test condition
while(inputFile>>planNbr>>planetName>>diameter>>celsius) {

// Calculation from Celsius to Fahrenheit
fahrenheit = (9*celsius)/5 + 32;
cout<<planNbr<<setw(15)<<planetName<<setw(15)<<diameter<<setw(15)<<celsius<<setw(15)<<fahrenheit<<endl; }

inputFile.close(); 

system("Pause");
return 0;
}
the output isn't aligning exactly the way I want

i'm not sure how exactly you want it to look like but this is the desired output for me:

cout << left << setw(10) << "NUMBER" << setw(15) << "PLANET NAME" << setw(15) << "DIAMETER" << endl; ...

Your cout statements will have conflicts because the default alignment of setw is to the right, you should
use std::left manipulator instead

Remember that setw() sets the field width ( or the maximum width the next ouput will consume ) , i'm sorry if i can't explain
clearly right now because i am so sleepy so i made a simple example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
    int nplanetNumber = 50;
    char szPlanetName[] = "Jupiter";
    int ndiameter = 139822; // In KM
    
    cout << left << setw(10) << "NUMBER"      << setw(15) << "PLANET NAME" << setw(15) << "DIAMETER(km)" << endl << endl;
    cout << left << setw(10) << nplanetNumber << setw(15) << szPlanetName  << setw(15) << ndiameter << endl;
        
    return 0;
}
Topic archived. No new replies allowed.