Velocity Reviews > C++ > C++ project help Generating variables.

C++ project help Generating variables.

kwikius
Guest
Posts: n/a

 11-28-2006

Jim Langston wrote:

> 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()(0),y(0){}
vect (double const & x_in, double const & y_in)
(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:stream & operator <<(std: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 pointsyntax 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;
}
}

BobR
Guest
Posts: n/a

 11-28-2006

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 pointsyntax 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 pointsyntax 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;
> } // main()

--
Bob R
POVrookie

kwikius
Guest
Posts: n/a

 11-29-2006
BobR wrote:

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

I see. Sincere Apologies for that. OK, now I got rid of the gotos. How
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()(0),y(0){}
vect (double const & x_in, double const & y_in)
(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:stream & operator <<(std: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 pointsyntax 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();}
}

BobR
Guest
Posts: n/a

 11-29-2006

kwikius wrote in message
<(E-Mail Removed). com>...
>BobR wrote:
>
>> 'goto' gives some C++ programmers 'the bends'! :-}

>
>I see. Sincere Apologies for that. OK, now I got rid of the gotos. How
>moving in this version.

> Better now huh?

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

>regards
>Andy Little
>
>#include <iostream>
>#include <cmath>
>#include <string>
>
>struct vect{ // a 2D vector type
> double x,y;
> vect()(0),y(0){}
> vect (double const & x_in, double const & y_in)
> (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:stream & operator <<(std: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 pointsyntax 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.
--
Bob R
POVrookie

kwikius
Guest
Posts: n/a

 11-29-2006

BobR wrote:
> kwikius wrote in message
> <(E-Mail Removed). com>...
> >BobR wrote:
> >
> >> 'goto' gives some C++ programmers 'the bends'! :-}

> >
> >I see. Sincere Apologies for that. OK, now I got rid of the gotos. How
> >moving in this version.

>
> > Better now huh?

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

Default User
Guest
Posts: n/a

 11-29-2006
BobR wrote:

>
> kwikius wrote in message
> <(E-Mail Removed). com>...
> > BobR wrote:
> >
> >> 'goto' gives some C++ programmers 'the bends'! :-}

> >
> > I see. Sincere Apologies for that. OK, now I got rid of the gotos.
> > stuff moving in this version.

>
> > Better now huh?

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

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

Brian

BobR
Guest
Posts: n/a

 11-30-2006

Default User wrote in message <(E-Mail Removed)>...
>BobR wrote:
>> kwikius wrote in message
>> <(E-Mail Removed). com>...
>> > BobR wrote:
>> >> 'goto' gives some C++ programmers 'the bends'! :-}
>> >
>> > I see. Sincere Apologies for that. OK, now I got rid of the gotos.
>> > stuff moving in this version.

>>
>> > Better now huh?

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

>
>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!! ]

--
Bob R
POVrookie

kwikius
Guest
Posts: n/a

 11-30-2006

Default User wrote:
> BobR wrote:
>
> >
> > kwikius wrote in message
> > <(E-Mail Removed). com>...
> > > BobR wrote:
> > >
> > >> 'goto' gives some C++ programmers 'the bends'! :-}
> > >
> > > I see. Sincere Apologies for that. OK, now I got rid of the gotos.
> > > stuff moving in this version.

> >
> > > Better now huh?

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

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

Default User
Guest
Posts: n/a

 11-30-2006
BobR wrote:

>
> Default User wrote in message <(E-Mail Removed)>...
> > BobR wrote:

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

> >
> > 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!! ]

<http://en.wikipedia.org/wiki/Next_of_Kin_(novel)>

Brian