-6
#include < iostream >
using namespace std;
int main() {
    int choice;
    const double pi = 3.14;
    double area, radius;
    cout << "Welcome to the geometry calculator!\n";
    cout << " 1. Calculate The Area OF A Circle:\n";
    cout << " 2. Calculate The Area Of A Rectangle\n";
    cout << " 3. Calculate The Area Of A Triangle\n";
    cout << " 4. Quit The Application";
    cin >> choice;
    if (choice == 1) {
        area = radius * radius * pi;
        cout << "What is the radius of the circle?";
        cin >> radius;
        cout << "the area of the circle is" << area;
    }
    cin.get();
    return 0;
}

This is a portion of code that i'm currently doing for homework. After compiling the code I'm presented with numerous errors all essentially saying the same thing. I don't understand what i'm doing wrong, anyone have any tips? Why do i have to initialize radius if its being used as an input?

Thanks for the down votes guys. Good to know that that seeking help will only get you ridiculed. I already attempted to look up the solution several times to no avail.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
  • 2
    Ahh those pesky error messages with their useless information. I don't know why we bother with them. Perhaps we should change every message to "You made a boo boo"? That would be much more useful than "uninitialized local variable used"... – John3136 Feb 24 '17 at 00:43
  • Well thanks for the condescending answer. Anyways, I figured out the problem. I was under the Impression that i didn't have to initialize the variable if the user was going to be inputting a number for it but it turns out i was wrong. I don't understand why you would rather make snarky comments rather than providing a simple answer. – Andre Fenton Feb 24 '17 at 01:03
  • 1
    You know, there are some languages that allow you to set up the formula and then put the values in, but C++ isn't one of them. That said, I can't call C++ first come first served, either because of brainfreaks like these: http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points. – user4581301 Feb 24 '17 at 01:16
  • 1
    I remember the old days of "syntax error on line 42" where the error was almost always not on line 42, but up several lines. Every time someone reminisces at me about the good ol' days I ask them what planet they were on. The old days weren't that good. A lot of them just plain sucked. – user4581301 Feb 24 '17 at 01:19

1 Answers1

0

You never assigned a variable to area and radius

You're using the radius variable to calculate the area but radius doesn't have a value; it was never initialized.

Simply typing

double radius = 0.0;

would fix it. It is good practice to set (initialize) your variables to something when you declare them.

Lunatoid
  • 68
  • 9
  • Thanks! Yeah i just realized that after looking looking up other people who had the same problem. I guess it never occurred to me that i should initialize a variable before i even used it. I appreciate that help informative answer. :D – Andre Fenton Feb 24 '17 at 01:06
  • @AndreFenton You can click the checkmark on the left to mark the the question as answered. – Lunatoid Feb 24 '17 at 01:10