C++ program for avg ship capacity.

Hello all,
I have written a C++ program containing arrays, loops, and all other beginner material and am have trouble getting my "getAvgCapacity" function to operate. If anyone has any ideas please let me know and post it!!



//Define all the preprocessor directives.

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

//Define all of the function prototypes that will be used in this program

int getShipData(string [], int, int [], int [], int []);
void showShipCategories(string [], int[], int[], int[], int);
string getCategory(int [], int);
double getAvgCapacity(int [], int);
void showShipCapacity(string [], int, int[]);
int getHighestSpeed(string [], int [], int, int);
void showShipSpeed(string [], int [], int, int);

const int MAX = 30;

//Write the main function

int main()
{
string shipname[MAX];
int i = 0;
int capacity[MAX], tonnage[MAX], speed[MAX];
int numship=0, counter=0, highestspeed;
double avgcapacity;

// Using all of the functions written previously to organize the data and navigate the program.

numship = getShipData(shipname, numship, capacity, tonnage, speed);
showShipCategories(shipname, capacity, tonnage, speed, numship);
showShipCapacity(shipname, numship, capacity);
highestspeed = getHighestSpeed(shipname, speed, numship, highestspeed);
showShipSpeed(shipname, speed, numship, highestspeed);




system ("pause");
return 0;
}

//Use a function to obtain the data from the input file.

int getShipData(string shipname[], int numship, int capacity[], int tonnage[], int speed[])
{
ifstream infile;
infile.open("ships.dat");
int i=0;

//Use a while loop to infile all of the information on the boats.

while (!infile.eof())
{
infile >> shipname[i];
infile >> capacity[i];
infile >> tonnage[i];
infile >> speed[i];
numship++;
i++;
}
return i;
}

//Use a function to display what category the ships are in.

void showShipCategories(string shipname[], int capacity[], int tonnage[], int speed[], int counter)
{
cout << setw(17) << left << "Category" << setw(12) << left << "Name" << setw(10) << left << "Capacity" << setw(10) << left << "Tonnage" << setw(5) << left << "Speed\n";
cout << setw(17) << left << "++++++++" << setw(12) << left << "++++" << setw(10) << left << "++++++++" << setw(10) << left << "+++++++" << setw(5) << left << "+++++\n";
string shipcategory;
for (int i = 0; i < counter; i++)
{
shipcategory = getCategory(tonnage, i);
cout << setw(17) << left << shipcategory << setw(12) << left << shipname[i] << setw(10) << left << capacity[i] << setw(10) << left << tonnage[i] << setw(5) << left << speed[i] << endl;
}

}


//Use the function the obtain the ships categroy

string getCategory(int tonnage[], int i)
{
string shipcategory;

if (tonnage[i] < 48600)
shipcategory = "Empress Family";
else if (tonnage[i] < 74000)
shipcategory = "Sovereign Family";
else if (tonnage[i] < 90100)
shipcategory = "Vision Family";
else if (tonnage[i] < 138000)
shipcategory = "Radiance Family";
else if (tonnage[i] < 160000)
shipcategory = "Voyager Family";
else if (tonnage[i] >= 160000)
shipcategory = "Freedom Family";
return shipcategory;


}

//Use the function to obtain the average capacity from the file

double getAvgCapacity(int capacity [], int numship)
{
int totalcapacity;
double avgcapacity;
for(int i=0; i < numship; i++)
{
totalcapacity = static_cast<double>(totalcapacity) + capacity[i];
}
avgcapacity = totalcapacity / numship;
return avgcapacity;
}

//Use a function to display the average capacity.

void showShipCapacity (string shipname [], int numship, int capacity [])
{
double avgcapacity;
cout << setprecision(2) << fixed << endl << "Average Capacity is " << avgcapacity << endl;
cout << "Ships with above average capacity" << endl;
cout << "+++++++++++++++++++++++++++++++++" << endl;
avgcapacity = getAvgCapacity(capacity, numship);
for (int i=0; i < numship; i++)
{
if(capacity[i] > avgcapacity)
{
cout << shipname[i] << right << capacity[i] << endl;
}
}
cout << endl;
}

//Use a function to get the highest speed for the ships.

int getHighestSpeed(string shipname [], int speed[], int numship, int highestSpeed)
{
for (int i=0; i < numship; i++)
{
if(speed[i] > highestSpeed)
{
highestSpeed = speed[i];
}
}
return highestSpeed;
}

//Use the function to show the highest speeds on the display.

void showShipSpeed(string shipname [], int speed[], int numship, int highestSpeed)
{
cout << "Ships with the highest cruising speed\n";
cout << "+++++++++++++++++++++++++++++++++++++\n";
for (int i=0; i < numship; i++)
{
if (speed[i] == highestSpeed)
{
cout << shipname[i] << "\t" << speed[i] << endl;
}
}
}
Last edited on
You never initialized totalcapacity.

Also, you don't want to do the cast until you are ready for the division.

Oh, and next time, please use code tags. Click on the button that shows "<>" and put your code between the tags.

Edit: cast comment
Last edited on
Topic archived. No new replies allowed.