Question

////****what am i doing wrong? im trying to have this program with constructors convert inches to...

////****what am i doing wrong? im trying to have this program with constructors convert inches to feet too I don't know what im doing wrong. (the program is suppose to take to sets of numbers feet and inches and compare them to see if they are equal , greater, less than, or not equal using operator overload

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <iomanip>
#include <cmath>


using namespace std;

//class declaration
class FeetInches {
private:
   int feet;
   int   inches;
   void simplify();

public:
   //constructor
   FeetInches(int f = 0, int i = 0){
       feet = f;
       inches = i;
       simplify();
   }

   //mutator
   void setFeet(int f) {
       feet = f;
   }
   void setInches(int i) {
       inches = i;
   }

   //accessors
   int getFeet() {
       return feet;
   }
   int getInches() {
       return inches;
   }

   bool operator < (FeetInches const &obj) {
       if (feet == obj.feet) {
           return inches < obj.inches;
       }
       return feet < obj.feet;
   }

   bool operator <= (FeetInches const &obj) {
       if (feet == obj.feet) {
           return inches <= obj.inches;
       }
       return feet <= obj.feet;
   }

   bool operator > (FeetInches const &obj) {
       if (feet == obj.feet) {
           return inches > obj.inches;
       }
       return feet > obj.feet;
   }

   bool operator >= (FeetInches const &obj) {
       if (feet == obj.feet) {
           return inches >= obj.inches;
       }
       return feet >= obj.feet;
   }

   bool operator == (FeetInches const &obj) {
       return feet == obj.feet&& inches == obj.inches;
   }

   bool operator != (FeetInches const &obj) {
       return feet != obj.feet || inches != obj.inches;
   }

   void simplify(){
       if (inches >= 12)
       {
           feet += (inches / 12);
           inches = inches % 12;
       }
       else if (inches < 0)
       {
           feet -= ((abs(inches) / 12) + 1);
           inches = 12 - (abs(inches) % 12);
       }
   }
};


