CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Member
Join Date
Nov 2009
Location
UK
Posts
166

## 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<< "X: ";
std::cin>> x1;
std::cout<< "Y: ";
std::cin>> y1;

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)
{

//Pythagorean theorem a^2+b^2=c^2/ which is rx^2+ry^2=distance^2
answer = x * x + y * y;
}

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)

2. Elite Member Power Poster
Join Date
Jan 2006
Location
Singapore
Posts
6,748

## Re: Problems with functions

You should #include <string>.

3. Member
Join Date
Nov 2009
Location
UK
Posts
166

## Re: Problems with functions

Originally Posted by laserlight
You should #include <string>.

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 =)

4. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

## 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.

5. Member
Join Date
Nov 2009
Location
UK
Posts
166

## 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

6. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

## 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.

7. Elite Member Power Poster
Join Date
Aug 2000
Location
New York, NY, USA
Posts
5,656

## 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?

8. Senior Member
Join Date
Jan 2009
Posts
1,689

## Re: Problems with functions

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.

9. Senior Member
Join Date
Aug 2005
Location
San Diego, CA
Posts
1,054

## 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.

10. Elite Member Power Poster
Join Date
Aug 2000
Location
New York, NY, USA
Posts
5,656

## 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.

11. Member
Join Date
Nov 2009
Location
UK
Posts
166

## 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

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
•