• 0

Calculate area/perimeter of shapes


Question

Ok... New issue; I shorten up the list to just square and rectangle. I have some errors and I think I put wrong coding into reading the appending data to calculate the data then create new file output with the calculation results. The errors I got was like "no match for 'operator>>' in 'inputFile >> line' " and "expected primary-expression before '==' token." I'm not sure how to correct them or whatsoever. I just followed the teacher's examples.

The ideal of shape.txt should be like:

SQUARE:

12

RECTANGLE:

33

12

The ideal of output.txt should be like:

SQUARE:

Given height: 12.

The area of your square is 144.

The perimeter of your square is 48.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <math.h>

using namespace std;

int main()
{
	//declare variables to use for storing user's input

	int choiceOne;
	char choiceTwo;
	char filename[21];

	double height, width;
	double area, perimeter;

	bool goagain = true;
	char response;

	ofstream inputFile;
	char line[21];

	// function prototypes

	double calcSquarePerimeter(double, double);
	double calcRectangleArea(double, double);
	double calcRectanglePerimeter(double, double);

	// User chosing the menu options

	cout << "\nWelcome to the SHAPE program." << endl << endl;

	while (goagain == true)
	{	
		cout << "\nPick from the following menu options." << endl;
		cout << "\t 1. Add a shape to a file." << endl;
		cout << "\t 2. Calculate area AND perimeter of shapes in a file." << endl;
		cout << "\t Enter 1 or 2: ";
		cin >> choiceOne;

		if (choiceOne == 1)
		{
			cout << "\nYou have chosen to add a shape to a file." << endl;		
			cout << "\n\nPlease enter the name of the file: ";
			cin >> filename;
			ofstream shapes(filename, ios::out | ios::app);
			cout << "\nWhat shape do you wish to enter?" << endl;
			cout << "\t a. Square" << endl;
			cout << "\t b. Rectangle" << endl;
			cout << "\t Choose either a and b: ";
			cin >> choiceTwo;

			switch (choiceTwo)
			{

				case 'a': 
						  cout << "\n\nYou want to enter a square." << endl;
						  cout << "Enter the height of the square: ";
						  cin >> height;
						  inputFile.open(filename, ios::out | ios::app);
								if (! inputFile)
								cout << "Error opening file.\n";
						  inputFile << "SQUARE:\r\n";
						  inputFile << height << "\r\n";
						  inputFile.close();
						  cout << "\nYou have entered a shape in " << filename << ". \n";
						  break;

				case 'b': cout << "\n\nYou want to enter a rectangle.\n";
						  cout << "Enter the width of the rectangle: ";
						  cin >> width;
						  cout << "Enter the height of the rectangle: ";
						  cin >> height;
						  inputFile.open(filename, ios::out | ios::app);
								if (! inputFile)
								cout << "Error opening file.\n";
						  inputFile << "RECTANGLE:\r\n";
						  inputFile << width << "\r\n";
						  inputFile << height << "\r\n";
						  inputFile.close();
						  cout << "\nYou have entered a shape in " << filename << ". \n";
						  break;

				default:  cout << "You did not enter a or b. \n";
			}
		}
		else if (choiceOne == 2)
		{
			cout << "\nYou have chosen to calculate area and perimeter ";
			cout << "\n\nPlease enter the name of the file: ";
			cin >> filename;
			inputFile.open(filename, ios::out | ios::app);
			if (! inputFile)
				cout << "Error opening file.\n";
			else
			{
				while (inputFile >> line)
				{
					if (strcmp (line, "SQUARE") == 0)
					{
					ofstream shapes("output.txt", ios::out);
					inputFile << "SQUARE:\r\n";
					inputFile << "Height:\r\n";
					height = atof(line);
					area = pow(height,2);
					inputFile << "The area of your square is" << area;
					perimeter = 4*height;
					inputFile << "The perimeter of your square is" << perimeter;
					inputFile.close();
					cout << "\nYour results are in output.txt. \n";
					}
					else if (line, "RECTANGLE") == 0)
					}
					cout >> "blah";
					}
				}
			}
		}
		else
			cout << "You did not enter a valid choice of 1 or 2.\n";
			cout << "\nDo you wish to run the program again? <y/n>: ";
			cin >> response;
			if (response == 'n' || response == 'N')
				goagain = false;

	} //end of while loop

	cout << endl;

	return 0;

}//end of main function

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

You need to be more consistent with your bracketing, i ran through your code and I think I got it to compile but I had to fix some of the brackets, for IF statements I tend to use the format:

if(condition1){
	// Code
}else if(condition2){
	// Code
}else{
	// Code
}

I'm also assuming you're trying to read a line from the file using the line while(inputFile >> line) loop but I'm not sure.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.