Hello all,
I am writing a program that reads in from a file and depending on the first character read in, it will then read in other data and make calculations. The file has data similar to "C 10 or T 10 20" The letter being the shape and the following fields being the parameters...radius, base, height etc. I am hitting a road block when I compile that states a variable is being used without being initialized and I am not sure how to correct this. Ant thoughts?

//Program to determine the area of various shapes
//from given data in input file

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>

using namespace std;

enum Shapes {ERROR, Triangle, Rectangle, Square, Circle};
istream getInput (istream& input, double base, double height, double width, double radius, double side);
Shapes getShapes (double base, double height, double width, double radius, double side);

int main()
{
	char shape, T, t, R, r, S, s, C, c;
	double Area;
	double base, height, width, radius, side;

	
	ifstream infile ("PJ657_Shapes.txt");

	if (!infile)
	{
		cerr <<"Error: cannot open file\n";
		system ("pause");
		return 1;
	}

		ofstream outfile ("PJ657_output.txt");
			if (!outfile)
				{
					cout <<"Error: cannot open PJ657_output.txt";
					return 2;
				}
		infile >> shape;

		while (infile)
		{
			if (shape == T || shape == t)
			{
				infile >> shape >>base >> height;
					Area = .5 * base * height;
						outfile <<"The given triangle has an area of "<<Area;
			}
			else if (shape == R || shape == r){
				infile >> shape >> height >> width;
					Area = height * width;
						outfile <<"The given rectangle has an area of "<<Area;
			}
			else if (shape == C || shape == c)
			{
				infile >> shape >> radius;
					Area = 3.14*radius*radius;
						outfile <<"The given circle has an area of "<<Area;
			}
			else if (shape == S || shape == s)
			{
				infile >> shape >> side;
					Area = side * side;
						outfile <<"The given square has an area of "<<Area;
			}
			else 
				outfile <<"Invalid data";
		
		}
	system ("pause");
	return 0;
}

I don't know if all the code and algorithms are correct or not yet, cant get past this error.

Ok, so I fixed the problem with the undeclared variable, however my algorithm is not working. Just to clarify, I am to get the first character from the file and depending on that variable I am to get other data....radius if first is C, side if first is S, height and width if R and so on. When I run the program now i get an output of "The given triangle has an area of 10The given rectangle has an area of 0The given circle has an area of 0The given square has an area of 0Invalid dataInvalid dataInvalid dataInvalid dataInvalid data" obviously not what I wanted....we can use a switch statement if necessary, just not sure how to do this. new code is as follows

//Program to determine the area of various shapes
//from given data in input file

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>

using namespace std;

enum Shapes {ERROR, Triangle, Rectangle, Square, Circle};
istream getInput (istream& input, double base, double height, double width, double radius, double side);
Shapes getShapes (double base, double height, double width, double radius, double side);

int main()
{
	char shape, T, t, R, r, S, s, C, c;
	double Area;
	double base, height, width, radius, side;

	
	ifstream infile ("PJ657_Shapes.txt");

	if (!infile)
	{
		cerr <<"Error: cannot open file\n";
		system ("pause");
		return 1;
	}

		ofstream outfile ("PJ657_output.txt");
			if (!outfile)
				{
					cout <<"Error: cannot open PJ657_output.txt";
					return 2;
				}
		infile >> shape;

		while (infile)
		{
			if (shape == 'T' || shape == 't')
			{
				infile >> shape >>base >> height;
					Area = .5 * base * height;
						outfile <<"The given triangle has an area of "<<Area;
			}
			else if (shape == 'R' || shape == 'r')
			{
				infile >> shape >> height >> width;
					Area = height * width;
						outfile <<"The given rectangle has an area of "<<Area;
			}
			else if (shape == 'C' || shape == 'c')
			{
				infile >> shape >> radius;
					Area = 3.14*radius*radius;
						outfile <<"The given circle has an area of "<<Area;
			}
			else if (shape == 'S' || shape == 's')
			{
				infile >> shape >> side;
					Area = side * side;
						outfile <<"The given square has an area of "<<Area;
			}
			else 
				outfile <<"Invalid data";
			
			infile >> shape;
		
		}
	system ("pause");
	return 0;
}

This line (and all similar to this one): if (shape == T || shape == t) Compares the content of the variable shape (which is a char) to the content of the variable T or t (which also should be some char). However, since you never initialized any of your variables T, t, none of us can be sure what actual character you compare against. Now what I guess you really want to do is this: if (shape == 'T' || shape == 't') This line actually checks if the content of your char variable shape equals a T or a t. So try changing that and remove all the declarations of those T and t variables, and I'm sure your program will work a lot better!

Edit: Ah I see you beat me to it :)

Emil Olofsson

Could you let us see some of the contents of PJ657_Shapes.txt. Preferably from the very beginning of the file..