int main() {
   int feet, inches;

   FeetInches first, second;
   cout << "Enter a distance in feet and inches: ";
   cin >> feet >> inches;
   first.setFeet(feet);
   first.setInches(inches);
   cout << "Enter another distance in feet and inches: ";
   cin >> feet >> inches;
   second.setFeet(feet);
   second.setInches(inches);

   //if else statements that will call the operator overload
   if (first < second && first != second){
       cout << "First is less than second " << endl;
   }
   else if (first == second) {
       cout << "They are equal " << endl;
   }
   else {
       cout << "First is greater than second " << endl;
   }
   return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The Errors in your code were:

declaration and definition of void simplify() was within the same class.

Either remove the declaration or move the definition outside of class.

I removed the declaration void simplify() on the top.

There was also logical error because the mutator setInches() should also simplify() inches.

I have modified the code a little and marked the changes as Changed (in comments)

**********************modified_code.cpp****************************

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <iomanip>
#include <cmath>


using namespace std;

//class declaration
class FeetInches {
private:
int feet;
int inches;
//Changed:Removed declaration

public:
//constructor
FeetInches(int f = 0, int i = 0){
feet = f;
inches = i;
simplify();
}

//mutator
void setFeet(int f) {
feet = f;
}
void setInches(int i) {
inches = i;
//Changed: used simplify also within this function
simplify();
}

//accessors
int getFeet() {
return feet;
}
int getInches() {
return inches;
}

bool operator < (FeetInches const &obj) {
if (feet == obj.feet) {
return inches < obj.inches;
}
return feet < obj.feet;
}

bool operator <= (FeetInches const &obj) {
if (feet == obj.feet) {
return inches <= obj.inches;
}
return feet <= obj.feet;
}

bool operator > (FeetInches const &obj) {
if (feet == obj.feet) {
return inches > obj.inches;
}
return feet > obj.feet;
}

bool operator >= (FeetInches const &obj) {
if (feet == obj.feet) {
return inches >= obj.inches;
}
return feet >= obj.feet;
}

bool operator == (FeetInches const &obj) {
return feet == obj.feet&& inches == obj.inches;
}

bool operator != (FeetInches const &obj) {
return feet != obj.feet || inches != obj.inches;
}
//Changed:Made definition private
private:
void simplify(){
if (inches >= 12)
{
feet += (inches / 12);
inches = inches % 12;
}
else if (inches < 0)
{
feet -= ((abs(inches) / 12) + 1);
inches = 12 - (abs(inches) % 12);
}
}
};


int main() {
int feet, inches;

FeetInches first, second;
cout << "Enter a distance in feet and inches: ";
cin >> feet >> inches;
first.setFeet(feet);
first.setInches(inches);
cout << "Enter another distance in feet and inches: ";
cin >> feet >> inches;
second.setFeet(feet);
second.setInches(inches);

//if else statements that will call the operator overload
if (first < second && first != second){
cout << "First is less than second " << endl;
}
else if (first == second) {
cout << "They are equal " << endl;
}
else {
cout << "First is greater than second " << endl;
}
return 0;
}


Add a comment
Know the answer?
Add Answer to:
////****what am i doing wrong? im trying to have this program with constructors convert inches to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #include <iostream> #include <string> #include <stdio.h> using namespace std; /** The FeetInches class holds distances measured...

    #include <iostream> #include <string> #include <stdio.h> using namespace std; /** The FeetInches class holds distances measured in feet and inches. */ class FeetInches { private: int feet; // The number of feet int inches; // The number of inches /** The simplify method adjusts the values in feet and inches to conform to a standard measurement. */ void simplify() { if (inches > 11) { feet = feet + (inches / 12); inches = inches % 12; } } /**...

  • Template Exercise (50 pts) Modified FeetInches Specification file (30pts) – FeetInches.h Driver program with function template...

    Template Exercise (50 pts) Modified FeetInches Specification file (30pts) – FeetInches.h Driver program with function template (20 pts) – TempDriver.cpp Template Exercise Write template for two functions called minimum and maximum. Each function should accept two arguments and return the lesser or greater of the two values. Test these templates in a driver program. The template with the following types: int, double, string and FeetInches (which is an object). You will need: The FeetInches class (which was provided in Week...

  • Hello, I am trying to write this program and have received a "Segmentation Fault" error but...

    Hello, I am trying to write this program and have received a "Segmentation Fault" error but cannot seem to figure out where. I haven't been able to find it and have been looking for quite a while. The goal is to basically create a version of Conway's Game of Life. I am able to generate a table if the input values for rows and columns are equal, but if they are not I receive a segmentation fault and cannot continue...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • Task: Tasks to complete: ------------------------------------------------------------------------------------------------------------------------------------------ given code: --------------------...

    Task: Tasks to complete: ------------------------------------------------------------------------------------------------------------------------------------------ given code: ------------------------------------------------------------------------------------------------------------------------------------------ main.cpp #include #include "rectangleType.h" using namespace std; // part e int main() { rectangleType rectangle1(10, 5); rectangleType rectangle2(8, 7); rectangleType rectangle3; rectangleType rectangle4; cout << "rectangle1: " << rectangle1 << endl; cout << "rectangle2: " << rectangle2 << endl; rectangle3 = rectangle1 + rectangle2;    cout << "rectangle3: " << rectangle3 << endl; rectangle4 = rectangle1 * rectangle2;    cout << "rectangle4: " << rectangle4 << endl; if (rectangle1 > rectangle2) cout << "Area...

  • NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------...

    NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------------------------------------------------- Tasks to complete: ---------------------------------------------------------------------------------------------------------------------------------------- given code: ----------------------------------------------------------------------------------------------------------------------------------------------- main.cpp #include <iostream> #include <iomanip> #include "fractionType.h" using namespace std; int main() { fractionType num1(5, 6); fractionType num2; fractionType num3; cout << fixed; cout << showpoint; cout << setprecision(2); cout << "Num1 = " << num1 << endl; cout << "Num2 = " << num2 << endl; cout << "Enter the fraction in the form a / b: "; cin >> num2; cout << endl; cout <<...

  • I have a problem with merging the two linked lists together. In the function AscendMerge, I...

    I have a problem with merging the two linked lists together. In the function AscendMerge, I am trying to compare the values of each node in the two lists and output them into one list in ascended order. Everything works except for the one function. Can someone tell me what im doing wrong, when I run it it skips over values. #include <iostream> #include <cassert> using namespace std; struct nodeType {    int info;    nodeType *link;    nodeType *current;...

  • Overload the + operator as indicated. Sample output for the given program: First vacation: Days: 7,...

    Overload the + operator as indicated. Sample output for the given program: First vacation: Days: 7, People: 3 Second vacation: Days: 12, People: 3 #include <iostream> using namespace std; class FamilyVacation{ public: void SetNumDays(int dayCount); void SetNumPeople(int peopleCount); void Print() const; FamilyVacation operator+(int moreDays); private: int numDays; int numPeople; }; void FamilyVacation::SetNumDays(int dayCount) { numDays = dayCount; return; } void FamilyVacation::SetNumPeople(int peopleCount) { numPeople = peopleCount; return; } // FIXME: Overload + operator so can write newVacation = oldVacation +...

  • This is for a C++ program: I'm almost done with this program, I just need to...

    This is for a C++ program: I'm almost done with this program, I just need to implement a bool function that will ask the user if they want to repeat the read_course function. I can't seem to get it right, the instructions suggest a do.. while loop in the main function. here is my code #include <iostream> #include <cstring> #include <cctype> using namespace std; const int SIZE = 100; void read_name(char first[], char last[]); void read_course(int & crn, char des[],...

  • Requirements I have already build a hpp file for the class architecture, and your job is to imple...

    Requirements I have already build a hpp file for the class architecture, and your job is to implement the specific functions. In order to prevent name conflicts, I have already declared a namespace for payment system. There will be some more details: // username should be a combination of letters and numbers and the length should be in [6,20] // correct: ["Alice1995", "Heart2you", "love2you", "5201314"] // incorrect: ["222@_@222", "12306", "abc12?"] std::string username; // password should be a combination of letters...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT