Function, Array, and Loop help for final?

Hello all. I need help with this program for my C++ final due tonight. I am able to do the functions, loops, and arrays just fine. When I enter the tree information, when I compile and run the program, it only displays the last tree data entry three time:( Can anyone help me with this problem and/or know where I am going wrong? You can search the problem by using C++ Forest Inventory on Google since it si too long to post here.

My code:

#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>

using namespace std;

// function prototypes
int getTreeNo();
int getSpeciesCode(int[],string[]);
double getDbh();
int getTotHt();
double calcTotVol(int,int[],double[],double[],double, int);
void displayTreeData(ofstream&,int ,int ,int [], string [],double,int,double);

int main()
{

int treeNo;
int speciesCode;
int totalHt;
int noTrees;
double dbh;
double totalVol;
double avgTotVol;

int Species[6]={11,12,13,21,22,23};
string speciesDesc[6]={"Loblolly Pine","White Pine","Red Pine","White Oak","Red Oak","Other Oak"};
double b0[6]={1.2446,0.000,2.0822,.7316,1.6378,.7554};
double b1[6]={.002165,.002364,.002046,.001951,.002032,.002174};
noTrees=0;
avgTotVol=0.0;

// open the outdata
ofstream out("report.dat");
out<<"Forest Inventory Detail Edit and Volume Calculation\n"
<<"Tree No. Species Description DBH Tot. Ht. Tot. Vol.";

cout << setiosflags(ios::fixed)<<setprecision(3);
cout << setiosflags(ios::fixed)<<setprecision(3);

do
{
treeNo=getTreeNo();
if(treeNo!=999)
{
speciesCode=getSpeciesCode(Species,speciesDesc);
dbh=getDbh();
totalHt=getTotHt();

totalVol=calcTotVol(speciesCode,Species,b0,b1, dbh, totalHt);


noTrees++;
avgTotVol+=totalVol;

displayTreeData(out,speciesCode,treeNo,Species,speciesDesc,dbh,totalHt,totalVol);

}
}
while(treeNo!=999);


cout<<"\n\nTotal trees measured = "<< noTrees <<"\nAverage total volume = ";

out<<"\n\nTotal trees measured = "<<noTrees <<"\nAverage total volume = ";

if(noTrees<=0)
{
cout<<0;
out<<0;
}

else
{
cout<<avgTotVol/noTrees;
out<<avgTotVol/noTrees;
}


out.close();
return 0;
}

//Now get the tree numbers..

int getTreeNo()
{
int input1;
do
{
cout<<"Enter a tree number between 1 and 199. 999 will finish inputs: ";
cin>>input >>input >>input;
if((input<1||input>199)&&input!=999)
{
cout<<"Invalid number entered.\n";
}
}
while((input<1||input>199)&&input!=999);


return input, input, input;
}


//get the species code

int getSpeciesCode(int Table[],string Name[]){
int code;
int found=0;

do
{
cout<<"\n\nCode\tDescription\n";

for(int i=0;i<6;i++)
{
cout<<Table[i]<<"\t"<<Name[i]<<endl;
}


cout<<"\n Enter Species Code: ";
cin>>code >>code >>code;

//search the Table to see if a valid code has been entered
for(int j=0;j<6;j++)
{
if(code==Table[j])
{
found = 1;
break;
}
}

if(found!=1){
cout<<"An invalid code has been entered.\n";
}
}
while(found!=1);

return code, code, code;
}


//get the Dbh

double getDbh(){
double inches;
do
{
cout<<"Enter the number of inches, which is a number between 5 and 50.6: ";
cin>>inches >>inches >>inches;
if(inches<5||inches>50.6){
cout<<"Please enter a valid number. \n";
}
}
while(inches<5||inches>50.6);

return inches, inches, inches;

}


//get the total height

int getTotHt(){
int Ht;
do
{
cout<<"Enter the closest even total height between 24 & 160: ";
cin>>Ht >>Ht >>Ht;
if((Ht<1||Ht>199)&&Ht%2!=0){
cout<<"An invalid number has been entered. The number also has to be an even number.\n";
}
}
while((Ht<1||Ht>199)&&Ht%2!=0);

return Ht, Ht, Ht;
}


//calculate the toal volume

double calcTotVol(int code,int table[],double b0[],double b1[],double dbh, int totalHt){
int index;
double totalVol;

//find index
for(int i=0;i<6;i++){
if(code==table[i]){
index=i;
break;
}
}

//calculate the totalVol. totVol is gotten by using (totVol = b0 + b1 (dbh)^2 ( totalHt))
totalVol=b0[index]+b1[index]*dbh*dbh*totalHt;

return totalVol;

}



void displayTreeData(ofstream& out,int speciesCode,int treeNo,int table[], string Name[],double dbh,int totalHt,double totalVol){
int index;

//now you need to find the correct index
for(int i=0;i<6;i++){
if(speciesCode==table[i]){
index=i;
break;
}
}

cout<<"\n\nTree No. Species Description DBH Tot. Ht. Tot. Vol.";
cout<<endl<<treeNo<<" "<<Name[index]<<" "<<setprecision(1)<<dbh<<" "<<totalHt<<" "<<setprecision(3)<<totalVol<<endl;
out<<endl<<treeNo<<" "<<Name[index]<<" "<<setprecision(1)<<dbh<<" "<<totalHt<<" "<<setprecision(3)<<totalVol;
cout<<endl<<treeNo<<" "<<Name[index]<<" "<<setprecision(1)<<dbh<<" "<<totalHt<<" "<<setprecision(3)<<totalVol<<endl;
out<<endl<<treeNo<<" "<<Name[index]<<" "<<setprecision(1)<<dbh<<" "<<totalHt<<" "<<setprecision(3)<<totalVol;
cout<<endl<<treeNo<<" "<<Name[index]<<" "<<setprecision(1)<<dbh<<" "<<totalHt<<" "<<setprecision(3)<<totalVol<<endl;
out<<endl<<treeNo<<" "<<Name[index]<<" "<<setprecision(1)<<dbh<<" "<<totalHt<<" "<<setprecision(3)<<totalVol;
return;
}
that is because you are reading three values in same variable cin>>input >>input >>input;, cin>>code >>code >>code; (2 more like this).

For reading three different values you need to declare and use three different variables
cin>>inputOne >>inputTwo >>inputThree;
Topic archived. No new replies allowed.