Question: 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.

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

/* This is what is completed so far. Please help:

#include

#include

#include

#include

#include

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"<

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

{

if(ship_type[i]==1)

{

c++;

//Cap = Cap + capacity[i];

cout<

cout<

//cout<

}

if(ship_type[i]==2)

{

c++;

//Cap = Cap + capacity[i];

cout<

cout<

//cout<

}

} //cout<

infile.close();

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!