Help with Program functions

Write your question here.

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
 #include <iostream>
#include <iomanip>

// Function prototypes
//getLength,getWidth, getArea, displayData
int getLength(int L);
int getWidth(int W);
int getArea(int l, int w);
void DisplayData(int);

using namespace std;

int main()
{
    //variable declaration
    // To hold the rectangle's length
    int length;
    // To hold the rectangle's width
    int width;
    // To hold the rectangle's area
    int RectangleArea;




//---------------------------------------------------





    // Call function --Get the rectangle's length.
    getLength(length);

    //Call function - Get the rectangle's width.
    getWidth(width);

    //Call function - Get the rectangle's area.
    getArea(length, width);

    //Call function - Display the rectangle's data.
    //DisplayData(DisplayData);

    return 0;
}


//***************************************************
// getLength function                               *
//***************************************************
int getLength(int){
    int length;
    // Get the length.
    cout << "Enter the length: "; cin >> length;
    cout<<"The length is "<<length<<endl;


}

//***************************************************
// getWidth function                                *
//***************************************************
int getWidth(int) {
    int width;
// Get the width.
    cout << "Enter the width: "; cin >> width;
    cout<<"The width is "<<width<<endl;

}

//***************************************************
// getArea function                                 *
//***************************************************
int getArea(int l, int w){
    int RectangleArea=l*w;
    cout << "Area: "<< RectangleArea;

    return RectangleArea;

}


i dont know why when input numbers like 4 for length and 5 for width i get some crazy number for area like: 1177310420

any ideas? suggestions?
Last edited on
Does this happen with other numbers? Have you tried running it through the debugger? But just looking at it I don't see an issue. Also with your function declarations at the bottom, you should have your parameters there as well.
Last edited on
- getLength(int) is called from main. The value passed into it is not used.
- a local variable inside getLength is created.
- A value for this data is entered.
- The function then ends, and this value no longer exists.
- The variable called "length" inside main is still uninitialized at this point.

Possible fix, is to pass the variable by reference. Note I changed the return type.
1
2
3
4
5
6
7
void getLength(int& length){

    // Get the length.
    cout << "Enter the length: "; cin >> length;
    cout<<"The length is "<<length<<endl;

}


Calling code stays the same.

Another possible fix it to return a value, changing the return type back to int
1
2
3
4
5
6
7
int getLength(){
    int length;
    // Get the length.
    cout << "Enter the length: "; cin >> length;
    cout<<"The length is "<<length<<endl;
    return length;
}


Calling code changes to: int length = getLength();

Similar issue with width, of course.
Last edited on
yea, ive tried with a different bunch of numbers. but it always gives me some crazy long number. and I really have no clue as to why.
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
 
#include <iostream>
#include <iomanip>
// Function prototypes
//getLength,getWidth, getArea, displayData
int getLength(int L);
int getWidth(int W);
int getArea(int length, int width);
void DisplayData();
using namespace std;


int main()
{
//variable declaration
// To hold the rectangle's length
    int length;
// To hold the rectangle's width
    int width;
//---------------------------------------------------
// Call function --Get the rectangle's length.
    length = getLength(length);
//Call function - Get the rectangle's width.
    width = getWidth(width);
//Call function - Get the rectangle's area.
    getArea(length, width);
//Call function - Display the rectangle's data.
    DisplayData();
    return 0;
}

//***************************************************
// getLength function *
//***************************************************
int getLength(int){
    int length;
// Get the length.
    cout << "Enter the length: "; cin >> length;
    cout<<"The length is "<<length<<endl;
    return length;
}

//***************************************************
// getWidth function *
//***************************************************
int getWidth(int) {
    int width;
// Get the width.
    cout << "Enter the width: "; cin >> width;
    cout<<"The width is "<<width<<endl;
    return width;
}

//***************************************************
// getArea function *
//***************************************************
int getArea(int l, int w){
    int RectangleArea=l*w;
    cout << "Area: "<<RectangleArea<<"\n"<<endl;
    return RectangleArea;
}

void DisplayData(){
    cout<<"Rectangle Data\n";
    cout<<"--------------\n";
    cout<<"Length:"<<setw(2)<<getLength<<endl;
    cout<<"Width:"<<setw(3)<<getWidth<<endl;
    cout<<"Area: "<<setw(3)<<getArea<<endl;
}


i got it working in the sense that it takes the values and displays it in the first part. but then when going to the DisplayData function

