How do I pass arrays from the main method in C++ to multiple functions? My progr
ID: 3937423 • Letter: H
Question
How do I pass arrays from the main method in C++ to multiple functions? My program wont compile.
Here is my code.
#include <iostream>
#include <iomanip>
using namespace std;
void input(char name[30], int w, int d);
float Totalcharges(char n[30], int wt, int dt);
main()
{
string fullname[3];
int distance[3];
int weight[3];
for(int i = 1; i <= 3; i++)
{
input(fullname,distance,weight);
}
}
void input(string name[], int d[], int w[])
{
for(int i = 0; i <= 2; i++)
{
cout << "What is your name?" << endl;
getline(cin, name[i]);
cout << "How far are your items going?" << endl;
cin >> d[i];
cout << "How much do your items weigh?" << endl;
cin >> w[i];
while(w[i] < 75)
{
cout << "Sorry we dont ship under 75 lbs" << endl;
cin >> w[i];
}
}
Totalcharges(name, d, w);
}
void Totalcharges(string n[], int wt[], int dt[])
{
float laborcost = 0.0;
float distancecost = 0.0;
float totalcharges = 0.0;
cout<<"Name Weight Distance Labor Charges Travel Charges Total Charges"<<endl;
for(int i = 0; i <= 2; i++)
{
laborcost = (wt[i]/100)*4;
distancecost = 50 + (1.75*dt[i]);
totalcharges = laborcost + distancecost;
cout<<n[i]<<" "<<wt[i]<<" "<<dt[i]<<" "<<laborcost<<" "<<distancecost<<" "<<totalcharges<<endl;
}
}
Explanation / Answer
Please copy paste below program as it is compiling fine.
#include <iostream>
#include<string.h>
#include <iomanip>
using namespace std;
void input(string name[], int d[], int w[]);
void Totalcharges(string n[], int dt[], int wt[]);
int main()
{
string fullname[3];
int distance[3];
int weight[3];
for(int i = 1; i <= 3; i++)
{
input(fullname,distance,weight);
}
return 0;
}
void input(string name[], int d[], int w[])
{
for(int i = 0; i <= 2; i++)
{
cout << "What is your name?" << endl;
getline(cin, name[i]);
cout << "How far are your items going?" << endl;
cin >> d[i];
cout << "How much do your items weigh?" << endl;
cin >> w[i];
while(w[i] < 75)
{
cout << "Sorry we dont ship under 75 lbs" << endl;
cin >> w[i];
}
}
Totalcharges(name, d, w);
}
void Totalcharges(string n[], int dt[], int wt[])
{
float laborcost = 0.0;
float distancecost = 0.0;
float totalcharges = 0.0;
cout<<"Name Weight Distance Labor Charges Travel Charges Total Charges"<<endl;
for(int i = 0; i <= 2; i++)
{
laborcost = (wt[i]/100)*4;
distancecost = 50 + (1.75*dt[i]);
totalcharges = laborcost + distancecost;
cout<<n[i]<<" "<<wt[i]<<" "<<dt[i]<<" "<<laborcost<<" "<<distancecost<<" "<<totalcharges<<endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.