Problems with functions
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Problems with functions

  1. #1
    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<< "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 ^^

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,391

    Re: Problems with functions

    You should #include <string>.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Re: Problems with functions

    Quote Originally Posted by laserlight View Post
    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 =)

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,228

    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. #5
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Re: Problems with functions

    Quote Originally Posted by GCDEF View Post
    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. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,228

    Re: Problems with functions

    Quote Originally Posted by Mariusmssj View Post
    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. #7
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,553

    Re: Problems with functions

    Quote Originally Posted by Mariusmssj View Post
    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:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  8. #8
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Problems with functions

    Quote Originally Posted by VladimirF View Post
    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. #9
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: Problems with functions

    Quote Originally Posted by ninja9578 View Post
    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. #10
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,553

    Re: Problems with functions

    Quote Originally Posted by ninja9578 View Post
    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:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  11. #11
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Re: Problems with functions

    Quote Originally Posted by GCDEF View Post
    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



    Quote Originally Posted by kempofighter View Post
    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

    Quote Originally Posted by VladimirF View Post
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center