473,804 Members | 2,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ project help Generating variables.

Basicly i have to make a program that calculates the distance between x
and y points in 2d space.
the code basicly goes like this
1. User says how many points they have (max of 10)

2. User enters points

3. Using sqrt( (x2-x1)^2 + (y2-y1)^2) ) It calculates the distance
between 2 points

4. It displays the length between the first and last point.
My problem is how do i accept the data. im not sure how to vary the
number of inputs or how to declare the variables. like say the user
wants 6 points how do i let the program know only to ask the user for 6
points. and then how do i do the same calculation for each of those
points.
i tried using a while loop and heres my code so far.

#include <iostream>;
#include <cmath.h>

using namespace std;

double length(double xa,double xb,double ya,double yb)
{
double length=0;

length=sqrt(((x b-xa)*(xb-xa))+((yb-ya)*(yb-ya)));

return (length);

}

int main()

int points=0;
int ans=0;
double length(double,d ouble,double,do uble)
double xa=0;
double xb=0;
double ya=0;
double yb=0;

cout <<"How many points would you like to input (Max 10)?\n\n";
cin >>points;
while (points 1)
{
cout <<"Please enter an x value\n";
cin >>xa;
cout <<"Please enter a y value\n";
cin >>ya;
cout <<"Please enter an x value\n";
cin >>xb;
cout <<"Please enter a y value\n";
cin >>yb;

ans=ans+length( xa,xb,ya,yb)

points=points-1;
}

return (0);

Im using VC++
i know the codes a little crappy but hey thats what help is for right
:)

Thanks in advance to any genius who can sort this mess out.

Nov 27 '06
28 1609

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():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::istre am & in, vect & pt)
{
in >pt.x >pt.y;
return in;
}

std::ostream & operator <<(std::ostre am & 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_po int, 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;
}
}

Nov 28 '06 #21

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;
} // main()
--
Bob R
POVrookie
Nov 28 '06 #22
BobR wrote:
'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::istre am & in, vect & pt)
{
in >pt.x >pt.y;
return in;
}
std::ostream & operator <<(std::ostre am & 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_po int, 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();}
}

Nov 29 '06 #23

kwikius wrote in message
<11************ *********@h54g2 000cwb.googlegr oups.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
about this version? I'm making cool use of exceptions to keep stuff
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():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::istre am & in, vect & pt){
in >pt.x >pt.y;
return in;
}

std::ostream & operator <<(std::ostre am & 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_po int, 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); // <GA million laughs. [1]
}

[1] - plus or ? a gig.
--
Bob R
POVrookie
Nov 29 '06 #24

BobR wrote:
kwikius wrote in message
<11************ *********@h54g2 000cwb.googlegr oups.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
about this version? I'm making cool use of exceptions to keep stuff
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

Nov 29 '06 #25
BobR wrote:
>
kwikius wrote in message
<11************ *********@h54g2 000cwb.googlegr oups.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 about this version? I'm making cool use of exceptions to keep
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
Nov 29 '06 #26

Default User wrote in message <4t************ *@mid.individua l.net>...
>BobR wrote:
>kwikius wrote in message
<11*********** **********@h54g 2000cwb.googleg roups.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 about this version? I'm making cool use of exceptions to keep
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
Nov 30 '06 #27

Default User wrote:
BobR wrote:

kwikius wrote in message
<11************ *********@h54g2 000cwb.googlegr oups.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 about this version? I'm making cool use of exceptions to keep
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

Nov 30 '06 #28
BobR wrote:
>
Default User wrote in message <4t************ *@mid.individua l.net>...
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.or g/wiki/Next_of_Kin_(no vel)>

Brian

Nov 30 '06 #29

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1323
by: Le, Thanh-Nhan | last post by:
Hi, I am a beginer in .NET. Is there an assistant or a free tool as in VB6 for generating a complete project frame in VS .NET. Thanks Nhan
7
1254
by: vladislav.moltchanov | last post by:
I countinue to discover problems for simple tasks. Now I would like to write code, Sub or Function, where some values (more than 1) are calculated and are to be assigned to the variables which names I would like to submit as arguments to this SUB /Function How to do this. -- Vlad. Moltchanov
15
3092
by: MR | last post by:
i need to develop a SOAP client, Since I have never personally done one I would like to make sure that I am going about it correctly. The client is a Windows (probably 2k3) application that communicates over HTTPS SOAP. The remote server is Unix based and implements Axis, which I know nothing about What are the steps I need to create this client? I will be developing in C# and I have the XML schema of the SOAP messages. How do I get...
4
1249
by: solrick51 | last post by:
Dear community, I dont really know if this is the right way to do, otherwise my excuses for that. I'm a Dutch student, and graduating this year on my graphic design study. For my graduating study, I want to do some Python work and i'm lokking for a partner wich can help/ cooperate me in programming python.
12
1378
by: E.D.G. | last post by:
Important Research Project (Related to computer programming) Posted by E.D.G. on August 30, 2007 edgrsprj@ix.netcom.com This report is being posted to a number of Internet Newsgroups to see if there are any experienced computer programmers who would like to provide some assistance with an effort to develop a Perl language computer program. Interested parties can try contacting me by e-mail or by posting a response note to the...
0
9705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10311
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10074
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9138
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7613
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5516
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.