functons output problem :(

hi friends my problem is tht i am unable to get the desired output of the following code :(
i am trying to calculate the lumber cost for differnt types of woods.
my code is:

#include<iostream>
#include<conio.h>
#include<cmath>
#include <iomanip>
float pine(float,float,float);
float fir(float,float,float);
float cedar(float,float,float);
float maple(float,float,float);
float oak(float,float,float);
int pieces;
float total;
char lumber;
float l; //lenght of the lumber

using namespace std;
int main()
{

cout<<setw(300)<<"welcome to Lumber Company Pvt. Limited"<<"\n"<<endl;
cout<<"To calculate price of the following items, read the instruction carefully."<<endl;
cout<<"Press P for PINE."<<endl;
cout<<"Press F for FIR."<<endl;
cout<<"Press C for CEDAR."<<endl;
cout<<"Press M for MAPLE."<<endl;
cout<<"Press O for OAK."<<endl;
cin>>lumber;
while(lumber==p)
cout<<"please enter the width of the lumber."<<endl;
float thickness;
cin>>thickness;
cout<<"please enter the height of the lumber."<<endl;
float height;
cin>>height;
cout<<"please enter the length of the lumber."<<endl;

cin>>l;

cout<< pine(thickness,height,l)<<endl;
cout<< fir(thickness,height,l)<<endl;
cout<< cedar(thickness,height,l)<<endl;
cout<< maple(thickness,height,l)<<endl;
cout<< oak(thickness,height,l)<<endl;



getch();
return 0;
}
float pine(float thickness,float height,float lenght)
{
switch(lumber){
case'p':
float a=(thickness*l*height)/12;
cout<<"please enter the number of pieces you want to buy."<<endl;
cin>>pieces;
total=pieces*a*0.89;
cout<<"total price for your desired lumber piece is:"<<"$"<<total<<endl;
break;}
return 0;
}

float fir(float thickness,float height,float lenght)
{
switch(lumber){
case'f':
float a=(thickness*l*height)/12;
cout<<"please enter the number of pieces you want to buy."<<endl;
cin>>pieces;
total=pieces*a*1.09;
cout<<"total price for your desired lumber piece is:"<<"$"<<total<<endl;
break;}
return 0;
}
float cedar(float thickness,float height,float lenght)
{
switch(lumber){
case'c':
float a=(thickness*l*height)/12;
cout<<"please enter the number of pieces you want to buy."<<endl;
cin>>pieces;
total=pieces*a*2.26;
cout<<"total price for your desired lumber piece is:"<<"$"<<total<<endl;
break;}
return 0;
}
float maple(float thickness,float height,float lenght)
{
switch(lumber){
case'm':
float a=(thickness*l*height)/12;
cout<<"please enter the number of pieces you want to buy."<<endl;
cin>>pieces;
total=pieces*a*4.50;
cout<<"total price for your desired lumber piece is:"<<"$"<<total<<endl;
break;}
return 0;
}
float oak(float thickness,float height,float lenght)
{
switch(lumber){
case'o':
float a=(thickness*l*height)/12;
cout<<"please enter the number of pieces you want to buy."<<endl;
cin>>pieces;
total=pieces*a*3.10;
cout<<"total price for your desired lumber piece is:"<<"$"<<total<<endl;
break;}
return 0;
}



now problem is tht i am geting 0's for every function in the output except the called function
but i only want the output of called function.
That's because you're returning 0 for every function.

And please use code tags.
i am new in the programing
and i also try to remove the return 0 statement
but same problem
You'll need to return a value, just not 0.

You're probably wanting to return total.
when i remove the return 0 statement then it returns the garbage value :(
what i want is tht i dont the outputs of others funtions that are not called
i just want the output of called functions
So return what you want from the functions.

Like I said, a brief glance at the code would suggest you want to return total.
actually i am getting 5 outputs
1 for the function tht i call and the others outputs from the uncalled functions
i just want the output of called function
a single output only
What? You're calling all five functions.
Last edited on
no i am calling a single function at a time
but it is giving me 5 outputs
You're making no sense.

1
2
3
4
5
cout<< pine(thickness,height,l)<<endl;
cout<< fir(thickness,height,l)<<endl;
cout<< cedar(thickness,height,l)<<endl;
cout<< maple(thickness,height,l)<<endl;
cout<< oak(thickness,height,l)<<endl;


You're calling each of the functions there. Regardless of whether the code inside runs due to your switch assessments (which, as a method of doing things, is pretty poor by the way), you're still returning values.
as i am new in c++
thts why i dont have much sence in making such things
i want tht if the user inputs 'p' then this statement is executed " cout<< pine(thickness,height,l)<<endl;"
f for the second statement,c for the 3rd statement,m for the 4th statement and o for the 5th statement ,.
So a better way to do it would be (you'll need more output, this is a rough example):

In main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
char lumber;
cout << "Enter type of lumber";
cin >> lumber;
switch(lumber)
{
   case 'M':
      cout << maple(thickness, height) << endl;
      break;
   case 'P':
      cout << pine(thickness, height) << endl;
      break;
   // And so on....
   default:
      cout << "Invalid lumber type" << endl;
      break;
}


You may want to format it a bit better than that.
Last edited on
i am really thankful for u help for me :)
and i resolve the problm
actually i am using many switches.
but i use only one switch for function this solve my pblm :)
Topic archived. No new replies allowed.