Program refuses to output correct information after <= overload

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 546 Views - Last Post: 07 October 2012 - 11:09 AM Rate Topic: -----

#1 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Program refuses to output correct information after <= overload

Posted 06 October 2012 - 10:42 AM

Header and Ipmlementation.

Enter 4, 1 for the 1st entry, and 3,6 for the 2nd, will display the correct output.

But 4,1 and then 4,0, will say that the 1st entry is less than which is false.

I implemented the <= overload, in lines 55 of header & 48 of implementation.

header
#ifndef FEETINCHES_H 
#define FEETINCHES_H

#include <iostream>
using namespace std;

// The FeetInches class holds distances or measurements 
// expressed in feet and inches. 

class FeetInches;		//Forward Declaration
// Function Prototypes for Overloaded Stream Operators 
//ostream &operator << (ostream &, const FeetInches &); 
//istream &operator << (istream &, FeetInches &); 

// The FeetInches class holds distances or measurements 
// expressed in feet and inches. 
class FeetInches
{
private: 
	int feet;			// To hold a number of feet 
	int inches;		// To hold a number of inches 
	void simplify();	// Defined in FeetInches.cpp 

public: 
	// Constructor: allows the feet and inches members to be set. Default values for these members is 0.
	FeetInches(int f = 0, int i = 0) 
		{	feet = f; 
			inches = i; 
			simplify(); }

	// Mutator functions 
	void setFeet(int f) 
		{	feet = f;	} 

	void setInches(int i) 
		{	inches = i; 
			simplify(); } 

	// Accessor functions 
	int getFeet() const 
		{	return feet;	}

	int getInches() const 
		{	return inches; } 

	// Overloaded operator functions 
	FeetInches operator + (const FeetInches &); // Overloaded + 
	FeetInches operator - (const FeetInches &); // Overloaded -
	FeetInches operator ++ ();				// Prefix ++ 
	FeetInches operator ++ (int);			// Postfix ++ 
	bool operator > (const FeetInches &); // Overloaded> 
	bool operator < (const FeetInches &); // Overloaded < 
	bool operator == (const FeetInches &); // Overloaded == 

	bool operator <= (const FeetInches &); //E
	bool operator != (const FeetInches &);


//************************************************************************************************
//The function has 2 parameters: an ostream reference object and a const FeetInches reference object. 
//ostream parameter will be a reference to the actual ostream object on the left side of rhe << operator. 
//2nd parameter, is a reference to a FeetInches object. Will reference the object on the right side of the
//<< operator.
//************************************************************************************************
	friend ostream &operator << (ostream &strm, const FeetInches &obj);
	friend istream &operator >> (istream &strm, FeetInches &obj);
};
#endif



Implementation
// Implementation file for the Feetlnches class 
#include <cstdlib>			// Needed for abs() 
#include "FeetInches.h" 

//*****************************************************************
// 3 feet 14 inches would be adjusted to 4 feet 2 inches and  
// 5 feet -2 inches would be adjusted to 4 feet 10 inches. 
//*****************************************************************
void FeetInches::simplify()	//simplify function is for normalizing the values held in feet and inches. This
{								//function adjusts any set of values where the inches member is > 12 or < 0
	if (inches >= 12)
	{ 
		feet += (inches / 12); 
		inches = inches % 12; 
	}
	else if (inches < 0) 
	{ 
		feet -= ((abs(inches) / 12) + 1);  //abs() function, requires that cstdlib be included. 
		inches = 12 - (abs(inches) % 12);
	}
}

//****************************************** 
// Overloaded binary + operator.
//****************************************** 
FeetInches FeetInches::operator + (const FeetInches &right) 
{ 
	FeetInches temp;		//this object is a temporary location for holding the results of the addition
 
	temp.inches = inches + right.inches;	//adds inches to right.inches and stores the result to temp.inches
	temp.feet = feet + right.feet;			//adds feet to right. feet and stores the result in temp, feet
	temp.simplify(); 		// adjust the values so they conform to a normal value expressed in feet and inches.
	return temp;			//return the value stored in temp
}

