Good Afternoon,

I'm reviewing structs, but don't get the concept very well.
Assume the following structure declaration is given

struct Rectangle
{
int length;
int width;
};

write a function that returns a Rectangle structure. The function should store the user's input in the members of the structure before returning.
So far I have this but it doesn't work.

int input(struct Rectangle*rect)
{
    int l,w, area;
    cout<<"Enter a length:";
    cin>>l;
    cout <<"enter a width:";
    cin>>w;
    area = l*w;
    return area;
}

Dani AI

Generated

The original request asked for a function that returns a Rectangle filled from user input, but the snippet in the thread takes a Rectangle* and returns an int, as pointed out. Input and calculation were mixed: calculating area in the input routine hides responsibilities and makes reuse harder. Returning a struct by value is idiomatic in modern C++ and is efficient because compilers optimize copies away.

A safe, clear implementation that returns a Rectangle by value while validating input:

#include <iostream>
#include <limits>

Rectangle readRectangleFromUser() {
    Rectangle r{};
    std::cout << "Enter length: ";
    while (!(std::cin >> r.length) || r.length < 0) {
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cout << "Please enter a non-negative integer for length: ";
    }

    std::cout << "Enter width: ";
    while (!(std::cin >> r.width) || r.width < 0) {
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cout << "Please enter a non-negative integer for width: ";
    }

    return r;
}

Compute the area separately (or make it a const member function) so the value is always correct:

int area(const Rectangle& r) {
    return r.length * r.width;
}

int main() {
    Rectangle rect = readRectangleFromUser();
    std::cout << "Area: " << area(rect) << '\n';
    return 0;
}

Notes and common pitfalls drawn from the thread: ’s first input() returning &rg is invalid because rg is a local stack variable — returning its address yields a dangling pointer and undefined behavior. Avoid storing derived values like area inside the struct unless the code guarantees they are updated every time dimensions change; computing on demand is safer. Also remember that in C++ one can (and generally should) refer to the type as Rectangle without the struct keyword, as mentioned.

Recommended Answers

All 4 Replies

Your code doesn't seem to match your description at all. You said the function should return a Rectangle, but the function in the code actually takes a rectangle (that it never uses) and returns an int.

Your function also calculates the area of the rectangle that the user entered which your description didn't say anything about. The calculation of the area should really happen in its own function.

PS: In C++ you don't need the struct keyword when declaring variables or parameters of a struct type.

commented: Observant, good suggestion to separate calculation and printing +12
struct Rectangle
{
    int lenght, height, area;
}

And input code should be:

Rectangle* input()
{
    Rectangle rg;
    cout << "Length: ";
    cin >> rg.Length;
    cout << "Height: ";
    cin >> rg.Height;
    rg.area = rg.Length * rg.Height;

    return &rg;
}

Or should be:

int input(Rectangle *rg)
{
    cout << "Length: ";
    cin >> rg->length;
    cout << "Height: ";
    cin >> rg->height;

    rg->area = rg->length * rg->height;
}

Your first input function is broken - it returns the address of a local variable.

thanks everyone for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.