C++ project help Generating variables.

K

kwikius

Jim said:
Well, your program would work with a little modification, but what about now
you want to know the average of the distances, etc? You are not storing the
points.

Two ways, like I said. A dynamic array, or a std::vector. I believe in
your case a std::vector would be prefered.

Another alternative is a recursive function. (mag_n below). In this
case the function call stack is used to store intermediate reults:

Also makes gratuitous use of the infamous GOTO :)

regards
Andy Little
//---------------------------------

#include <iostream>
#include <cmath>
#include <string>

// a 2D vector type
struct vect{
double x,y;
vect():x(0),y(0){}
vect (double const & x_in, double const & y_in)
:x(x_in),y(y_in){}
};

inline
vect operator -(vect const & lhs, vect const & rhs)
{
return vect(lhs.x-rhs.x,lhs.y - rhs.y);
}

std::istream & operator >>(std::istream & in, vect & pt)
{
in >> pt.x >> pt.y;
return in;
}

std::eek:stream & operator <<(std::eek:stream & out, vect const & pt)
{
out <<'(' << pt.x << ','<< pt.y << ')';
return out;
}

inline
double magnitude(vect const & v)
{
return std::sqrt(v.x * v.x + v.y * v.y);
}

typedef vect point;

double mag_n( point const & p_in, int current_point, int const
num_points)
{
int next_point = current_point + 1;
std::cout << "Enter point number " << next_point << " : ";
point p;
std::cin >> p;
double result = magnitude( p - p_in);
if( next_point < num_points) {
result += mag_n(p,next_point, num_points);
}
return result;
}

int main()
{
// the infamous goto...
start:

std::cout << "Enter num points to input : ";
int num_points = 0;
std::cin >> num_points; std::cout << '\n';

if ((num_points <2) || (num_points > 10)){
std::cout << "Error: must be 2 <= num points <=10\n";
goto start;
}

std::cout << "Enter first point:(syntax x y) : ";
point p;
std::cin >> p;
double result = mag_n(p,1, num_points) ;
std::cout << "path length = " << result <<'\n';

std::cout << "Do another set of points? (y/n) : ";
std::string str;
std::cin >> str;

if ( str.substr(0,1) == "y"){
goto start;
}
}
 
B

BobR

kwikius wrote in message ...
Also makes gratuitous use of the infamous GOTO :)
Andy Little
//---------------------------------

int main(){
start: // the infamous goto...

std::cout << "Enter num points to input : ";
int num_points = 0;
std::cin >> num_points; std::cout << '\n';
if ((num_points <2) || (num_points > 10)){
std::cout << "Error: must be 2 <= num points <=10\n";
goto start;
}
std::cout << "Enter first point:(syntax x y) : ";
point p;
std::cin >> p;
double result = mag_n(p,1, num_points) ;
std::cout << "path length = " << result <<'\n';
std::cout << "Do another set of points? (y/n) : ";
std::string str;
std::cin >> str;
if ( str.substr(0,1) == "y"){
goto start;
}
}

'goto' gives some C++ programmers 'the bends'! :-}


int main(){
// >start: // the infamous goto...

while(true){
std::cout << "Enter num points to input : ";
int num_points( 0 );
std::cin >> num_points;
std::cout<< num_points << std::endl;
if( (num_points < 2) || (num_points > 10) ){
std::cout << "Error: must be 2 <= num points <=10\n";

// > goto start;
continue;
} // if(!range)

std::cout << "Enter first point:(syntax x y) : ";
point p;
std::cin >> p;
double result = mag_n(p,1, num_points) ;
std::cout << "path length = " << result <<'\n';

std::cout << "Do another set of points? (y/n) : ";
std::string str;
std::cin >> str;
std::cout << str <<std::endl;
// > if ( str.substr(0,1) == "y"){ goto start; }
if ( str.substr(0,1) != "y" ){
break;
}
} // while(1)

return 0;
 
K

kwikius

BobR said:
'goto' gives some C++ programmers 'the bends'! :-}

I see. Sincere Apologies for that. OK, now I got rid of the gotos. How
about this version? I'm making cool use of exceptions to keep stuff
moving in this version. Better now huh?

regards
Andy Little

#include <iostream>
#include <cmath>
#include <string>


// a 2D vector type
struct vect{
double x,y;
vect():x(0),y(0){}
vect (double const & x_in, double const & y_in)
:x(x_in),y(y_in){}
};


inline
vect operator -(vect const & lhs, vect const & rhs)
{
return vect(lhs.x-rhs.x,lhs.y - rhs.y);
}


std::istream & operator >>(std::istream & in, vect & pt)
{
in >> pt.x >> pt.y;
return in;
}


std::eek:stream & operator <<(std::eek:stream & out, vect const & pt)
{
out <<'(' << pt.x << ','<< pt.y << ')';
return out;
}


inline
double magnitude(vect const & v)
{
return std::sqrt(v.x * v.x + v.y * v.y);
}


typedef vect point;

double mag_n( point const & p_in, int current_point, int const
num_points)
{
int next_point = current_point + 1;
std::cout << "Enter point number " << next_point << " : ";
point p;
std::cin >> p;
double result = magnitude( p - p_in);
if( next_point < num_points) {
result += mag_n(p,next_point, num_points);
}
return result;
}

