Question: I'm working on an assignment in which you need to print out various basic calculations using classes by reading values from a text file. The
I'm working on an assignment in which you need to print out various basic calculations using classes by reading values from a text file. The text file has these numbers:
2 5 7 3 2 1 4 4 4 The current code that I have (.cpp):
//CLIENT PROGRAM THAT WORKS WITH SQUARES
#include
#include "square_FML.h" // include the file header for squares
using namespace std;
int main() { ifstream inFile; int s1, s2, s3; // declare three int variables square sq1;// declare 4 objects of type square named sq1, sq2, sq3, and sq4 initializing the third one to 10 square sq2; square sq3(10); square sq4;
inFile.open("input21.txt"); if(!inFile) { cout << "Could not open input file!" << endl; system("pause"); return 1; }
// Access members using dot operator cout << "Before assigning values to the squares their sides are:" << endl; cout << "Square 1: " << sq1.getSide() << endl; // Show the initial side of Square 1 cout << "Square 2: " << sq2.getSide() << endl; // Show the initial side of Square 2 cout << "Square 3: " << sq3.getSide() << endl; // Show the initial side of Square 3 cout << endl << "............Starting to process the file............" << endl << endl; inFile >> s1 >> s2 >> s3; // read 3 sides from the file (priming read) while(inFile) { cout << "Storing the values read into the side of squares..." << endl << endl; sq1.setSide(s1); // store s1 in Square 1 sq2.setSide(s2); // store s2 in Square 2 sq3.setSide(s3); // store s3 in Square 3 cout << "Displaying the new side of squares" << endl; cout << "Square 1: " << sq1.getSide() << endl; // Show the new side of Square 1 cout << "Square 2: " << sq2.getSide() << endl; // Show the new side of Square 2 cout << "Square 3: " << sq3.getSide() << endl; // Show the new side of Square 3 cout << endl << "Testing other member functions" << endl << endl; cout << "Area of Square 1: " << sq1.area() << endl; // show the area of Square 1 cout << "Perimeter of Square 2: " << sq2.perimeter() << endl; // show the perimeter of Square 2 if(sq1.equalTo(sq3)) // if Square 1 is equal to Square 3 cout << "Square 1 and Square 3 are equal" << endl; else cout << "Square 1 and Square 3 are not equal" << endl;
cout << "Area of (Square 2 + Square 3): " << sq2.plus(sq3) << endl; // Add the areas of Square 2 and Square 3 cout << "Area of (Square 3 - Square 1): " << sq3.minus(sq1) << endl; // Subtract the area of Square 1 from Square 3
cout << "Creating a square double the size of Square 1" << endl; sq4 = sq1.increaseBy(2); // Double the area of Square 1 and assign it to Square 4 cout << "The side of the new square: " << sq4.getSide() << endl; // Show the side of Square 4
cout << "Creating a square of area equal to average area of Square 2 and Square 3" << endl; sq4 = sq2.avgSquare(sq3); // Get the average area of Square 2 and Square 3 and assign it to Square 4 cout << "The side of the new square: " << sq4.getSide() << endl; // Show the side of Square 4 cout << "The area of the new square: " << sq4.area()<< endl; // Show the area of Square 4
cout << endl << "..........Done with this set of values :-).........." << endl << endl; inFile >> s1 >> s2 >> s3; // read next set of 3 sides } inFile.close(); system("pause"); return 0; }
And this is the current code that contains the classes (.h):
// SPECIFICATION FILE FOR C++ CLASS Square #include
class square { public: void setSide(int s); int getSide(); square(int s); square(); int plus(square sq); int minus(square sq); square increaseBy(int factor); square avgSquare(square sq); bool equalTo(square sq); int area(); int perimeter();
private: int side; };
void square::setSide(int s) { side = s;
}
int square::getSide() {
return side; }
square::square() {
side = 0;
}
square::square(int s) {
side = s; }
int square::plus(square sq) {
return area() + sq.area(); }
int square::minus(square sq) {
return area() - sq.area();
}
square square::increaseBy(int factor) {
square ss(side * factor); return ss;
}
square square::avgSquare(square sq) {
double avgArea = (area() + sq.area()) / 2;
int newside = static_cast
square ss(newside);
return ss;
}
bool square::equalTo(square s) {
if (side == s.side) {
return true;
} else {
return false;
}
}
int square::area() { return side * side;
}
int square::perimeter() { return 4 * side;
}
My current issue is that when it prints (only showing the first two outputs) the 3 2 1 from the text file, the "The side of the new square: 1" should be a 2 and the "The area of the new square: 1" should be a 4. I was wondering what the current problem would be?
I know that doing the using namespace std; isn't ideal but it's to save time right now.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