//****************************************** 
// Overloaded binary - operator.
//****************************************** 
FeetInches FeetInches::operator - (const FeetInches &right) 
{ 
	FeetInches temp; 
	temp.inches = inches - right.inches; 
	temp.feet = feet - right.feet; 
	temp.simplify(); 
	return temp;
}

bool FeetInches::operator <= (const FeetInches &r)
{
	bool status;
	if (feet <= r.feet)
		status = true;
	else if (feet == r.feet && inches <= r.inches)
		status = true;
	else
		status = false;
	return status;
}

bool FeetInches::operator != (const FeetInches &r)
{
	if (feet != r.feet)
		return true;
	else
		return false;
}


ostream &operator << (ostream &strm, const FeetInches &obj) 
{ 
	strm << obj.feet << " feet, " << obj.inches << " inches"; 
	return strm; 
}

//************************************************************************************************
//The function returns a reference to an istream object so several of these expressions may be chained together. 
//************************************************************************************************
istream &operator >> (istream &strm, FeetInches &obj) 
{ 
	// Prompt the user for the feet. 
	cout << "Feet:  "; 
	strm >> obj.feet; 
	// Prompt the user for the inches. 
	cout << "Inches: "; 
	strm >> obj.inches; 
	// Normalize the values. 
	obj.simplify(); 
	return strm; 
}






Driver
// This program demonstrates the FeetInches class's overloaded 
// + and - operators. 
#include <iostream> 
#include "FeetInches.h" 
using namespace std;