// exceptions..
struct good_input{
int n_points;
good_input(int n_points_in):n_points(n_points_in){}
};
struct quit{ int f(){return 0;}};

int main()
{
try{
for(;;){
try{
while(1){
int num_points = 0;
std::cout << "Enter num points to input : ";
std::cin >> num_points; std::cout << '\n';
if ((num_points >=2) && (num_points <= 10)){
throw good_input(num_points);
}
else{
std::cout << "Error: must be 2 <= num points
<=10\n";
}
}
}
catch (good_input & e){

int num_points = e.n_points;
std::cout << "Enter first point:(syntax x y) : ";
point p;
std::cin >> p;
double result = mag_n(p,1, num_points) ;
std::cout << "path length = " << result <<'\n';
std::cout << "Do another set of points? (y/n) : ";
std::string str;
std::cin >> str;
if ( str.substr(0,1) != "y"){
throw quit();
}
}
}
}
catch (quit & e){return e.f();}
}
 
B

BobR

kwikius wrote in message
I see. Sincere Apologies for that. OK, now I got rid of the gotos. How
about this version? I'm making cool use of exceptions to keep stuff
moving in this version.
Better now huh?

regards
Andy Little

#include <iostream>
#include <cmath>
#include <string>

struct vect{ // a 2D vector type
double x,y;
vect():x(0),y(0){}
vect (double const & x_in, double const & y_in)
:x(x_in),y(y_in){}
};

inline vect operator -(vect const & lhs, vect const & rhs){
return vect(lhs.x-rhs.x,lhs.y - rhs.y);
}

std::istream & operator >>(std::istream & in, vect & pt){
in >> pt.x >> pt.y;
return in;
}

std::eek:stream & operator <<(std::eek:stream & out, vect const & pt){
out <<'(' << pt.x << ','<< pt.y << ')';
return out;
}

inline double magnitude(vect const & v){
return std::sqrt(v.x * v.x + v.y * v.y);
}

typedef vect point;

Why a typedef? Simply rename 'vect' to 'point'. You trying to be cute? Ah,
you're 'just pulling my leg'!
double mag_n( point const & p_in, int current_point, int const
num_points){
int next_point = current_point + 1;
std::cout << "Enter point number " << next_point << " : ";
point p;
std::cin >> p;
double result = magnitude( p - p_in);
if( next_point < num_points) {
result += mag_n(p,next_point, num_points);
}
return result;
}

struct good_input{ // exceptions..
int n_points;
good_input(int n_points_in):n_points(n_points_in){}
};
struct quit{ int f(){return 0;}};

int main(){
try{
for(;;){
try{
while(1){
int num_points = 0;
std::cout << "Enter num points to input : ";
std::cin >> num_points; std::cout << '\n';
if ((num_points >=2) && (num_points <= 10)){
throw good_input(num_points);
}
else{
std::cout << "Error: must be 2 <= num points
<=10\n";
}
}
}

**Gross misuse** of exceptions to control normal program flow.
It's fun to experiment, but, I wouldn't show that code to *anyone*!!

What if some newbie student saw this, and turned it in. (S)He would get an
'F'! You should have sprinkled in a few smiley faces ( :-} ) and grins (<G>)
to show you were not serious.

catch (good_input & e){
int num_points = e.n_points;
std::cout << "Enter first point:(syntax x y) : ";
point p;
std::cin >> p;
double result = mag_n(p,1, num_points) ;
std::cout << "path length = " << result <<'\n';
std::cout << "Do another set of points? (y/n) : ";
std::string str;
std::cin >> str;
if ( str.substr(0,1) != "y"){ throw quit(); }
}
}
}
catch (quit & e){return e.f();}
}

LMAOROF!

#include <iostream>
void Painfull(int ouch){
while(true){ for(;;){ std::cout<<" Ha Ha"; Painfull(++ouch);}}
return;
}

int main(){
Painfull(42); // <G> A million laughs. [1]
}

[1] - plus or ? a gig.
 
K

kwikius

BobR said:
kwikius wrote in message



<CHOKE>
First 'the bends', now 'the willies'!!

Oh dear... sorry :-(
Why a typedef? Simply rename 'vect' to 'point'. You trying to be cute? Ah,
you're 'just pulling my leg'!

No way man!
**Gross misuse** of exceptions to control normal program flow.
It's fun to experiment, but, I wouldn't show that code to *anyone*!!

What if some newbie student saw this, and turned it in. (S)He would get an
'F'! You should have sprinkled in a few smiley faces ( :-} ) and grins (<G>)
to show you were not serious.

hmmm... back to the drawing board I guess :-(

.....;-)

regards
Andy Little
 
D

Default User

BobR said:
kwikius wrote in message



<CHOKE>
First 'the bends', now 'the willies'!!

That's what happens when you don't have a Eustace looking out for you.





Brian
 
B

BobR

Default User wrote in message said:
That's what happens when you don't have a Eustace looking out for you.
Brian

DANG! I just checked Fry's, and they are out of them!!

Uhhh..... what's a 'Eustace'?
[ seems I knew that once, but with my half dead single-brain-cell...]
[ ....hope it's nothing like a eunuch!! ]
 
K

kwikius

Default said:
That's what happens when you don't have a Eustace looking out for you.

Yeah... Eustace came by and punched me on the nose and told me to stop
being a smartass!

regards
Andy Little
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top