Stuck on commas when entering numbers

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// program to determine slope of a line

#include <ios>
#include <iostream>
#include <istream>
#include <limits>

using namespace 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 
			long double (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...
	
}
Last edited on
Do you expect exactly the (x,y) format (parenthesis, number, comma, number, parenthesis) or just x,y (number, comma, numer)?

Either way, you could read the non-digits as char:

1
2
3
4
5
6
7
char c1,c2,c3;
double x1,y1;
cin >> c1 >> x1 >> c2 >> y1 >> c3; // or cin >> x1 >> c1 >> y1;
if(! cin || c1 != '(' || c2 != ',' || c3 != ')')
{
    // handle the input error
}
,

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.

Last edited on
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?
Last edited on
Topic archived. No new replies allowed.