how to determine if a input string is valid

so for this program the first steps i have to do which ive never done before is i have to determine if the input is a valid input the requirements are i need spaces between every single character, all uppercase, even amount of parenthesis, and i need to make sure every operator has a operand before and after it.

in my code below im trying to check if the parenthesis are even by every time it reads a ( parenthesis it will add it to a stack and when a ) shows up it will pop off the stack but what is happening is its just changing everything in it to ( parenthesis any help would be appreciated

http://i.imgur.com/mAsA6ik.png

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
#include <iostream>
#include <string>
#include <stack>
#include <fstream>
using namespace std;

typedef string::iterator point;

void parse(string data);
bool parenthesis(string data);

int main()
{
	ifstream inFile;
	string data;
	inFile.open("statements.txt");
	if (inFile)
	{
		cout << "input detected. running program..." << endl;
	}
	else if (!inFile)
	{
		cout << "Error: inccorect or no file choosen" << endl;
	}

	if (inFile.is_open())
	{
		while (getline(inFile, data))
		{
			parenthesis(data);
			//parse(data);
			//cout << data << '\n';
		}
		cout << endl << "end of data from input file" << endl;
		inFile.close();
	}

	system("pause"); 
	return 0;
	
}
	//only functions in here 

void parse(string data)
{
	cout << data << endl;
}
  
bool parenthesis(string data)
{
	stack<char> stack;
	int rpar = 0;
	int lpar = 0;

	for (int i = 0; i < data.length()-1; i++)
	{
		if (data[i] = '(')
		{
			stack.push(data[i]);
			cout << data[i];
			cout << i << endl;
			cout << data << endl;
			
		}
		else if (data[i] = ')')
		{
			if (stack.empty())
			{
				cout<< "error: input has incorect amount of parentheis" << endl;
				return false;
			}
			else{
				stack.pop();
				cout << "parentehsis are correct" << endl;
			}
			return true;
			
		}
		
	}
	
}
Line 57.
if (data[i] = '(')
= is for assignment
== is to test equality, so change it to this.
Good luck with your delimiting.
@ganado
thanks so much that fixed the big problem but now its just outputing the parenthsis are corrected even tho the input i put in is inncorect i tried it with ( ( A + B ) and ( A + B ) ) and still saying its correct i feel like i have a problem with my returns any idea how i can fix that?
Your parenthesis function returns true or false at the first ')' encountered. It should be returning false when one is encountered and the stack is empty, or when the stack is not empty and you've run out of data.

If no ')' is encountered in data, the function returns nothing.. which should be avoided since it promises to return a value of type bool. One does wonder why you're returning a value though, since it isn't used.
Last edited on
i actualy fixed it with a new much easier method i just thought of thank you guys for your help
Last edited on
i actualy fixed it with a new much easier method i just thought of thank you guys for your help


What did you do to solve your problem? (Just in case others need help in the same area)

Also, did you check that your parentheses contain data?
For example, say you're ( ( ) + ( ) ). It passes your test for parentheses.
Last edited on
void parenthesischeck(string data)
{
int lpar = 0;
int rpar=0;

for (int i = 0; i <= data.size(); i++)
{
if (data[i] == '('){
lpar++;
}
else if (data[i] == ')'){
rpar++;
}
}
if (rpar > lpar)
{
cout << "ERROR: parenthesis are incorrect. program will now exit" << endl;
system("pause");
exit(0);
}
else if (rpar == lpar)
{
cout << "parenthesis are correct" << endl;
}
else if (rpar < lpar){
cout << "ERROR: parenthesis are incorrect. program will now exit" << endl;
system("pause");
exit(0);
}

}

this is what i did as my new solution i just created a counter for each time a right or left parenthesis came up then checked if they were equal at the end
So ")(" is correct?
why not,
loop: if '(' --> start++, if ')' --> end++ ;

if start == end --> ok
easier.
Topic archived. No new replies allowed.