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?
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
usingnamespace 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;
}
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>
usingnamespace 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;
}