Hello,
So, I am fairly new to programming and I've been trying to implement some other previously posted solutions to a problem, but so far I can't figure out how to adapt what I've found to my program. The program I have is from a tutorial where the user enters two points on a line, and then the program calculates the mid-point and slope.
I want to modify it from it's initial form so that co-ordinates can be input in (x,y) fashion. Right now the user has to enter the x-coordinate, enter a space, and then enter the y-coordinate (lame...)
I saw people using SStream and using that to either write functions to ignore the comma or similar things for converting one file into an array, but not quite what I am trying here. Any hints anyone could send me? Thanks!!
// program to determine slope of a line
#include <ios>
#include <iostream>
#include <istream>
#include <limits>
usingnamespace std;
void myflush ( istream& in )
{
in.ignore ( numeric_limits<streamsize>::max(), '\n' );
in.clear();
}
void mypause() // have to press ENTER twice for some reason...
{
cout << endl << "Press [Enter] to exit . . .";
cin.get();
}
int main()
{
float x1, x2, y1, y2, mdpt1, mdpt2, slope; // must use float, double, or
longdouble (integer will not work)
cout << "This program will help you calculate the mid-point and
slope of a line." << endl << endl;
cout << "Please input the co-ordinates of the first point " << endl
<< "(x and y with a space in between): " << endl << endl;
cin >> x1 >> y1;
cout << "Thank you, please input the value of the second point: " << endl;
cin >> x2 >> y2;
mdpt1 = (x1 + x2) / 2; //begin calculation
mdpt2 = (y1 + y2) / 2;
slope = (y2 - y1) / (x2 - x1);
cout << "The midpoint of the line is (" << mdpt1 << ", " << mdpt2
<< ")." <<endl << endl; //send to screen
cout << "The slope of the line is " << slope << "." << endl << endl;
cout << "Thanks for using this program." << endl;
myflush ( cin );
mypause();
cin.get(); // still have to press enter twice...
}
You could also modify the inputs stream to treat commas as space (takes a few lines of boilerplate code), or you could use a more serious parser like regex or boost.spirit.
Also, if you're looking for exactly (x,y) format, you could simply read the user input into a complex number. The complex numbers in C++ use exactly that syntax: parenthesis, number, comma, number, parenthesis.
Thank you for your help! I re-worked it as follows for the first set of inputs.
This ignores the parenthesis part because I want the user to enter the coordinates as just integers with a comma in between.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int main()
{
char c1,c2;
double x1, x2, y1, y2, mdpt1, mdpt2, slope;
cout << "This program will help you calculate the mid-point and
slope of a line." << endl << endl;
cout << "Please input the co-ordinates of the first point " << endl <<
"using the format x,y " << endl << endl;
cin >> x1 >> c1 >> y1;
if(! cin || c1 != (','))
{
}
I intend for the input to be without the parenthesis. However, what I am beginning to notice is that if I input something like a symbol, an extra space, or something other than the request sequence of an integer, a comma, and a second integer, the program just goes to the next line.
You could also modify the inputs stream to treat commas as space (takes a few lines of boilerplate code),
or you could use a more serious parser like regex or boost.spirit
Should I pursue these ways of handling all incorrect inputs by the user or are these specific to just dealing with certain characters like the comma?