How to ignore certain characters?

Hi, so this program is basically a menu, and it asks the user to input an option of either calculating distance between two points, calculating the midpoint between two points, or quitting. My professor wants input to include parenthesizes and commas. So for example, if I choose to do the midpoint formula, instead of inputting 12 42, I input (12, 42).
So basically, I am wondering how can you ignore the parenthesizes and commas. I am pretty sure it has something to do with cin.ignore(), but in this case, I am not sure how to use it.

Thank you.

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>

using namespace std;

int main (void){

char choice;
bool loop = true;
double point1x;
double point1y;
double point2x;
double point2y;
double a;
double b;
double c;
double distance;
double midpointx;
double midpointy;

	cout << "\n\t\tWelcome to the Point Menu Program!!!\n\n";

	while(loop != false){
	cout << "\t1) calculate Distance between two points\n";
	cout << "\t2) calculate Midpoint of two points\n";
	cout << "\t3) Quit\n\n";
	cout << "\tChoice: ";
	cin >> choice;
	cin.ignore(INT_MAX, '\n');
	
	if(choice == '1' || choice == 'D' || choice == 'd'){ 
	cout << "\n\nWhere is your first point? ";
	cin >> point1x;
	cin >> point1y;
	cout << "Where is your second point? ";
	cin >> point2x;
	cin >> point2y;
	
	a = pow(point2x - point1x, 2);
	b = pow(point2y - point1y, 2);
	c = a + b;
	distance = sqrt(c);

	cout << "\n(" << point1x << ", " << point1y << ") is " << distance << " units away from (" << point2x << ", " << point2y << ").\n\n\n";	   
	}
	
	else if(choice == '2' || choice == 'M' || choice == 'm'){
		cout << "\n\nWhere is your first point? ";
		cin >> point1x;
		cin >> point1y;
		cout << "Where is your second point? ";
		cin >> point2x;
		cin >> point2y;
	
	midpointx = (point1x + point2x) / 2;
	midpointy = (point1y + point2y) / 2;
	
		cout << "\nThe midpoint of the line segment from (" << point1x << ", " << point1y << ") to\n";
		cout << "(" << point2x << ", " << point2y << ") is (" << midpointx << ", " << midpointy << ").\n\n\n";
		   
	}
	
	else if(choice == '3' || choice == 'Q' || choice == 'q')
	{
	loop = false;
	cout << "\nThank you for using the PMP!!\n\n";
	cout << "Endeavor to have a transcendental day!\n\n";
	}
	
	else if(choice > '3' || choice < '1')
	{
		cout << "\nI'm sorry, that choice is invalid!\n\n";
		cout << "Please try to read/type more carefully next time...\n\n";
	}
	
	else
	{
		cout << "\nI'm sorry, that choice is invalid!\n\n";
		cout << "Please try to read/type more carefully next time...\n\n";
	}
}
	return 0;
}
Last edited on
You can :
1. Use a cin statement for delimiter.
1
2
char commaDelimiter;
cin >> commaDelimiter; // Will eat a comma character (,) 


2. Use std::getline
1
2
string testString;
std::getline(std::cin, testString, ',');

Hi, I did what you said, and it looks like it is working properly!

I forgot to mention, but there's an extra part in the assignment, and it wants me to make the input format more flexible. So for example, instead of just typing only '(12, 42)', I can input any of the following: '12, 42)', '(12, 42', '(12 42)', '12, 42', '12 42)', '(12 42', or '12 42'.
I am wondering if I am suppose to maybe use an if to tell if there's a character in the input or not?

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>

using namespace std;

int main (void){

char choice;
bool loop = true;
double point1x, point1y;
double point2x, point2y;
double a, b, c;
double distance;
double midpointx, midpointy;
char commaDelimiter1, commaDelimiter2;
char parenthesis1, parenthesis2, parenthesis3, parenthesis4;

		cout << "\n\t\tWelcome to the Point Menu Program!!!\n\n";

	while(loop != false){
		cout << "\t1) calculate Distance between two points\n";
		cout << "\t2) calculate Midpoint of two points\n";
		cout << "\t3) Quit\n\n";
		cout << "\tChoice: ";
		cin >> choice;
		cin.ignore(INT_MAX, '\n');
	
	if(choice == '1' || choice == 'D' || choice == 'd'){ 
		cout << "\n\nWhere is your first point? ";
		cin >> parenthesis1;
		cin >> point1x;
		cin >> commaDelimiter1;
		cin >> point1y;
		cin >> parenthesis2;
		
		cout << "Where is your second point? ";
		cin >> parenthesis3;
		cin >> point2x;
		cin >> commaDelimiter2;
		cin >> point2y;
		cin >> parenthesis4;
	
		a = pow(point2x - point1x, 2);
		b = pow(point2y - point1y, 2);
		c = a + b;
		distance = sqrt(c);

		cout << "\n(" << point1x << ", " << point1y << ") is " << distance << " units away from (" << point2x << ", " << point2y << ").\n\n\n";	   
	}
	
	else if(choice == '2' || choice == 'M' || choice == 'm'){
		cout << "\n\nWhere is your first point? ";
		cin >> parenthesis1;
		cin >> point1x;
		cin >> commaDelimiter1;
		cin >> point1y;
		cin >> parenthesis2;
		cout << "Where is your second point? ";
		cin >> parenthesis3;
		cin >> point2x;
		cin >> commaDelimiter2;
		cin >> point2y;
		cin >> parenthesis4;
		
		midpointx = (point1x + point2x) / 2;
		midpointy = (point1y + point2y) / 2;
	
		cout << "\nThe midpoint of the line segment from (" << point1x << ", " << point1y << ") to\n";
		cout << "(" << point2x << ", " << point2y << ") is (" << midpointx << ", " << midpointy << ").\n\n\n";
		   
	}
	
	else if(choice == '3' || choice == 'Q' || choice == 'q')
	{
		loop = false;
		cout << "\nThank you for using the PMP!!\n\n";
		cout << "Endeavor to have a transcendental day!\n\n";
	}
	
	else if(choice > '3' || choice < '1')
	{
		cout << "\nI'm sorry, that choice is invalid!\n\n";
		cout << "Please try to read/type more carefully next time...\n\n";
	}
	
	else
	{
		cout << "\nI'm sorry, that choice is invalid!\n\n";
		cout << "Please try to read/type more carefully next time...\n\n";
	}
}
	return 0;
}

Last edited on
Topic archived. No new replies allowed.