-
December 21st, 2009, 07:23 AM
#1
Problems with functions
well here i have a program with 2 functions 1st one is to send 2 coordinates of the objects and calculate the distance between then, 2nd one is to tell how close they are apart and display a message
i think i have messed up with the types of the functions i used but since i am new at it, i am having hard time understanding where is the problem, could anyone tell me how and why is it there
Code:
#include<iostream>
#include <math.h>
double EUC(double, double);
std::string message(double);
int main()
{
int x1,y1 = 0; //coordinates of 1st object
int x2,y2 = 0; //coordinates of 2nd object
double rx,ry = 0; //final values of coordinates will be stored in these 2 integers
double distance = 0; // answers will be stored is this integer
std::string collision;
std::cout<< "Please enter your 1st object coordinates""\n";
std::cout<< "X: ";
std::cin>> x1;
std::cout<< "Y: ";
std::cin>> y1;
std::cout<< "Please enter your 2nd object coordinates""\n";
std::cout<< "X: ";
std::cin>> x2;
std::cout<< "Y: ";
std::cin>> y2;
rx = x2 - x1; //takes away 1st object coordinates from the 1st one
ry = y2 - y1; //takes away 1st object coordinates from the 1st one
distance = EUC(rx, ry);
std::cout<< "Distance between 1st and 2nd object is: " << distance << "\n"; //displays the output and the value of distance
collision = message(distance);
std::cout<< collision;
}
double EUC(double x, double y)
{
double answer;
//Pythagorean theorem a^2+b^2=c^2/ which is rx^2+ry^2=distance^2
answer = x * x + y * y;
answer = sqrt(answer);
return(answer);
}
std::string message(double close)
{
std::string rtn_msg;
if((close>=0)||(close>=3))
{
rtn_msg = "A collision has accoured";
}
else if ((close>=4)||(close>=6))
{
rtn_msg = "Near miss has accoured";
}
else
{
rtn_msg = "Objects are far apart";
}
return(rtn_msg);
}
rerror: 1 euc_main.cpp(34) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
thanks in advance guy ^^
-
December 21st, 2009, 07:25 AM
#2
Re: Problems with functions
You should #include <string>.
-
December 21st, 2009, 07:35 AM
#3
Re: Problems with functions
Originally Posted by laserlight
You should #include <string>.
OMFG i spat my milk now when i read your reply xD
and it works O_O i spent half an hour working on my functions thinking i done something wrong.
i feel so stupid now, but really thankful for your help =)
-
December 21st, 2009, 08:22 AM
#4
Re: Problems with functions
if((close>=0)||(close>=3))
I don't get it. If close >= 3 it's automatically >= 0.
Any positive non-zero number will be true.
White space and eliminating superfluous parenthesis will make your code much more readable.
-
December 21st, 2009, 02:30 PM
#5
Re: Problems with functions
Originally Posted by GCDEF
if((close>=0)||(close>=3))
I don't get it. If close >= 3 it's automatically >= 0.
Any positive non-zero number will be true.
White space and eliminating superfluous parenthesis will make your code much more readable.
thank you
that has been fixed now xD had some small bugs i had to work on.
also changed || to && xD
-
December 21st, 2009, 02:37 PM
#6
Re: Problems with functions
Originally Posted by Mariusmssj
thank you
that has been fixed now xD had some small bugs i had to work on.
also changed || to && xD
That wouldn't help in that case. If it's greater than three, it's also greater than zero.
-
December 21st, 2009, 04:46 PM
#7
Re: Problems with functions
Originally Posted by Mariusmssj
Code:
int x1,y1 = 0; //coordinates of 1st object
Quick question: what do you think this line does? And why did you do it?
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
December 21st, 2009, 06:10 PM
#8
Re: Problems with functions
Originally Posted by VladimirF
Quick question: what do you think this line does? And why did you do it?
That's proper code isn't it? It creates two ints and initialized y to 0.
-
December 21st, 2009, 06:49 PM
#9
Re: Problems with functions
Originally Posted by ninja9578
That's proper code isn't it? It creates two ints and initialized y to 0.
It doesn't initialize X1 though and none of the cin statements are checked for errors which is a serious program defect. Although the code is syntactically correct and will compile it is severely flawed. Maybe he was just trying to point out a possible run-time error that could be occurring.
-
December 21st, 2009, 09:19 PM
#10
Re: Problems with functions
Originally Posted by ninja9578
That's proper code isn't it? It creates two ints and initialized y to 0.
That is a *valid* code. I am not sure if it is proper - it depende on what the author wanted to do.
But it *looks like* the intention was to init both values with 0; they both are used the same way in the following code.
Anyway, if you choose to init one variable and not another one - I would want an explanation.
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
December 22nd, 2009, 08:34 AM
#11
Re: Problems with functions
Originally Posted by GCDEF
That wouldn't help in that case. If it's greater than three, it's also greater than zero.
yes i know what you mean i have change < and > the right way now
Originally Posted by kempofighter
It doesn't initialize X1 though and none of the cin statements are checked for errors which is a serious program defect. Although the code is syntactically correct and will compile it is severely flawed. Maybe he was just trying to point out a possible run-time error that could be occurring.
wow i never knew that, i will have to change that now, thank you i will try not to make such a mistake again
Originally Posted by VladimirF
That is a *valid* code. I am not sure if it is proper - it depende on what the author wanted to do.
But it *looks like* the intention was to init both values with 0; they both are used the same way in the following code.
Anyway, if you choose to init one variable and not another one - I would want an explanation.
thanks for noticing my mistake i will correct it ^^
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|