whilel loop with multiple conditions

Oct 3, 2013 at 9:15pm
The program is mainly for proper notation of a point. Will loop thru until the proper notation is correct.
Ex (9,0) and then exits out of the program, but not "9 0".
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
#include <iostream>

using namespace std;

void peek(char & temp);
void space(void);
void proper(char L_P, char comma, char R_P, 
			bool & L, bool & C, bool & R);

int main()
{
	double x,y;
	char L_P, comma, R_P;
	bool L, C, R;
	
	while( !L || !C || !R )//fails at the 2 run thru
	{//want to continue as long as one or more are false
	 cout<<"Where is your first point\n";
	 peek(L_P);
	 cin>>x;
	 space();
	 peek(comma);
	 cin>>y;
	 space();
	 peek(R_P);
	 proper(L_P, comma, R_P, L, C, R);
    }	
	return 0;
} 

void peek(char & temp)
{
	if(ispunct(cin.peek()))
	{
		cin>>temp;
	}
	else
	{
		temp=0;
	}
	return;
}

void space(void)
{
	while (isspace(cin.peek()) && cin.peek() != '\n')
	{
		cin.ignore();
	}
	return;
}

void proper(char L_P, char comma, 
			  char R_P, bool & L, bool & C, bool & R)
{
	if(L_P != '(')
	{
		cout<<"Left Parenthesis missing.\n";
		L=false;
	}	
	else
	{
		L=true;
	}
	if(comma != ',')
	{
		cout<<"Comma is missing.\n";
		C=false;
	}
	else
	{
		C=true;
	}
	if(R_P != ')')
	{
		cout<<"Right Parenthesis missing.\n";
		R=false;
	}
	else
	{
		R=true;
	}
	return;
}

the main problem is the while loop. The first loop thru it works fine on the second one though it goes into an infinite loop on the input "(9,0 & (9 0 & (9,0)".
I don't quite understand why it goes into and infinite loop, and how to fix it.
Thank you for your help in advance.
The while loop is the main program.
Last edited on Oct 3, 2013 at 9:36pm
Oct 3, 2013 at 9:25pm
Which while loop? There is more than one presented in this code.
Oct 3, 2013 at 9:27pm
It's the one in the main program the one with while( !L || !C|| !R)
Oct 3, 2013 at 10:06pm
Your code cannot handle two consecutive punctuation marks.

One doesn't need to resort to as complex an input as "(9, 0 & (9 0 &(9,0)" to spoof your code. "((" will suffice.

Also L, C and R have unspecified values when the while loop is entered. Initialize them to meaningful values.
Last edited on Oct 3, 2013 at 10:07pm
Oct 3, 2013 at 10:21pm
Even when i initialize them L, C, and R to false the problem still happens. ill have to write a function to check that a number was actually entered instead of a char, then. Thanks
Last edited on Oct 3, 2013 at 10:22pm
Oct 3, 2013 at 10:25pm
Even when i initialize them L, C, and R to false the problem still happens.

Yes. That issue wasn't causing your problem. It is an issue one should take care of, nevertheless.


ill have to write a function to check the that a number was actually entered instead of a char, then.

It is usually easier to validate input after you extract it.
Last edited on Oct 3, 2013 at 10:25pm
Topic archived. No new replies allowed.