int main() 
{ 
	//Create 2 FeetInches objects. The default arguments 
	//for the constructor will be used.
	FeetInches first, second; 
	
	//Get a distance for the 1st object 
	cout << "Enter a distance in feet and inches.\n"; 
	cin >> first; 
	
	//Get a distance for the 2nd object
	cout << "Enter another distance in feet and inches.\n"; 
	cin >> second; 

	// Display the values in the objects. 
	cout << "The values you entered are:\n";
	cout << first << " and " << second << endl; 

	if (first <= second)
		cout << "\nFirst values entered were not greater.\n";
	else
		cout << "\nFirst values were indeed greater.\n";

	system("pause");
	return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Program refuses to output correct information after <= overload

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1621
  • View blog
  • Posts: 3,079
  • Joined: 30-May 10

Re: Program refuses to output correct information after <= overload

Posted 06 October 2012 - 10:51 AM

> Enter 4, 1 for the 1st entry, and 3,6 for the 2nd, will display the correct output.
> But 4,1 and then 4,0, will say that the 1st entry is less than which is false.
Do you really type in "4,1" (separated by a comma)

Because there is no allowance for such a thing in your code.

You should add something like this
istream &operator >> (istream &strm, FeetInches &obj) 
{ 
	// Prompt the user for the feet. 
	cout << "Feet:  "; 
	strm >> obj.feet; 
	// Prompt the user for the inches. 
	cout << "Inches: "; 
	strm >> obj.inches; 
	if ( strm.good() ) {
		// Normalize the values. 
		obj.simplify(); 
	} else {
		// do something to make obj invalid
	}
	return strm; 
}


http://www.cplusplus...tream/ios/good/
Was This Post Helpful? 0
  • +
  • -

#3 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: Program refuses to output correct information after <= overload

Posted 06 October 2012 - 10:57 AM

No , i did not enter the comma.

Basically, 4, 1, is, 4 entered for feet and 1 entered for inch, when asked.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3988
  • View blog
  • Posts: 12,301
  • Joined: 25-December 09

Re: Program refuses to output correct information after <= overload

Posted 06 October 2012 - 10:58 AM

Also in your bool FeetInches::operator<= (const FeetInches &r) if the feet are equal you never test the inches.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1621
  • View blog
  • Posts: 3,079
  • Joined: 30-May 10

Re: Program refuses to output correct information after <= overload

Posted 06 October 2012 - 10:59 AM

So run the code in the debugger, print out first and second to make sure the member variables are what you expect them to be, then single-step through the <= implementation.

Learning how to use a debugger is a vital skill - consider this an excellent learning opportunity.
Was This Post Helpful? 1
  • +
  • -

#6 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: Program refuses to output correct information after <= overload

Posted 06 October 2012 - 11:16 AM

View PostSalem_c, on 06 October 2012 - 12:59 PM, said:

So run the code in the debugger, print out first and second to make sure the member variables are what you expect them to be, then single-step through the <= implementation.

Learning how to use a debugger is a vital skill - consider this an excellent learning opportunity.


How do i display the debugger in Microsoft Visual C++ 2010? Nothing is displayed in the call stack though.

This is what the author did to overload < operator
bool FeetInches::operator < (const FeetInches &right) 
{ 
	bool status ; 
	if (feet < right.feet) 
		status = true; 
	else if (feet == right.feet && inches < right.inches) 
		status = true; 
	else 
		status = false; 
	return status; 
} 


I don't see what I did wrong.

Also the driver, displays that i entered the correct value during run time.
From int main
	// Display the values in the objects. 
	cout << "The values you entered are:\n";
	cout << first << " and " << second << endl; 

Was This Post Helpful? 0
  • +
  • -

#7 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1621
  • View blog
  • Posts: 3,079
  • Joined: 30-May 10

Re: Program refuses to output correct information after <= overload

Posted 06 October 2012 - 11:25 AM

How to use the debugger?

this
http://www.google.co...B%2B+2010+debug

leads to (for example)
http://kipirvine.com...o2010/index.htm
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 3988
  • View blog
  • Posts: 12,301
  • Joined: 25-December 09

Re: Program refuses to output correct information after <= overload

Posted 06 October 2012 - 11:56 AM

Quote

I don't see what I did wrong.

Follow the following logic when feet == 4 and right.feet == 4
	if (feet < right.feet) 
		status = true; 
	else if (feet == right.feet && inches < right.inches) 
		status = true; 
	else 
		status = false; 
	return status; 


Remember you are using if/else if clauses so if an expression evaluates to true none of the other statements will be evaluated.

Jim
Was This Post Helpful? 0
  • +
  • -

#9 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: Program refuses to output correct information after <= overload

Posted 06 October 2012 - 12:05 PM

I use Visual C++ version 10 not Visual Studio.

I tried following that tutorial but got stuck at: Open a Visual Studio Project file .

Because I don't see any files ending with a .sln or .asm extention.

Quote

Go to the Examples\ch03\Project_Sample folder in the disk directory that contains the book's sample programs. Ordinarily, the full path will be c:\Irvine\Examples\ch03\Project_Sample.


Author doesn't offer any download link for the sample project neither.
Was This Post Helpful? 0
  • +
  • -

#10 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1305
  • View blog
  • Posts: 4,490
  • Joined: 19-February 09

Re: Program refuses to output correct information after <= overload

Posted 06 October 2012 - 12:50 PM

Hi, you've copied the < operator but the logic is not quite the same.


bool FeetInches::operator <= (const FeetInches &r)
{
	bool status;
	if (feet <= r.feet)
		status = true;
	else if (feet == r.feet && inches <= r.inches)
		status = true;
	else
		status = false;
	return status;
}



If feet and right.feet are 4 the initial if statement will be true, the status will be true no matter the inches.
Was This Post Helpful? 1
  • +
  • -

#11 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: Program refuses to output correct information after <= overload

Posted 06 October 2012 - 02:24 PM

Thanks alot! A nested if did the trick.

This post has been edited by mgrex: 06 October 2012 - 02:28 PM

Was This Post Helpful? 0
  • +
  • -

#12 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1305
  • View blog
  • Posts: 4,490
  • Joined: 19-February 09

Re: Program refuses to output correct information after <= overload

Posted 06 October 2012 - 03:13 PM

Don't think you need another if, this may work :-

04    if (feet <= r.feet)
      // could be
04    if (feet <  r.feet)


Was This Post Helpful? 0
  • +
  • -

#13 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: Program refuses to output correct information after <= overload

Posted 06 October 2012 - 04:02 PM

Unfortunately I have 1 or 2 more obstacles to tackle.
I am to add a copy constructor to the program. The instructions don't say anything about overloading an assignment operator, but I'd still like to learn how.

I have added the copy constructor on line 33 of the header file.

I defined the operator = overload on line 61 as well. Now the problem here though is how do I define it's contents outside of the header file?

I tried the following:
FeetInches FeetInches::& operator = (const FeetInches &r)

An error occurs/displays on the & (expected an identifier) and operator ("must be a member function"), syntaxes.
This is defined in line 48 of the implementation file.

Also, how would I demonstrate, the copy constructor in the driver/int main()?

Spoiler


implementation
// Implementation file for the Feetlnches class 
#include <cstdlib>			// Needed for abs() 
#include "FeetInches.h" 

//*****************************************************************
// 3 feet 14 inches would be adjusted to 4 feet 2 inches and  
// 5 feet -2 inches would be adjusted to 4 feet 10 inches. 
//*****************************************************************
void FeetInches::simplify()	//simplify function is for normalizing the values held in feet and inches. This
{								//function adjusts any set of values where the inches member is > 12 or < 0
	if (inches >= 12)
	{ 
		feet += (inches / 12); 
		inches = inches % 12; 
	}
	else if (inches < 0) 
	{ 
		feet -= ((abs(inches) / 12) + 1);  //abs() function, requires that cstdlib be included. 
		inches = 12 - (abs(inches) % 12);
	}
}

//****************************************** 
// Overloaded binary + operator.
//****************************************** 
FeetInches FeetInches::operator + (const FeetInches &right) 
{ 
	FeetInches temp;		//this object is a temporary location for holding the results of the addition
 
	temp.inches = inches + right.inches;	//adds inches to right.inches and stores the result to temp.inches
	temp.feet = feet + right.feet;			//adds feet to right. feet and stores the result in temp, feet
	temp.simplify(); 		// adjust the values so they conform to a normal value expressed in feet and inches.
	return temp;			//return the value stored in temp
}

//****************************************** 
// Overloaded binary - operator.
//****************************************** 
FeetInches FeetInches::operator - (const FeetInches &right) 
{ 
	FeetInches temp; 
	temp.inches = inches - right.inches; 
	temp.feet = feet - right.feet; 
	temp.simplify(); 
	return temp;
}

FeetInches FeetInches::& operator = (const FeetInches &r)
{

}

bool FeetInches::operator <= (const FeetInches &r)
{
	bool status;
	if (feet <= r.feet)
	{
		if(inches <= r.inches)
			status = true;
		else
			status = false;
	}
	else
		status = false;
	return status;
}

bool FeetInches::operator != (const FeetInches &r)
{
	if (feet != r.feet && inches != r.inches)
		return true;
	else
		return false;
}


ostream &operator << (ostream &strm, const FeetInches &obj) 
{ 
	strm << obj.feet << " feet, " << obj.inches << " inches"; 
	return strm; 
}

//************************************************************************************************
//The function returns a reference to an istream object so several of these expressions may be chained together. 
//************************************************************************************************
istream &operator >> (istream &strm, FeetInches &obj) 
{ 
	// Prompt the user for the feet. 
	cout << "Feet:  "; 
	strm >> obj.feet; 
	// Prompt the user for the inches. 
	cout << "Inches: "; 
	strm >> obj.inches; 

	// Normalize the values. 
	obj.simplify(); 

	return strm; 
}




Driver
// This program demonstrates the FeetInches class's overloaded 
// + and - operators. 
#include <iostream> 
#include "FeetInches.h" 
using namespace std;

int main() 
{ 
	//Create 2 FeetInches objects. The default arguments 
	//for the constructor will be used.
	FeetInches first, second; 
	
	//Get a distance for the 1st object 
	cout << "Enter a distance in feet and inches.\n"; 
	cin >> first; 
	
	//Get a distance for the 2nd object
	cout << "Enter another distance in feet and inches.\n"; 
	cin >> second; 

	// Display the values in the objects. 
	cout << "The values you entered are:\n";
	cout << first << " and " << second << endl; 

	if (first <= second)
		cout << "\nFirst values entered were not greater.\n";
	else
		cout << "\nFirst values were indeed greater.\n";

	if (first != second)
		cout << "\n\nValues entered are not equal.\n";
	else
		cout << "The values are the same.\n";

	system("pause");
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#14 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1305
  • View blog
  • Posts: 4,490
  • Joined: 19-February 09

Re: Program refuses to output correct information after <= overload

Posted 06 October 2012 - 08:21 PM

View Postmgrex, on 07 October 2012 - 02:02 AM, said:

... I tried the following:
FeetInches FeetInches::& operator = (const FeetInches &r)

An error occurs/displays on the & (expected an identifier) and operator ("must be a member function"), syntaxes.
This is defined in line 48 of the implementation file. ...


The error complains about the & (ampersand). You want to return a reference to FeetInches. What would happen if you removed the ampersand.


View Postmgrex, on 07 October 2012 - 02:02 AM, said:

... Also, how would I demonstrate, the copy constructor in the driver/int main()? ...


You could try constructing an object and initializing with another object. Put a cout in the copy constructor to check it is running.


Here if *this is 3 feet 6 and r is 4 feet 4, say. Then *this <= r will be false.

bool FeetInches::operator <= (const FeetInches &r)
{
	bool status;
	if (feet <= r.feet)
	{
		if(inches <= r.inches)
			status = true;
		else
			status = false;
	}
	else
		status = false;
	return status;
}


Was This Post Helpful? 1
  • +
  • -

#15 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: Program refuses to output correct information after <= overload

Posted 07 October 2012 - 10:24 AM

This is what my code looks like, and the output displayed.

Header
Spoiler


Spoiler


int main()

// This program demonstrates the FeetInches class's overloaded 
// + and - operators. 
#include <iostream> 
#include "FeetInches.h" 
using namespace std;

int main() 
{ 
	//Create 2 FeetInches objects. The default arguments 
	//for the constructor will be used.
	FeetInches first, second, trd; 
	
	//Get a distance for the 1st object 
	cout << "Enter a distance in feet and inches.\n"; 
	cin >> first; 
	
	//Get a distance for the 2nd object
	cout << "Enter another distance in feet and inches.\n"; 
	cin >> second; 

	// Display the values in the objects. 
	cout << "The values you entered are:\n";
	cout << first << " and " << second << endl; 

	if (first <= second)
		cout << "\nFirst values entered were not greater.\n";
	else
		cout << "\nFirst values were indeed greater.\n";

	if (first != second)
		cout << "\n\nValues entered are not equal.\n";
	else
		cout << "The values are the same.\n";

	trd = second;

	cout << endl << trd << endl;

	system("pause");
	return 0;
}



Output

Quote

Enter a distance in ft and in.
Feet: 4
Inches: 3
Enter another distance in ft and in.
Feet: 3
Inches: 15
The values you entered are:
4 feet, 3 inches and 4 feet, 3 inches

First values entered were non greater.
The values are the same.
Copy constructor called.
Copy constructed has executed.

4 feet, 3 inches //Shouldn't this have come between the 2 previous statements?
Press any key to continue...


The final output, of 4 feet 3 inches, doesn't seem right to me.

This post has been edited by mgrex: 07 October 2012 - 10:38 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2