Cartesian
A.First Edition
This is my first edition of a simple assignment of C++. And I hesitate for some time to decide whether to add it.
There is only one thing worth mentioning: It solves the "cin" problem---suppose you want to cin>>aInteger, and
the naughty user input a character, then you program crashed. I want to solve it for long time. The instructor
showed me the correct way, though still some detail not figured out.
1. void inputCart(CartesianPoint& theCart)
bool doInput(float& number)
It is the only reason that I include this program in my collection---it solves the failed input for cin.
You need to check cin.fail() first and then clear the fail bit. But I don't understand the cin.get()! If it
is not included, the program is crashing like before.
C.Further improvement
1. Are you kidding? For such a trivial program?
กก
/* Assignment #1
=============
Question 1:
----------
Using the structure for a Cartesian point given below, write a function that
receives two Cartesian points and returns the distance between the two points.
struct CartesianPoint {
float x ;
float y ;
float z ;
} ;
Use the function prototype:
float distance( CartesianPoint , CartesianPoint ) ;
Write a driver program to test your function.
*/
#include <iostream>
#include <cmath>
using namespace std;
struct CartesianPoint {
float x ;
float y ;
float z ;
} ;
float distance(CartesianPoint& cart1, CartesianPoint& cart2);
void inputCart(CartesianPoint& theCart);
int main()
{
CartesianPoint cart1, cart2;
char choice;
do
{
//definitely the user must input correct cartesian to go to next
//and this is done by checking each input
cout<<"Now enter first cartesian\n";
inputCart(cart1);
cout<<"Now enter second cartesian\n";
inputCart(cart2);
cout<<"then the distance between two cartesian point is:\n";
cout<<distance(cart1,cart2)<<endl;
//a menu choice for user to choose if continue
cout<<"enter 'q' to quit...";
cin>>choice;
}
while(choice!='q');
return 0;
}
//this does the job of input a cartesian by prompt user to
//input three float, if either one fails, all fail
void inputCart(CartesianPoint& theCart)
{
//a small sub-routine to save repeat code
bool doInput(float& number);
do
{
cout<<"\nnow enter x:";
}
while (!doInput(theCart.x));
do
{
cout<<"\nnow enter y:";
}
while (!doInput(theCart.y));
do
{
cout<<"\nnow enter z:";
}
while (!doInput(theCart.z));
}
//this is a sub-routine from "inputCart" as I don't want to write
//same code three times
bool doInput(float& number)
{
cin>>number;
//be frank with you, before I wrote mail to you, I did try this way,
//but I was cheated by my other small bugs
if (cin.fail())
{
cin.clear();
cout<<"input error! try again!.\n";
cin.get();//this line I do have doubt,
//why do we need this? how can we clear buffer?
return false;
}
else
{
return true;
}
}
//I change the function proto type from passing by value to by reference
//because I think it saves some processing time by not copying structure
float distance(CartesianPoint& cart1, CartesianPoint& cart2)
{
int sumSquare=0;
//we only need to calc square, so order of two cart doesn't matter
sumSquare += (cart1.x - cart2.x)* (cart1.x - cart2.x);
sumSquare += (cart1.y - cart2.y)* (cart1.y - cart2.y);
sumSquare += (cart1.z - cart2.z)* (cart1.z - cart2.z);
return sqrt(sumSquare);
}