I'm new to C++.. and killing my brain to find this 1 error and cannot figure it out!!
I don't understand why square cannot equal true??? or false??
this is my 3rd program for my class to turn in!!
Any help would be appreciated!!
Eddy

//This program allow the user to enter dimensions in length and witdth
//of a rectangle in inches. The program then computes the perimeter in feet,
//the area in square feet and the length of the diagonal in feet.
// the program then determine if the rectangle is a square.

#include <iostream>
#include <cmath>
using namespace std;

//Function takes two integer parameters
//and gets values input from the keyboard
double get_perimeter(int& length, int& width);

//Function that gets the area
double get_area(int& length, int& width);

//Function that get the diagonal
double get_diagonal(int& length, int& width);

//Function determine square
bool determine_square(int& length, int& width);


int main()
{
    int length, width;
    double area, perimeter, diagonal;
    bool square;

//get input from user
    cout << "Enter length: ";
    cin >> length;
    cout << "Enter width: ";
    cin >> width;

    perimeter = get_perimeter (length, width);

    area = get_area (length, width);

    diagonal = get_diagonal (length, width);

    square = determine_square (length, width);


    cout <<"The perimeter is = "<< perimeter <<endl;
    cout <<" The area is = "<< area <<endl;
    cout <<" The diagonal length is = "<< diagonal <<endl;

    if (square)
        cout <<" This rectangle is a square.";
    else
        cout <<" This rectangle is not a square.";

    return 0;
}

double get_perimeter (int& length, int& width)
{
    double get_perimeter;
    get_perimeter = ((2 * length) + (2 * width)) / 12.0;

    return get_perimeter;
}

double get_area (int& length, int& width)
{
    double get_area;
    get_area = (length * width) / 144.0;

    return get_area;
}

double get_diagonal (int& length, int& width)
{
    double get_diagonal;
    get_diagonal = sqrt (length * length + width * width) / 12.0;

    return get_diagonal;
}

bool determine_square (int& length, int& width)
{
    if (length == width)

        square = true;

    else
        square = false;

    return square;

}

Recommended Answers

All 16 Replies

Use code tags.

bool determine_square (int& length, int& width)
{
    if (length == width)
    
        square = true;

    else
        square = false;
    
    return square;

}

That should simply be:

bool determine(int &length, int &width) {
  return length==width;
}

In your function you make a reference to square but the scope of C++ defines that it is out of scope there, ie you can't access it. The variable square is only local within main itself.

Also you might want to consider not creating temp variables every time use a function. You can simply "return sqrt (length * length + width * width) / 12.0;", in the get diagonal function. Same with the area and perimeter functions.

Thanks a Million!! twomers
I finally understand it -- the way you explain it to me!!
I really appreciate it !

double get_diagonal (int& length, int& width)
{
	return sqrt((length * length) + (width * width)) / 12.0;
}

I tried executing the program with the corrections mentioned above, but it showed up with this error
Error 1 error C2668: 'sqrt' : ambiguous call to overloaded function c:\users\chasevoid\documents\visual studio 2008\projects\c++\squarecheck\squarecheck\squarecheck.cpp 69

If I exclude the part about get_diagonals, then I get another error.
Error 1 error LNK2019: unresolved external symbol "bool __cdecl determine_square(int &,int &)" (?determine_square@@YA_NAAH0@Z) referenced in function _main SquareCheck.obj
Error 2 fatal error LNK1120: 1 unresolved externals C:\Users\ChaseVoid\Documents\Visual Studio 2008\Projects\C++\SquareCheck\Debug\SquareCheck.exe

Maybe It's just me doing something wrong.

Have you #include-ed <cmath>?

//This program allow the user to enter dimensions in length and witdth
//of a rectangle in inches. The program then computes the perimeter in feet,
//the area in square feet and the length of the diagonal in feet.
// the program then determine if the rectangle is a square.

#include <iostream>
#include <cmath>
using namespace std;

//Function takes two integer parameters
//and gets values input from the keyboard
double get_perimeter(int& length, int& width);

//Function that gets the area
double get_area(int& length, int& width);

//Function that get the diagonal
double get_diagonal(int& length, int& width);

//Function determine square
bool determine_square(int& length, int& width);

	int length, width;
	double area, perimeter, diagonal;
	bool square;

int main()
{
	

	//get input from user
	cout << "Enter length: ";
	cin >> length;
	cout << "Enter width: ";
	cin >> width;

	perimeter = get_perimeter (length, width);

	area = get_area (length, width);

	diagonal = get_diagonal (length, width);

	square = determine_square (length, width);


	cout <<"The perimeter is = "<< perimeter <<endl;
	cout <<" The area is = "<< area <<endl;
	cout <<" The diagonal length is = "<< diagonal <<endl;

	if (square)
	cout <<" This rectangle is a square.";
	else
	cout <<" This rectangle is not a square.";

	return 0;
}

double get_perimeter (int& length, int& width)
{
	return ((2 * length) + (2 * width)) / 12.0;
}

double get_area (int& length, int& width)
{
	return (length * width) / 144.0;
}

double get_diagonal (int& length, int& width)
{
	return sqrt((length * length) + (width * width)) / 12.0;
}

bool determine_square(int &length, int &width) 
{
  return length==width;
}

Yeah I did.

What errors are you getting?

