Variable being used without initialized

Feb 23, 2011 at 6:44pm
// David1
// Lab 14. This program takes user input and assigns
// appropriate values to the sides of a triangle.

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>

using namespace std;

const string ID = "David - Lab 14";

int main()
{
ofstream fout;

int side1, // sides of triangle
side2,
side3,
Triangle, // Types of triangles
Equilateral, // ^
Isosceles, // ^
Scalene; // ^

string exit;

fout.open("David - Lab 14.out");
fout << ID << endl << endl;

cout << "Running lab 14... " << ID << endl << endl;

cout << "Enter three sides:";
cin >> side1 >> side2 >> side3;

// bool

if (((side1 + side2) > side3) || ((side2 + side3) > side1) || ((side1 + side3) > side2))
{Triangle;
}
else {
false;
}

if ((Triangle = true) && (side1 == side2 == side3))
{Equilateral;
}
else {
false;
}

if ((Triangle = true) && ((side1 == side2) || (side2 == side3) || (side1 == side3)))
{Isosceles;
}
else {
false;
}

if ((Triangle = true) && (side1 != side2) || (side1 != side3) || (side2 != side3))
{Scalene;
}
else {
false;
}

// output

fout << "You entered: " << side1 << " " << side2 << " " << side3 << endl
<< "----------------------" << endl
<< "Type " << "Value" << endl
<< "----------------------" << endl
<< "Triangle" << Triangle << endl
<< "Equilateral" << Equilateral << endl
<< "Isosceles" << Isosceles << endl
<< "Scalene" << Scalene << endl
<< "----------------------" << endl;

cout << "Press any letter and return to exit";
cin >> exit;

return 0;
}


Sorry, I'm kinda new at C++... It keeps saying that "Scalene is being used without being initialized".

Thanks
Last edited on Feb 23, 2011 at 7:01pm
Feb 23, 2011 at 6:56pm
closed account (zb0S216C)
It's quite simple, you need to initialize Scalene before you use it. You should always initialize variables as soon as possible - preferably at the point of definition/deceleration.

If you don't know what initialization is, here's an example:
1
2
3
int Variable;      // Un-initialized.
int Variable( 0 ); // Initialized to zero( calling int's constructor ).
int Variable = 0;  // Initialized to zero. 

Last edited on Feb 23, 2011 at 6:59pm
Feb 23, 2011 at 7:04pm
Why is it only happening for Scalene then? Why not for Triangle, Equilateral, and Isosceles as well? I did not assign values to those..

How would I go about initializing it? The value needs to be interchangeable based on the user input, so if I set it to int Variable = 0, it will be permanently set to 0...
Feb 23, 2011 at 7:07pm
closed account (zb0S216C)
it will be permanently set to 0...

No it won't as long as it's not marked constant( const ). Try initializing all the variables you define to zero and see if that helps.
Feb 23, 2011 at 7:14pm
Oh, Awesome! Thank you so much!
Topic archived. No new replies allowed.