T 42 10
R 10 20
c 10
s 20
D 10 10

This is the entire file

Every time you input shape, display it as a character and and integer. You will see immediately what's wrong.

Things to look at:
1) Let the program tell you what is wrong. Output values to be sure they are correct.
2) Format your code better. There is no reason to indent just to indent:

}
		ofstream outfile ("PJ657_output.txt");  // why indent this?
			if (!outfile)
				{  // why indent this?
					cout <<"Error: cannot open PJ657_output.txt";
					return 2;
				}
		infile >> shape;

		while (infile)
		{
			if (shape == 'T' || shape == 't')
			{
				infile >> shape >>base >> height;
					Area = .5 * base * height; // why indent this?
						outfile <<"The given triangle has an area of "<<Area; // why indent this?
			}

3) Don't use system("PAUSE")

Thanks, I will look into this. FYI, I indent because this is how the instructor wants it...and use system ("pause") during my debugging. It is just easier for me.

Ok, so I have change my code as follows

//Program to determine the area of various shapes
//from given data in input file

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>

using namespace std;

enum Shapes {ERROR, Triangle, Rectangle, Square, Circle};
istream getInput (istream& input, double &base, double &height, double &width, double &radius, double &side);
Shapes getShapes (char shape);
double calculateareas(double d1, double d2, double d3);

int main()
{
	char shape;
	double Area;
	//double base, height, width, radius, side;
	double d1, d2, d3;
	char ashape;
	//char enumshapename;

	
	ifstream infile ("PJ657_Shapes.txt");

	if (!infile)
	{
		cerr <<"Error: cannot open file\n";
		system ("pause");
		return 1;
	}

		ofstream outfile ("PJ657_output.txt");
			if (!outfile)
				{
					cout <<"Error: cannot open PJ657_output.txt";
					return 2;
				}
		infile >> shape;

		while (infile)
		{
			if (shape == 'T' || shape == 't')
			{
				infile >> d1 >> d2 >> d3;
				ashape = getShapes( shape );
				Area =  calculateareas(d1,d2,d3);
			}
			else if (shape == 'C' || shape == 'c')
			{
				infile >> d1 >> d2;
				ashape = getShapes ( shape );
				Area = calculateareas(d1, d2, d3);
			}
			else if (shape == 'S' || shape == 's')
			{
				infile >> d1 >> d2;
				ashape = getShapes ( shape );
				Area = calculateareas(d1, d2, d3);
			}
			else if (shape == 'R' || shape == 'r')
			{
				infile >> d1 >> d2 >> d3;
				ashape = getShapes ( shape );
				Area = calculateareas(d1, d2, d3);
			}

			outfile <<"The area of the given "<<getShapes<<" is "<<Area;
			outfile <<endl;
					
			
		
		}
	system ("pause");
	return 0;
}
Shapes getShapes (char shape)
{
	

	if (shape == 'T' || shape == 't')
		return Triangle;
	else if (shape == 'C' || shape == 'c')
		return Circle;
	else if (shape == 'S' || shape == 's')
		return Square;
	else if (shape == 'R' || shape == 'r')
		return Rectangle;
	else
		return ERROR;
}
double calculateareas(double d1, double d2, double d3)
{
	double Area;
	char ashape;

	if (ashape = Triangle)
	{
		Area = .5 * d1 * d2;
		return Area;
	}
	else if (ashape = Circle)
	{
		Area = 3.14 * d2 * d2;
		return Area;
	}
	else if (ashape = Rectangle)
	{
		Area = d2 * d3;
		return Area;
	}
	else if (ashape = Square)
	{
		Area = d2 * d2;
		return Area;
	}
}

Something is still not right, I only get one line in the results and the shape doesnt return correct, any ideas...when i run it now i get
"The area of the given 001B10A5 is 210"

if (ashape = Triangle)
	{
		Area = .5 * d1 * d2;
		return Area;
	}
	else if (ashape = Circle)
	{
		Area = 3.14 * d2 * d2;
		return Area;
	}
	else if (ashape = Rectangle)
	{
		Area = d2 * d3;
		return Area;
	}
	else if (ashape = Square)

Two equals signs are needed to test for equality. One equals is the assignment operator, so you're setting the values each time, and (if I am not mistaken) the if statement will always return true in that case.

Also you're comparing a char to a variable of type 'Shapes'. Is that a char as well? If not, I don't think it will work.

char ashape;

	if (ashape == Triangle)

Also, about the indenting, what I took it to mean was that you have some unnecessary indenting, although indenting is the right thing to do. Oddly enough, that was one of the hardest things for me to get right, when I started programming.

}
		ofstream outfile ("PJ657_output.txt");  // why indent this?
			if (!outfile)
				{  // why indent this?

Maybe something like this would be better/more readable?

} ofstream outfile ("PJ657_output.txt"); 
		if (!outfile) {
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.