How do i make it so that i can cout the value for length, width and area?
with the code like this, it just output 1 for everything.

Last edited on
first, you are NOT calling the functions in display data. You need (parameter) notation, eg cout << "length " << getlength(unclearvalue) << endl;

and second, you just discovered the problem with embedding print statements in functions that do something else as their main feature.

its going to generate all sorts of fun I/O when you do this.

maybe you need this flow in main...
main()
{
int length = getLength(whatisthis);
int width = getWidth(orthis);
int area = getArea(length, width);
displayData(length, width, area); //I added parameters to give you a hint

with the idea being that main glues it all together. You get the input, process the input to get a result, and display it all up pretty, in that order, using the local variables in main to hold intermediate values.

you want to change area to not print anything, most likely, since displaydata prints your info (???).

does this make sense?
Last edited on
ok so i should put getLength() but what goes inside the ()?
im confused...
hmm... i think im starting to get it now.
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
#include <iostream>
#include <iomanip>
// Function prototypes
//getLength,getWidth, getArea, displayData
int getLength(double L);
int getWidth(double W);
int getArea(double length, double width);
int DisplayData(double, double, double);
using namespace std;


int main()
{
//variable declaration
// To hold the rectangle's length
    int length;
// To hold the rectangle's width
    int width;
// To hold the rectangle's area
    int area;
//---------------------------------------------------
// Call function --Get the rectangle's length.
    length = getLength(length);
//Call function - Get the rectangle's width.
    width = getWidth(width);
//Call function - Get the rectangle's area.
    area = getArea(length, width);
//Call function - Display the rectangle's data.
    DisplayData(length, width, area);
    return 0;
}

//***************************************************
// getLength function *
//***************************************************
int getLength(double){
// store length inside function
    int length;
// Get the length.
    cout << "Enter the length: "; cin >> length;
    cout<<"The length is "<<length<<endl;
    return length;
}

//***************************************************
// getWidth function *
//***************************************************
int getWidth(double) {
// store width inside function
    int width;
// Get the width.
    cout << "Enter the width: "; cin >> width;
    cout<<"The width is "<<width<<endl;
    return width;
}

//***************************************************
// getArea function *
//***************************************************
int getArea(double l, double w){
//  produce the rectangle area
    int RectangleArea=l*w;
    cout << "Area: "<<RectangleArea<<"\n"<<endl;
    return RectangleArea;
}

//***************************************************
// DisplayData function *
//***************************************************
int DisplayData(double, double, double){
    cout<<"Rectangle Data\n";
    cout<<"--------------\n";
    cout<<"Length:"<<setw(2)<<length<<endl;
    cout<<"Width:"<<setw(3)<<width<<endl;
    cout<<"Area: "<<setw(3)<<area<<endl;
}


something like this?

even tho my compiler says no matching function for call to 'DisplayData'
and length width and area say "use of undeclared identifier"
Last edited on
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
#include <iostream>

double get_length() ;
double get_width() ;
double compute_area( double length, double width ) ;
void display_result( double length, double width, double area ) ;

int main()
{
    // const: we have no intention of modifying these values later
    const double length = get_length() ;
    const double width = get_width() ;
    const double area = compute_area( length, width ) ;

    display_result( length, width, area ) ;
}

// helper function to accept a number greater than zero from the user
// and return it (used by both get_length and get_width)
static double get_positive_number( const char* prompt )
{
    std::cout << prompt << " (enter a positive number): " ;
    double number ;
    if( std::cin >> number ) // if the user entered a number
    {
        if( number > 0 ) return number ; // valid input, return the number
        else std::cout << "please enter a value greater than zero\n" ;
    }
    else // user did not enter a number (you may want to ignore this part for now)
    {
        std::cout << "that input is not a number\n" ;
        std::cin.clear() ; // clear the failed state
        std::cin.ignore( 1000, '\n' ) ; // discard the bad input
    }

    return get_positive_number( prompt ) ; // there was an input error: try again
}

double get_length() { return get_positive_number( "length" ) ; }
double get_width() { return get_positive_number( "width" ) ; }

double compute_area( double length, double width ) { return length * width ; }

void display_result( double length, double width, double area )
{
    std::cout << "length: " << length << '\n'
              << " width: " << width  << '\n'
              << "  area: " << area << '\n' ;
}
Topic archived. No new replies allowed.