In C++ and code blocks: I need help getting the correct total shipping cost and
ID: 3572965 • Letter: I
Question
In C++ and code blocks:
I need help getting the correct total shipping cost and total weight to print out with each of the 4 records.
Below is the code I have finished any help would be appreciated.
Code:
#include <iomanip>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cctype>
#include <cmath>
using namespace std;
//The structure declaration
struct company{
char name[14];
int zonearea;
int weight;
double cost;
};
int totalWeight[10]={0};
double totalCost[10]={0};
//int coltotal[5]={0};
int col;
int row;
void readtable(ifstream &f2, float table[][4]);
void readdata(ifstream &f1, company co_array[], int & header);
}
//display info from company struct
void Disp_inf(company Arr_comp[], int Str_head,float data_tab[][4])
{
double cost;
int weight;
//int i,c,r;
//int col;
cout << "NAME Zone Area Weight Cost" << endl;
cout<<"------------- "<<"---- "<<"---- "<<"------ "<<"-------"<
for (int i = 0; i < Str_head; i++)
cout << Arr_comp[i].name << " " << Arr_comp[i].zonearea/10 << " " << Arr_comp[i].zonearea%10
<< " " << Arr_comp[i].weight << " " << Arr_comp[i].cost << endl;
cout<<"------------- "<<"---- "<<"---- "<<"------ "<<"-------"<
totalWeight[0]+=Arr_comp[0].weight;
totalCost[0]+=Arr_comp[0].cost;
cout<<"Total shipping cost = "< cout<<"Total weight = "< cout << endl << endl;
}
/*void col_total(int x [10][5],int coltotal[5],int row,int col)
{ int c = 0;
for(int c = 0; c < col; c++)
{for(int r = 0; r < row; r++)
coltotal[c]+=x[r][c];}
cout<<" Totals= ";
for(int c = 0;c {
//cout<
cout<<"Total shipping cost= "< //cout<<"Total weight = "< cout << endl << endl;}
}*/
//sorted by name in alphabetical order
void name_sort(company Arr_comp[], int Str_head)
{
for (int i = 0; i < Str_head; i++)
{
for (int j = i+1; j < Str_head; j++)
{
if (strcmp(Arr_comp[i].name,Arr_comp[j].name) == 1)
{
company temp = Arr_comp[i];
Arr_comp[i] = Arr_comp[j];
Arr_comp[j] = temp;
}
}
}
}
//sorted by weight function
void sort_weight(company Arr_comp[], int Str_head)
{
for (int i = 0; i < Str_head; i++)
{
for (int j = i+1; j < Str_head; j++)
{
if (Arr_comp[i].weight > Arr_comp[j].weight)
{
company temp = Arr_comp[i];
Arr_comp[i] = Arr_comp[j];
Arr_comp[j] = temp;
}
}
}
}
// footer function
void footer()
{
// display the end
cout<<" "
< << " ";
}
int main()
{
// set precision
cout << setiosflags(ios::showpoint|ios::fixed);
cout.precision(2);
float table [6][4]= {0};
company co_array[30];
int header = 0;
//int x [10][5];
// initialize the array of structure
for(int i = 0; i < 30; i++)
{
strcpy ( co_array[i].name, " ");
co_array[i].zonearea = 0;
co_array[i].weight = 0;
co_array[i].cost = 0.0;
}
//locate data7.txt and table7.txt
ifstream f1 ("h:\data7.txt", ios::in);
ifstream f2 ("h:\table7.txt",ios::in);
//call heading
//heading();
//read data from file to calculate shipping costs
readtable(f2,table);
//read data
readdata(f1, co_array , header);
for (int i = 0; i < header; i++)
co_array[i].cost = co_array[i].weight*table[co_array[i].zonearea/10][co_array[i].zonearea%10];
cout<<" ";
//call disp_info function
Disp_inf(co_array,header,table);
// col_total(x,coltotal,10,5);
//call name_sort function
name_sort(co_array,header);
//call disp_info function
Disp_inf(co_array,header,table);
//call sort_weight function
sort_weight(co_array,header);
//call disp_info function
Disp_inf(co_array,header,table);
//display a report of companies in zone 2
cout << "NAME Zone Area Weight Cost" << endl;
cout<<"------------- "<<"---- "<<"---- "<<"------ "<<"-------"< for (int i = 0; i < header; i++)
{
if (co_array[i].zonearea%10 == 2)
cout << co_array[i].name << " " << co_array[i].zonearea/10 << " " << co_array[i].zonearea%10
<< " " << co_array[i].weight << " " << co_array[i].cost << endl;
}
cout<<"------------- "<<"---- "<<"---- "<<"------ "<<"-------"< //totalWeight[5]+=co_array[5].weight;
//totalCost[5]+=co_array[5].cost;
//cout<<"Total shipping cost= "< //cout<<"Total weight = "< //cout << endl << endl;
//close files
f1.close();
f2.close();
//display footer function
footer();
return 0;
}
// function to read the table
void readtable( ifstream &f2, float table[][4])
{ cout<<" table7.txt file ";
for(int row =1; row < 6; row ++)
{
for (int col = 1; col < 4; col++)
{ f2>> table[row] [col];
cout<< table [row] [col]<< " ";
}
cout<< endl;
}
}
// read header record
void readdata(ifstream &f1, company co_array[], int & header)
{
f1>>header;
cout<<" header record for data7.txt = "<< header<<" ";
for( int i = 0; i < header; i++)
{ f1>> co_array[i].name
>> co_array[i].zonearea
>> co_array[i].weight;
cout<< co_array[i].name <<" "
<< co_array[i].zonearea<<" "
<
}
}
Explanation / Answer
#include <iomanip>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cctype>
#include <cmath>
using namespace std;
//The structure declaration
struct company{
char name[14];
int zonearea;
int weight;
double cost;
};
int totalWeight[10]={0};
double totalCost[10]={0};
//int coltotal[5]={0};
int col;
int row;
void readtable(ifstream &f2, float table[][4]);
void readdata(ifstream &f1, company co_array[], int & header);
//display info from company struct
void Disp_inf(company Arr_comp[], int Str_head,float data_tab[][4])
{
double cost;
int weight;
//int i,c,r;
//int col;
cout << "NAME Zone Area Weight Cost" << endl;
cout<<"------------- ---- ---- ------ -------"<<endl;
for (int i = 0; i < Str_head; i++)
cout << Arr_comp[i].name << " " << Arr_comp[i].zonearea/10 << " " << Arr_comp[i].zonearea%10
<< " " << Arr_comp[i].weight << " " << Arr_comp[i].cost << endl;
cout<<"------------- "<<"---- "<<"---- "<<"------ "<<"-------"<<endl;
totalWeight[0]+=Arr_comp[0].weight;
totalCost[0]+=Arr_comp[0].cost;
cout<<"Total shipping cost = "<<totalCost[0]; cout<<"Total weight = "<<totalWeight[0]; cout << endl << endl;
}
/*void col_total(int x [10][5],int coltotal[5],int row,int col)
{ int c = 0;
for(int c = 0; c < col; c++)
{for(int r = 0; r < row; r++)
coltotal[c]+=x[r][c];}
cout<<" Totals= ";
for(int c = 0;c {
//cout<
cout<<"Total shipping cost= "< //cout<<"Total weight = "< cout << endl << endl;}
}*/
//sorted by name in alphabetical order
void name_sort(company Arr_comp[], int Str_head)
{
for (int i = 0; i < Str_head; i++)
{
for (int j = i+1; j < Str_head; j++)
{
if (strcmp(Arr_comp[i].name,Arr_comp[j].name) == 1)
{
company temp = Arr_comp[i];
Arr_comp[i] = Arr_comp[j];
Arr_comp[j] = temp;
}
}
}
}
//sorted by weight function
void sort_weight(company Arr_comp[], int Str_head)
{
for (int i = 0; i < Str_head; i++)
{
for (int j = i+1; j < Str_head; j++)
{
if (Arr_comp[i].weight > Arr_comp[j].weight)
{
company temp = Arr_comp[i];
Arr_comp[i] = Arr_comp[j];
Arr_comp[j] = temp;
}
}
}
}
// footer function
void footer()
{
// display the end
cout<<" "<< " ";
}
int main()
{
// set precision
cout << setiosflags(ios::showpoint|ios::fixed);
cout.precision(2);
float table [6][4]= {0};
company co_array[30];
int header = 0;
//int x [10][5];
// initialize the array of structure
for(int i = 0; i < 30; i++)
{
strcpy ( co_array[i].name, " ");
co_array[i].zonearea = 0;
co_array[i].weight = 0;
co_array[i].cost = 0.0;
}
//locate data7.txt and table7.txt
ifstream f1 ("h:\data7.txt", ios::in);
ifstream f2 ("h:\table7.txt",ios::in);
//call heading
//heading();
//read data from file to calculate shipping costs
readtable(f2,table);
//read data
readdata(f1, co_array , header);
for (int i = 0; i < header; i++)
co_array[i].cost = co_array[i].weight*table[co_array[i].zonearea/10][co_array[i].zonearea%10];
cout<<" ";
//call disp_info function
Disp_inf(co_array,header,table);
// col_total(x,coltotal,10,5);
//call name_sort function
name_sort(co_array,header);
//call disp_info function
Disp_inf(co_array,header,table);
//call sort_weight function
sort_weight(co_array,header);
//call disp_info function
Disp_inf(co_array,header,table);
//display a report of companies in zone 2
cout << "NAME Zone Area Weight Cost" << endl;
cout<<"------------- "<<"---- "<<"---- "<<"------ "<<"-------"<<endl;
for (int i = 0; i < header; i++)
{
if (co_array[i].zonearea%10 == 2)
cout << co_array[i].name << " " << co_array[i].zonearea/10 << " " << co_array[i].zonearea%10
<< " " << co_array[i].weight << " " << co_array[i].cost << endl;
}
cout<<"------------- "<<"---- "<<"---- "<<"------ "<<"-------"<<endl; //totalWeight[5]+=co_array[5].weight;
//totalCost[5]+=co_array[5].cost;
//cout<<"Total shipping cost= "< //cout<<"Total weight = "< //cout << endl << endl;
//close files
f1.close();
f2.close();
//display footer function
footer();
cin>>header;
return 0;
}
// function to read the table
void readtable( ifstream &f2, float table[][4])
{ cout<<" table7.txt file ";
for(int row =1; row < 6; row ++)
{
for (int col = 1; col < 4; col++)
{ f2>> table[row] [col];
cout<< table [row] [col]<< " ";
}
cout<< endl;
}
}
// read header record
void readdata(ifstream &f1, company co_array[], int & header)
{
f1>>header;
cout<<" header record for data7.txt = "<< header<<" ";
for( int i = 0; i < header; i++)
{ f1>> co_array[i].name
>> co_array[i].zonearea
>> co_array[i].weight;
cout<< co_array[i].name <<" "<< co_array[i].zonearea<<" "<<endl;
}
}