trouble getting functions to work right

i cant get it to print the area, it basically just prints the number 1 for no reason, also i am having issues with my functions, can anyone help explain why you have to put" int ComputeArea( int length, int width)" instead of putting int ComputeArea( int length * int width) Thanks for any help anyone can provide i really appreciate it




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//*********************************************************************
//File name PaintRoom
// 

//Description: This program is given the length and width of a region to
//be painted, and the price of a gallon of paint as input. The square
//footage of the region is calculated, and the number of gallons of paint
//needed to cover the region, and the cost is determined. One gallon of
//paint will cover 200 square feet of the region.
//*********************************************************************

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std ;


// function prototypes
int GetLength();
int GetWidth();
double GetGallonCost();
int ComputeArea( int, int );
int FindGallons( int );
double ComputeCost( double, int );
void PrintResults( int, int, double );

// global constants
const int COVERAGE = 200;       // 1 gallon of paint will cover 200 sq. ft.


int main()
  {
    // local variable declarations for main
    int   length;      // length of the region to be covered
    int   width;       // width of the region to be covered
    int   area;        // area of the region to be covered; long int
                       // would be required to store large areas
    int   gallons;     // number of gallons needed to cover the region
    double gallon_cost; // price of a gallon of paint
    double paint_cost;  // the cost of the paint needed

    // input data
    length      = GetLength();
  
    width       = GetWidth();
    
    gallon_cost = GetGallonCost();
    
    
    // process data
    area       = ComputeArea( length, width );
    
    gallons    = FindGallons( area );   // find number of gallons to
    
    
    paint_cost = ComputeCost( gallon_cost, gallons );  // compute cost of paint

    // print results
    PrintResults( gallons, area, paint_cost );

    system ("pause");
    return(0);
  }



//**********************************************************************
//Function Name: GetLength
// Prompts the user to input the lenth of a rectangular room
// Returns the int value input by the user
//**********************************************************************
int length;
int GetLength()
  {  
    cout << "Length?\n";
    cin >> length;
    return 1;
  }


//*********************************************************************
//Function Name: GetWidth
// Prompts the user to input the width of a rectangular room
// Returns the int value input by the user
//**********************************************************************
int width;
int GetWidth()
  {
    cout << "Width?\n";
    cin >> width;
  return 1;
  }


//**********************************************************************
//Function Name: GetGallonCost
//Prmpts the user to input the cost of a gallon of paint
//Returns the value input by the user

//**********************************************************************
double gallon_cost;
double GetGallonCost()
  {
    cout << "Cost per gallon of paint?\n";
    cin >> gallon_cost;
   return 1.0;
  }


//**********************************************************************
//Function Name: ComputeArea
// Formal parameters are the length and width of a rectangle
// Calculates and returns the rectangle area

//**********************************************************************
int area;
int ComputeArea( int length, int width)

{
  area = length * width;
  cout << area;
    return 1;
        
     
}


//**********************************************************************
//Function Name: FindGallons

//Calculates and returns the number of gallons of paint needed 
// Formal parameter is the area to be painted
// Global constant COVERAGE tells the number of square feet
// covered by one gallon
// Calculates and returns the number of gallons
// Paint must be purchased by the gallon so if (Area / COVERAGE)
// yields a remainder, round up to the highest integer

//**********************************************************************
int FindGallons( int paint_area )
{
    
   cout << area / COVERAGE;
   if (COVERAGE <= 200)
   COVERAGE =1
   else if (COVERAGE > 200 && COVERAGE <= 400)
   COVERAGE =2
 return 1;
}


//**********************************************************************
//Function Name: ComputeCost
//Computes how much it would cost to buy all the paint necessary
//to cover the region.
// Formal parameters are the cost per gallon and the number of gallons 
// of paint needed


//**********************************************************************
double ComputeCost( double gallon_price, int num_gallons )
{
 return 1.0;
}


//**********************************************************************
//Function Name: PrintResults
// A void function
// Formal parameters are the number of gallons, the area to be painted,
// and the total cost of the paint required.
// Prints a message to the screen stating the number of gallons,
// the area, and the post of the piant.

//**********************************************************************
void PrintResults( int gallons, int area, double paint_cost )
  {

  }


// end of function declarations
First problem:

Very weird variable declaration style.

1
2
3
4
5
6
7
8
9
10
int area;
int ComputeArea( int length, int width)

{
  area = length * width;
  cout << area;
    return 1;
        
     
}


area will here be a global variable, though you probably wanted it to be a local one. Declare local variables inside the function body. Also, this function always returns 1- it should return area.
now i tried changing return 1; to return area;, still cant get it to output the area

1
2
3
4
5
6
7
8
9
10
11
int ComputeArea( int length, int width)

{
    int area;
  area = length * width;
  
    return area;

}

Well yeah, I can tell you why.

1
2
3
4
void PrintResults( int gallons, int area, double paint_cost )
  {

  }


The reason is your output function is completely empty.
Topic archived. No new replies allowed.