Keep saying The variable 'width' is being used without being initialized.

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

int main() 
{ 
    float length;
    float width;
    float area; 

    cout << "Enter The Length Of The Rectangle: "; 
    cin >> length; 
    area=length*width; 

    cout <<"The area of the rectangle is : "<< area << endl;

    return 0; 
}

Dani AI

Generated

Short diagnosis: the warning is correct — width is used before it ever gets a value, which yields undefined behavior. 's suggestion to prompt for width is the immediate fix, but it's better to validate input and avoid accidental uninitialized use.

A compact, more robust pattern: read lines and parse them so non-numeric input is rejected and leftover characters don't confuse the next read.

#include <iostream>
#include <string>
#include <sstream>

double readNumber(const std::string &prompt) {
    std::string line;
    double value;
    while (true) {
        std::cout << prompt;
        if (!std::getline(std::cin, line)) return 0.0; // EOF or error
        std::stringstream ss(line);
        if (ss >> value && ss.eof()) return value;    // valid number and nothing extra
        std::cout << "Invalid input; please enter a number.\n";
    }
}

int main() {
    double length = readNumber("Enter length: ");
    double width  = readNumber("Enter width: ");
    std::cout << "Area = " << (length * width) << '\n';
    std::cout << "Press Enter to exit...";
    std::cin.get();
    return 0;
}

Notes and quick tips:

  • : your continue_() idea is fine as a pause helper, but cin >> c skips whitespace and your cout << "Your Area is: " area << endl; line is missing << before area. Name continue_ avoids the keyword continue, which is why the underscore was used.
  • Prefer double for simple calculations, enable compiler warnings (e.g. -Wall -Wextra) to catch uninitialized uses, and avoid system("pause"). Initialize variables only when a meaningful default exists; otherwise explicitly read and validate input.

Recommended Answers

All 3 Replies

The error isn't unclear or ambiguous. width doesn't have a predictable value when you try to use it. You probably wanted another input request to fill it in just like length:

#include <iostream>
#include <cmath>

using namespace std; 

int main() 
{ 
    float length;
    float width;
    float area; 

    cout << "Enter The Length Of The Rectangle: "; 
    cin >> length; 

    cout << "Enter The Width Of The Rectangle: "; 
    cin >> width; 

    area = length * width; 

    cout <<"The area of the rectangle is : "<< area << endl;

    return 0; 
}

Also try this:

#include <iostream>
#include <cmath>

using namespace std;

void continue_()
{
    char c;
    cout << "Press any key to end program: ";
    cin >> c; 
    cout << endl;
}

int main()
{
    float area, length, width;

    cout << "What is your length? ";
    cin >> length;
    cout << endl;

    cout << "What is your width? ";
    cin >> width;
    cout << endl;

    cout << "Area = Length * Width" << endl << endl;

    area = (length * width);

    cout << "Your Area is: " area << endl << endl;

    continue_();
    return 0;
}

The admin posts above everyone else's states that we shouldn't use system("pause"), but that we should make up our own. So practice that also.

@ Zvjezdan Veselinovic

Can you explain the use of continue_() function ?

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.