Edit1 to OP: Put in your units :)
Edit2: You might have to cast. I think sqrt takes a double ... Not certain now though.

Member Avatar for iamthwee

bool determine(int &length, int &width) {
return length==width;
}

Care to explain that?

OK.

You know you can do:

if ( something == something_else ) { ... }

If something does indeed equal something_else then the parameters return true through the == operator, right (and if it doesn't it returns false)? Well, returning something==something_else from a function is the exact same thing.

It just looks confusing is all. Once you think about it it makes sense.

Also, ignore the &'s before the value name. That just means that the values are being passed into the function by reference.


To further the above you can use all kinds of operators for returning bools (and more):

int greater_than( int val1, int val2 ) { // returns true if val1>val2
  return val1>val2;
}

int not_equal_to( ... ) { // same parameters as above
  return val1 != val2;
}

// etc etc etc
Member Avatar for iamthwee

Really, if that is indeed true, why would you do that? Pray tell?

Just cause it's easier:

bool eq1( int &v1, int &v2 ) {
  if ( [B][U]v1 == v2[/U][/B] )
    return true;
  else 
    return false;
}
bool eq2( int &v1, int &v2 ) {
  return ([B][U]v1==v2[/U][/B] ? true : false);
}
bool eq3( int &v1, int &v2 ) {
  return [B][U]v1==v2[/U][/B];
}

int main( void ) {
  int num1 = 1, num2 = 2;

  std::cout<< eq1(num1, num2) << "\n";
  std::cout<< eq2(num1, num2) << "\n";
  std::cout<< eq3(num1, num2) << "\n";
  
  return 0;
}

Might also be good for micro optimisations.

Edit: Also what other way is there of doing this which doesn't result in using a comparison operator in the process? Why use more than one when you don't need to? I bolded and underlined examples of such in the above code.

Member Avatar for iamthwee

>Just cause it's easier:
Is it?

>Why use more than one when you don't need to?
I don't believe that is a valid point. Clarity is paramount over everything else.

>Might also be good for micro optimisations.
Please explain.

btw you don't need to void inside your int main.

>> Is it.
I believe so anyway.

>> Clarity is paramount over everything else.
I agree, but I don't find the code I presented to be particularily (or in earnest any way), confusing. Do you? If yes, why? It's (very nearly), the same thing as using operators in if statements, control operations in loops etc. If one can handle them I can't see why one should have problems handleing/understanding that.

But all this said - everyone has their own programming style. What I posted earlier is what I tend to do, iamthewee. If you prefer other ways about it then that's your right.

Just looking back at my first post I said "That should simply be:", while what I should have said is "That could simply be:"

>> Please explain.
The other two code samples use the comparison operator to determine if equality exists between the two parameters and acting on this result they return a value rather than returning the result. The other way just returns what the operator returns. I'm not 'all up' on my efficiencies but maybe some compilers will negate any differences (which will be tiny anyway), but I believe the return operator is better pratise.

>> btw you don't need to void inside your int main.
True. I think it makes it clearer which, as you mentioned, is paramount ;)

BTW, how would you accomplish returning bools? Assuming there is processing being done before the return so the use of a function is legit.

Member Avatar for iamthwee

>I agree, but I don't find the code I presented to be particularily (or in earnest any way), confusing. Do you? If yes, why? It's (very nearly), the same thing as using operators in if statements, control operations in loops etc. If one can handle them I can't see why one should have problems handleing/understanding that.

Indeed, but all the OP needed was another line of code to make their original statement valid. Perhaps, it would have been better to show them that instead of your obfuscated version. Clarity is important, more so when explaining ideas to newcomers.


>I'm not 'all up' on my efficiencies
If you have made a statement, but feel yourself unequipped to defend it, don't make it. What you believe is irrevelant.

>I think it makes it clearer which, as you mentioned, is paramount
Please explain why having a void inside the main makes it clearer?

>BTW, how would you accomplish returning bools?
It's a secret...

>> feel yourself unequipped to defend it
I did defend it:

The other two code samples use the comparison operator to determine if equality exists between the two parameters and acting on this result they return a value rather than returning the result.

Gah!

>> OP needed was another line of code to make their original statement valid.
Function.
Yes. But the OP's underlying understanding of C++ and scope showed that there were other issues. I may be wrong but I assumed (s)he thought that the bool variable was accessable within the function.

>> I finally understand it
That doesn't sound like (s)he was confused.

Do you really think that was obfuscated? Really?

>> Please explain why having a void inside the main makes it clearer?
Are you really just trying to attack everything I say?

>> It's a secret...
ie - the same way I do it.

Member Avatar for iamthwee

I just don't get why to got rid of all their code which just needed an extra line to make it valid.

And I still don't get why having void inside the int main() makes it any clearer?

That's all?

If I have offended you I am sorry. I didn't mean it.

>> That's all?
Is that a question or a statement?

>> I just don't get why to got rid of all their code which just needed an extra line to make it valid.
I don't get why you keep on complaining about things I do, but you do.
Just accept that's how I program and get over it. Not everybody has to conform to your style of programming. That, imthewee, is mine.
Good bye.

>> If I have offended you I am sorry. I didn't mean it.
You frustrated me for sure but to offend me... You'd probably have to compare my coding standards to something dead and ugly.

commented: I'm sorry if I have hurt your feelings. +11
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.