User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,531 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,890 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1198 | Replies: 16
Reply
Join Date: Oct 2007
Posts: 7
Reputation: eddy518 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
eddy518 eddy518 is offline Offline
Newbie Poster

Help needed! Thanks in advance

  #1  
Oct 8th, 2007
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;

}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2007
Location: Ireland
Posts: 1,761
Reputation: twomers will become famous soon enough twomers will become famous soon enough 
Rep Power: 6
Solved Threads: 34
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Help needed! Thanks in advance

  #2  
Oct 8th, 2007
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.
Last edited by twomers : Oct 8th, 2007 at 2:57 pm.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote  
Join Date: Oct 2007
Posts: 7
Reputation: eddy518 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
eddy518 eddy518 is offline Offline
Newbie Poster

Re: Help needed! Thanks in advance

  #3  
Oct 8th, 2007
Thanks a Million!! twomers
I finally understand it -- the way you explain it to me!!
I really appreciate it !
Reply With Quote  
Join Date: Aug 2007
Location: Bangalore, India
Posts: 101
Reputation: ChaseVoid is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 10
ChaseVoid's Avatar
ChaseVoid ChaseVoid is offline Offline
Junior Poster

Re: Help needed! Thanks in advance

  #4  
Oct 9th, 2007
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.
Reply With Quote  
Join Date: May 2007
Location: Ireland
Posts: 1,761
Reputation: twomers will become famous soon enough twomers will become famous soon enough 
Rep Power: 6
Solved Threads: 34
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Help needed! Thanks in advance

  #5  
Oct 9th, 2007
Have you #include-ed <cmath>?
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote  
Join Date: Aug 2007
Location: Bangalore, India
Posts: 101
Reputation: ChaseVoid is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 10
ChaseVoid's Avatar
ChaseVoid ChaseVoid is offline Offline
Junior Poster

Re: Help needed! Thanks in advance

  #6  
Oct 9th, 2007
//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.
Reply With Quote  
Join Date: May 2007
Location: Ireland
Posts: 1,761
Reputation: twomers will become famous soon enough twomers will become famous soon enough 
Rep Power: 6
Solved Threads: 34
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Help needed! Thanks in advance

  #7  
Oct 9th, 2007
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.
Last edited by twomers : Oct 9th, 2007 at 3:21 pm.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote  
Join Date: Aug 2005
Posts: 4,832
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 324
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Help needed! Thanks in advance

  #8  
Oct 9th, 2007
bool determine(int &length, int &width) {
return length==width;
}

Care to explain that?
... the hat of 'is this a cat in a hat?'
Reply With Quote  
Join Date: May 2007
Location: Ireland
Posts: 1,761
Reputation: twomers will become famous soon enough twomers will become famous soon enough 
Rep Power: 6
Solved Threads: 34
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Help needed! Thanks in advance

  #9  
Oct 9th, 2007
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):

  1. int greater_than( int val1, int val2 ) { // returns true if val1>val2
  2. return val1>val2;
  3. }
  4.  
  5. int not_equal_to( ... ) { // same parameters as above
  6. return val1 != val2;
  7. }
  8.  
  9. // etc etc etc
Last edited by twomers : Oct 9th, 2007 at 3:33 pm.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote  
Join Date: Aug 2005
Posts: 4,832
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 324
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Help needed! Thanks in advance

  #10  
Oct 9th, 2007
Really, if that is indeed true, why would you do that? Pray tell?
Last edited by iamthwee : Oct 9th, 2007 at 3:32 pm.
... the hat of 'is this a cat in a hat?'
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 4:32 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC