Learn how to use getline function and many string member fun
Learn how to use getline() function and many string member functions to process input file one line at a time and display data in groups.
Read ship records from the input file shipRecords.txt
1 \"Royal Queen\" 2015 160
2 \"Carnival\" 2016 1600
1 \"Ocean King\" 2013 110
2 \"Royal Prince\" 2012 2000
2 \"Royal Princess\" 2010 2100
2 \"Royal Caribbean\" 2016 1600
There are 4 columns in the input file. You can assume that there is no data error in the input file. The number of blank characters between two adjacent data can be any.
Column
Comments
1st
Ship type
1 - cargo ship; 2 - cruise ship.
2nd
Name of the ship
It is enclosed by a pair of double quotes
3rd
The year of the ship
4 digits
4th
Cargo capacity or number of passengers
The number indicates the cargo capacity for cargo ship, and number of passengers for cruise ship.
Display a report like the following table. All columns in the report must be lined up properly. The first column Ship Name must be left-adjusted, and the other two columns right-adjusted.
Cargo Ship
===============
Year
Cargo Capacity
Royal Queen
2015
160
Ocean King
2013
110
2 Cargo Ships
Total Cargo Capability 270
Cruise Ship
================
Year
# of Passengers
Carnival
2016
1600
Royal Prince
2012
2000
Royal Princess
2010
2100
Royal Caribbean
2016
1600
4 Cruise Ships
Total # of Passengers 7300
| 1 \"Royal Queen\" 2015 160 |
Solution
Dear Student,
Here is the partial program please wait for some time i am sorting out the issue and will soon update the program,,,,
-------------------------------------------------------------------------------------------------------------------------------------
program...
#include<iostream>
#include<iomanip>
#include<string>
#include<stdlib.h>
#include<fstream>
using namespace std;
int main()
{
int ship_type[10];
string line;
char fname[20];
char lname[20];
int year[10];
int capacity[10];
int Cap =0;
int i =0;
int count = 0;
int c =0;
ifstream infile;
infile.open(\"shipRecords.txt\");
if(!infile)
{
cout<<\"File does not exist\"<<endl;
return 1;
}
//while(!infile.eof())
while(getline(infile, line))
{
infile>>ship_type[i]>>fname[i]>>lname[i]>>year[i]>>capacity[i];
i++;
count++;
}
for(i=0;i<count;i++)
{
if(ship_type[i]==1)
{
c++;
//Cap = Cap + capacity[i];
cout<<setw(20)<<left<<fname[i]<<\" \"<<lname[i]<<setw(20)<<left<<year[i]<<setw(20)<<left<<capacity[i]<<endl;
cout<<setw(60)<<c<<\"Cargo Ships\"<<endl;
//cout<<setw(60)<\"Total Cargo Capability \"<<Cap<<endl;
}
if(ship_type[i]==2)
{
c++;
//Cap = Cap + capacity[i];
cout<<setw(20)<<left<<fname[i]<<\" \"<<lname[i]<<setw(20)<<left<<year[i]<<setw(20)<<left<<capacity[i]<<endl;
cout<<setw(60)<<count<<\"Cruise Ships\"<<endl;
//cout<<setw(60)<\"Total# of Passengers\"<<Cap<<endl;
}
}
//cout<<ship_type<<fname<<lname<<year<<capacity<<endl;
infile.close();
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------
Thanks for your patience...



