•
•
•
•
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
![]() |
•
•
Join Date: Oct 2007
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
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;
}
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;
}
Use code tags.
That should simply be:
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.
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.
•
•
Join Date: Aug 2007
Location: Bangalore, India
Posts: 101
Reputation:
Rep Power: 0
Solved Threads: 10
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.
•
•
Join Date: Aug 2007
Location: Bangalore, India
Posts: 101
Reputation:
Rep Power: 0
Solved Threads: 10
//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.
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.
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):
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):
cpp Syntax (Toggle Plain Text)
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
Last edited by twomers : Oct 9th, 2007 at 3:33 pm.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- drwin (Windows NT / 2000 / XP / 2003)
- Help needed for a project concerning protected mode (Assembly)
- Stopping a thread... (Java)
- Inspiron 4100 won't boot (Troubleshooting Dead Machines)
- Quick, Insertion, and Partition (C++)
- Game Server Dedicated Box (linux) (Networking Hardware Configuration)
- strings as input, substr and len (C++)
- Help required locking down desktop display properties (Windows NT / 2000 / XP / 2003)
- Sorry to bother you, but I'm getting insane! (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: Arrays getting disfigured automatically!
- Next Thread: how to pass string into function



Linear Mode