bananaamy
asked on
Arrays Pointers and Functions II
I am trying to learn about pointers and arrays. In this program I playing with several concepts, however, I dont have the funtion right (I only included 1 of several). What am I doing wrong? Thanks much, Annie
#include <iostream>
#include "inputarray.cpp"
/*struct input{double numCostPerPart; int numPublished;
//double numSalCost; int numSold; double numRoyalPercent;
double numHolePrice;};*/
#include "outputarray.cpp"
/*struct output
{double numTotalCost; double numSalValue; double numGrossProfit;
double numRoyalty; double numProfit;};*/
using namespace std;
const int SIZE =3;
void makeCalculations(input ainput[], int nSize);
int main()
{
int n;
input ainput[SIZE]; //declare array input
output aoutput[SIZE]; //decalre array output
for(n=0; n<SIZE; n++) //loops array
{
cout<<endl;
cout<<"Enter cost of parts: ";
cin>>ainput[n].numCostPerP art;
cout<<"Enter number of parts published: ";
cin>>ainput[n].numPublishe d;
cout<<"Enter salvation cost of parts: "; //inputs
cin>>ainput[n].numSalCost;
cout<<"Enter number of parts sold: ";
cin>>ainput[n].numSold;
cout<<"Enter Royalty Percentage: ";
cin>>ainput[n].numRoyalPer cent;
cout<<"Enter wholesale cost of parts: ";
cin>>ainput[n].numHolePric e;
}
cout << endl;
makeCalculations(ainput, 1);
for( n=0; n<SIZE; n++) //loops output array
{
cout << "Total Cost"<<aoutput[n].numTotal Cost<<endl ;
cout <<"Salvation Value" <<aoutput[n].numSalValue<< endl; // outputs
cout <<"Gross Profit" << aoutput[n].numGrossProfit< <endl;
cout <<"Royalty" << aoutput[n].numRoyalty<<end l;
cout <<"Profit" << aoutput[n].numProfit<<endl ;
}
system("pause");
return EXIT_SUCCESS;
}
void makeCalculations(input ainput[], int nSize)
{
double totalCost = 0;
for (int n = 0; n < nSize; ++n)
{
totalCost += ainput[n].numCostPerPart;
}
ainput[n].numCostPerPart* totalCost;
}
#include <iostream>
#include "inputarray.cpp"
/*struct input{double numCostPerPart; int numPublished;
//double numSalCost; int numSold; double numRoyalPercent;
double numHolePrice;};*/
#include "outputarray.cpp"
/*struct output
{double numTotalCost; double numSalValue; double numGrossProfit;
double numRoyalty; double numProfit;};*/
using namespace std;
const int SIZE =3;
void makeCalculations(input ainput[], int nSize);
int main()
{
int n;
input ainput[SIZE]; //declare array input
output aoutput[SIZE]; //decalre array output
for(n=0; n<SIZE; n++) //loops array
{
cout<<endl;
cout<<"Enter cost of parts: ";
cin>>ainput[n].numCostPerP
cout<<"Enter number of parts published: ";
cin>>ainput[n].numPublishe
cout<<"Enter salvation cost of parts: "; //inputs
cin>>ainput[n].numSalCost;
cout<<"Enter number of parts sold: ";
cin>>ainput[n].numSold;
cout<<"Enter Royalty Percentage: ";
cin>>ainput[n].numRoyalPer
cout<<"Enter wholesale cost of parts: ";
cin>>ainput[n].numHolePric
}
cout << endl;
makeCalculations(ainput, 1);
for( n=0; n<SIZE; n++) //loops output array
{
cout << "Total Cost"<<aoutput[n].numTotal
cout <<"Salvation Value" <<aoutput[n].numSalValue<<
cout <<"Gross Profit" << aoutput[n].numGrossProfit<
cout <<"Royalty" << aoutput[n].numRoyalty<<end
cout <<"Profit" << aoutput[n].numProfit<<endl
}
system("pause");
return EXIT_SUCCESS;
}
void makeCalculations(input ainput[], int nSize)
{
double totalCost = 0;
for (int n = 0; n < nSize; ++n)
{
totalCost += ainput[n].numCostPerPart;
}
ainput[n].numCostPerPart* totalCost;
}
Your makeCalculations doesn't store totalCost anywhere.
Try changing...
ainput[n].numCostPerPart* totalCost
...to...
ainput[n].numCostPerPart = totalCost;
Note also that you are passing size as 1 to this fnction in the calling code, which means that it will only operate on the first index of the array.
Try changing...
ainput[n].numCostPerPart* totalCost
...to...
ainput[n].numCostPerPart = totalCost;
Note also that you are passing size as 1 to this fnction in the calling code, which means that it will only operate on the first index of the array.
ASKER
I am trying to learn this languge and have been experimenting with it for about 4 weeks. What I am tring to do is to see how several things work together(pointers,arrays and functions). . I want to enter in data into an "input array" a specific amount of times, I then want to pass the value to a function, preform several calculations, and pass the value of the output of that function to the "output array". I want the output array to display the answers to my calculations. What I cant figure out is how to make my functions work with pointers.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I found many errors in the code. It seems to be written in Dev-C++ (just a thought). In the function why is the line 'ainput[n].numCostPerPart*
Please explain the problem that you are facing. Are you getting wrong results??
Cheers!