English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Novice c++ guy here, trying to wrap my head around arrays and functions. This is supposed to be a program that takes the center and radii from 2 3d spheres and determines if they collide.

#include
#include
#include
using namespace std;

struct sphere
{
float center[3];
float radius;
};

bool sphereDist (sphere &s1, sphere &s2)
{
return (pow(s2.center[0] - s1.center[0], 2) +
pow(s2.center[1] - s1.center[1], 2) +
pow(s2.center[2] - s1.center[2], 2)) <
(s1.radius + s2.radius);


}



void main ()
{
cout <<"Sphere collision detection program, by D.R."<< endl;
cout <<"Input the centers of sphere one"<< endl;
cin>>s1.center;
cout <<"input the radius of sphere one"<< endl;
cin>>s1.radius;
cout <<"Now input the centers of sphere two"<< endl;
cin>>s2.center;
cout <<"Please input the radius of sphere two"<< endl;
cin>>s2.radius;

if (sphereDist == true)
{
cout <<"The spheres collide." << endl;
}
else
{
cout << "the spheres do not collide." < }

2007-11-29 17:03:23 · 2 answers · asked by dustin_r_85 1 in Computers & Internet Programming & Design

** reposted in appropiate subject**

Naturally, the program doesn't work.

1>c:\documents and settings\dustin\my documents\visual studio 2005\projects\tut2\tut2\tut2.cpp(31) : error C2065: 's1' : undeclared identifier
1>c:\documents and settings\dustin\my documents\visual studio 2005\projects\tut2\tut2\tut2.cpp(31) : error C2228: left of '.center' must have class/struct/union
1> type is ''unknown-type''
1>c:\documents and settings\dustin\my documents\visual studio 2005\projects\tut2\tut2\tut2.cpp(33) : error C2228: left of '.radius' must have class/struct/union
1> type is ''unknown-type''
1>c:\documents and settings\dustin\my documents\visual studio 2005\projects\tut2\tut2\tut2.cpp(35) : error C2065: 's2' : undeclared identifier
1>c:\documents and settings\dustin\my documents\visual studio 2005\projects\tut2\tut2\tut2.cpp(35) : error C2228: left of '.center' must have class/struct/union

.... and on and on, 9 errors, to big to fit here.

2007-11-29 17:06:29 · update #1

2 answers

main should return int, not void
you need to define your spheres s1 and s2 in main
cin >> doesn't know what to do with an array of floats...
you need to call your function in the conditional in main:

#include
#include
#include
using namespace std;

struct sphere
{
float center[3];
float radius;
};

bool sphereDist (sphere &s1, sphere &s2)
{
return (pow(s2.center[0] - s1.center[0], 2) +
pow(s2.center[1] - s1.center[1], 2) +
pow(s2.center[2] - s1.center[2], 2)) <
(s1.radius + s2.radius);


}


//int return type
int main ()
{
sphere s1, s2 ;
cout <<"Sphere collision detection program, by D.R."<< endl;
cout <<"Input the centers of sphere one"<< endl;
cin>>s1.center[0];
cin>>s1.center[1];
cin>>s1.center[2];
cout <<"input the radius of sphere one"<< endl;
cin>>s1.radius;
cout <<"Now input the centers of sphere two"<< endl;
cin>>s2.center[0];
cin>>s2.center[1];
cin>>s2.center[2];
cout <<"Please input the radius of sphere two"<< endl;
cin>>s2.radius;

if (sphereDist( s1, s2 ) == true)
{
cout <<"The spheres collide." << endl;
}
else
{
cout << "the spheres do not collide." < }
}

2007-11-29 17:17:57 · answer #1 · answered by mdigitale 7 · 0 0

when you call a function, you need to tell it what variables are going into it. You have "if (sphereDist == true)". First, if you have an if statement, and the condition is if a bool is true, you can just put "if (shereDist)" or "if (!shereDist)" if the condition were if it is false. But since you are calling a finction, you need to say
"if (shereDist(s1, s2))"
replace the s1 and s2 with the names of the 2 variables that you want to use in your shereDist function.

2007-11-30 01:24:30 · answer #2 · answered by Josh 2 · 0 0

fedest.com, questions and answers