I have a switch function in a program that needs a little help. The switch function is as follows

ostream& displayShapes( ostream& outfile, Shapes ashape)
{
	switch( ashape )
	{
		case Triangle: outfile<< "Triangle" ; break;
		case Circle: outfile << "Circle" ; break;
		case Rectangle: outfile << "Rectangle" ; break;
		case Square: outfile << "Square" ; break;
	}
	cout << endl << endl;
	return displayShapes;
	
}

The problem I am having is with the return. I am not sure what to place here, seems everything I tried gives an error. The most current error is
"error C2440: 'return' : cannot convert from 'std::ostream &(__cdecl *)(std::ostream &,Shapes)' to 'std::ostream &'"
any thoughts on what I need to do here?

If all your function does is display (print) the name of the shape, then why return anything? I'd just call it a void function and omit the return statement.

Then when you call it, don't assign it to anything:

aFunction(); //call a void function

//declare function as void
void aFunction(){
    //print stuff
}

//unless you're using recursion, I don't think you want to call your function within itself. But if you do call it, you'll want to include the ()'s

Edited 6 Years Ago by MyrtleTurtle: n/a

It is required by the assignment that I use Switch/case statements

I didn't say you can't use a switch.

//declare function as void
void aFunction(pass your variables here){

    //put your switch in here
	switch( ashape )
	{
		case Triangle: outfile<< "Triangle" ; break;
		case Circle: outfile << "Circle" ; break;
		case Rectangle: outfile << "Rectangle" ; break;
		case Square: outfile << "Square" ; break;
	}
	cout << endl << endl;//probably want outfile instead of cout, don't you?

}

Edited 6 Years Ago by MyrtleTurtle: just a little bit of OCD...haha

Thanks, I misunderstood the first time....now the program compiles without error, however my output is showing the memory address rather than the shape name....any ideas there? Here is my entire code (I know you don't like the indentation, but for now that is what the teacher wants)

//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, Shapes s);
void displayShapes( ostream& outfile, Shapes ashape);

int main()
{
	char shape;
	double Area;
	double d1, d2;
	Shapes ashape;
	

	
	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;
				ashape = getShapes( shape );
				Area =  calculateareas(d1,d2, ashape);
			}
			else if (shape == 'C' || shape == 'c')
			{
				infile >> d1;
				d2 = 0;
				ashape = getShapes ( shape );
				Area = calculateareas(d1,d2, ashape);
			}
			else if (shape == 'S' || shape == 's')
			{
				infile >> d1;
				d2 = 0;
				ashape = getShapes ( shape );
				Area = calculateareas(d1, d2, ashape);
			}
			else if (shape == 'R' || shape == 'r')
			{
				infile >> d1 >> d2;
				ashape = getShapes ( shape );
				Area = calculateareas(d1, d2, ashape);
			}

			outfile <<"The area of the given "<<displayShapes;
			outfile <<" is "<<Area;
			outfile <<endl;

			infile >>shape;
		}
	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, Shapes s)
{
	double Area;
		
	if (s == Triangle)
	{
		Area = .5 * d1 * d2;
		return Area;
	}
	else if (s == Circle)
	{
		Area = 3.14 * d1 * d1;
		return Area;
	}
	else if (s == Rectangle)
	{
		Area = d1 * d2;
		return Area;
	}
	else if (s == Square)
	{
		Area = d1 * d1;
		return Area;
	}
	else
		return 0;
}
void displayShapes( ostream& outfile, Shapes ashape)
{
	switch( ashape )
	{
		case 1: outfile<< "Triangle" ; break;
		case 2: outfile << "Rectangle" ; break;
		case 3: outfile << "Square" ; break;
		case 4: outfile << "Circle" ; break;
	}
	cout << endl << endl;
	
	
}

my output looks like this
The area of the given 001F1294 is 420
The area of the given 001F1294 is 100
The area of the given 001F1294 is 314
The area of the given 001F1294 is 400
The area of the given 001F1294 is 400
The area of the given 001F1294 is 400
The area of the given 001F1294 is 400
The area of the given 001F1294 is 400
The area of the given 001F1294 is 400

input file is as follows
R 42 10
T 10 20
c 10
s 20
D 10 10 (this line is intentional and should return ERROR)

Did you want to call this function here? Can't do that.

outfile <<"The area of the given "<<displayShapes;
outfile <<"The area of the given "; //print stuff
            displayShapes(outfile, ashape);  //call function...cannot call it with outfile
            outfile <<" is "<<Area;  //print more stuff

My output after making those changes:

The area of the given Triangle is 210
The area of the given Rectangle is 200
The area of the given Circle is 314
The area of the given Square is 400
The area of the given Square is 400
The area of the given Square is 400
The area of the given Square is 400
The area of the given Square is 400
The area of the given Square is 400

Edited 6 Years Ago by MyrtleTurtle: n/a

Thanks that took care of that, but any clues why it reads square 4 or five times?

Thanks that took care of that, but any clues why it reads square 4 or five times?

It depends on what you feed. Remember that computer isn't intelligent at all. Yes, it is fast but never intelligent. garbage in-garbage out. So post the content of the file PJ657_output.txt and we'll know why. Also instead of using

if (shape == 'T' || shape == 't')

Just use

#include <cctype>
//........................
if (tolower(shape) ==  't')

I had posted this earlier, however will post again....the output is
The area of the given Rectangleis 420
The area of the given Triangle is 100
The area of the given Circle is 314
The area of the given Square is 400
The area of the given Square is 400
The area of the given Square is 400
The area of the given Square is 400
The area of the given Square is 400
The area of the given Square is 400

not program output..but the content of the file PJ657_output.txt

that is the contents of PJ657_output.txt

He surely meant PJ657_Shapes.txt; it has been posted in the very first post:

input file is as follows
R 42 10
T 10 20
c 10
s 20
D 10 10 (this line is intentional and should return ERROR)

And the answer is that you do not handle the erroneous input. None of the ifs/else ifs gets satisfied, and the code prints the data obtained earlier - one print for each character in the D line.

This article has been dead for over six months. Start a new discussion instead.