In C++ and code blocks: I need help getting the correct total shipping cost and
ID: 3573391 • 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 and text files any help would be appreciated.
Data7.txt
10
Chevrolet.... 23 750
Zapata.Co.... 53 375
Schlumberger. 51 1204
FMC.......... 13 675
Buick........ 22 475
Action.Inc... 41 1712
IBM.......... 32 979
Apple.Comp... 42 790
Zapata.Co.... 52 443
Univ..Mich... 12 1960
table7.txt
1.45 1.65 1.75
1.60 1.75 1.85
1.75 1.85 1.90
2.10 2.15 2.35
2.19 3.19 4.19
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<<"------------- "<<"---- "<<"---- "<<"------ "<<"-------"<<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]<<endl;
cout<<"Total weight = "<<totalWeight[0]<<endl;
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<col;c++)
{
//cout<<coltotal[c]<<" ";
//cout<<" ";
cout<<"Total shipping cost= "<<coltotal[c]<<endl;
//cout<<"Total weight = "<<coltotal[c]<<endl;
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<<" "
<<setw(40)<< "The End"
<< " ";
}
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);
//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= "<<totalCost[5]<<endl;
//cout<<"Total weight = "<<totalWeight[5]<<endl;
//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<<" "
<<co_array[i].weight<< endl;
}
}
Explanation / Answer
Solution:
Corrected the for loop in Disp_inf method to correctly calculate the total cost and weight. Corrected the same problem in the last for loop at the end of main function
Complete Program:
-----------------------------------------------------------------------------------------------------------------------------------------------------
#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 = 0;
int weight = 0;
//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;
weight+=Arr_comp[i].weight;
cost+=Arr_comp[i].cost;
}
cout<<"------------- "<<"---- "<<"---- "<<"------ "<<"-------"<<endl;
cout<<"Total shipping cost = "<<cost<<endl;
cout<<"Total weight = "<<weight<<endl;
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<col;c++)
{
//cout<<coltotal[c]<<" ";
//cout<<" ";
cout<<"Total shipping cost= "<<coltotal[c]<<endl;
//cout<<"Total weight = "<<coltotal[c]<<endl;
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<<" "
<<setw(40)<< "The End"
<< " ";
}
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;
double cost = 0;
int weight = 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);
//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;
cost += co_array[i].cost;
weight += co_array[i].weight;
}
}
cout<<"------------- "<<"---- "<<"---- "<<"------ "<<"-------"<<endl;
totalWeight[5]+=co_array[5].weight;
totalCost[5]+=co_array[5].cost;
cout<<"Total shipping cost= "<<cost<<endl;
cout<<"Total weight = "<<weight<<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<<" "
<<co_array[i].weight<< endl;
}
}