strings help

im trying to write a code using stack container(templated) where my program would have to read string one character at a time, and a algorithm that can compare parenthesis. this is what i need to read

1. A + B - C
2. A * B / (C +10)
3. A * ((B / C) + D + (E - 10)
4. A * (B / C) + D + (E - 10))

I guess my problem is the getline.
Your are trying to write some code. I assume that you are reading stdin? So you could use:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

bool bReading = true;

stack< char > MyLeftParentheses;

stack< char > MyRightParentheses;

char ch = 0;

while( bReading )
    {
    cin >> ch;

    if( cin.fail() )
        {
        bReading = false;

        continue;

        }    /*    if( cin.fail() )    */

    }    /*    while( bReading )    */


to read a string.

Inside your while loop above you could do something like this:

1
2
3
4
5
6
7

    if( '(' == ch )
        MyLeftParentheses.push( ch );

    if( ')' == ch )
        MyRightParentheses.push( ch );


i forgot to mention that im suppose to get those character from a file
OK, look up istream. Everything else will be the same.
Look at
"http://www.cplusplus.com/reference/iostream/istream/get/".
There's an example on how to open a file and then how to get characters one at a time.
Here's some code, that checks if the brackets in each line match. It ignores all characters, except for '(', ')' and '\n'.
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
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

// tests if the brackets in the coming line of the input stream match.
bool doBracketsMatch( istream & is )
{
	int nOpenBrackets = 0;

	while ( is.good() )     // as long as there are characters to read...
	{
		switch ( is.get() ) // get character from input stream
		{
		case '(': ++nOpenBrackets; break; // increase open brackets
		case ')': --nOpenBrackets; break; // decrease open brackets
		case '\n': return nOpenBrackets == 0; // at line end: check if all brackets are closed
		}
		// check, if there are not more closing brackets than opening ones.
		if ( nOpenBrackets < 0 ) 
		{
			// read characters to the end of the line and return.
			while ( is.good() )
				if ( is.get() == '\n' )
					return false;
		}
	}
	return nOpenBrackets == 0;
}

int main(void)
{
	// Get filename from user.
	cout << "Enter the name of an existing text file: ";
	string s;
	cin >> s;

	// open file
	ifstream is( s.c_str() );

	// show result
	for (int line = 1; is.good(); ++line )
	{
		const bool match = doBracketsMatch( is );
		cout << "The brackets in line " << line 
			<< ( match ? " match." : " don't match." )
			<< endl;
	}
}
ralph83 i like the way you design ur code but when i type the name of the file it does not work
Did you type in the whole name of the file, like "C:\temp\test.txt"?


im trying to make the output looking like


string:A + B - C no parenthesis
string: A * B / (C +10) matching parenthesis
string: A * ((B / C) + D + (E - 10) parenthesis dont match. missing right parenthesis
string: A * (B / C) + D + (E - 10)) parenthesis dont match. missing left parenthesis

PS: why did you write the function void as a parameter of the function int main()????
Last edited on
I thought you were supposed to use a stack?

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

#include <string>
#include <stack>
#include <iostream>

using namespace std;


int main( int argc; char** argv )
    {
    bool bReading = true;

    stack< char > MyLeftParentheses;

    stack< char > MyRightParentheses;

    string String= "";

    char ch = 0;

    while( bReading )
        {
        cin >> ch;

        if( cin.fail() )
            {
            bReading = false;

            continue;

            }    /*    if( cin.fail() )    */

        String += ch;

        switch( ch )
            {
            case '(':
                MyLeftParentheses.push( ch );

                break;

            case ')':
                MyRightParentheses.push( ch );

                break;

            case '\n':
            case ';':
                cout << "string: " << String << " ";

                if( 0 == MyLeftParentheses.size() && 0 == MyRightParentheses.size() )
                    cout << "no parenthesis";
                else if( MyLeftParentheses.size() == MyRightParentheses.size() )
                    cout << "matching parenthesis";
                else if( MyLeftParentheses.size() > MyRightParentheses.size() )
                    cout << "parentheses don't match: Missing right parenthesis";
                else
                    cout << "parentheses don't match: Missing left parenthesis";

               cout << endl;

               String = "";

               while( ! MyLeftParentheses.empty() )
                   MyLeftParenthesis.pop();

               while( ! MyRightParentheses.empty() )
                   MyRightParentheses.pop();

                break;

            default:
                break;

            }    /*    switch( ch )    */


        }    /*    while( bReading )    */


    }
        /*    main()    */

 



Last edited on
you right. but i tried to compile ur program it didnt work. you forgot to write a code that reads ch from a file.
As I said, use an istream instead of cin. And it is some code, not "a" code!
oh yaa that's what im trying to figure out how to implemented in your code..

by the way, why did you use "int argc; char** argv" as a parameter of function main???
by the way, why did you use "int argc; char** argv" as a parameter of function main???

They allow you to read arguments passed from the command line. So you could execute a program like this: ./ProgramName Arg1 Arg2 Arg3 ...

It's a good habit to do even if you will never read the arguments, as you will always be able to without having to go back and add it later. Char** argv is the same as char *argv[].
im trying to implement this code

cout<<" enter the text file name: ";
string String= "";
cin >> String;
ifstream is(String.c_str());

in your code but i cant find where to implemented because you did everything inside the function main !!!!

Umm, put it before the big-hairy while-loop ...
tanks guys i did appreciate and learned from you.
Last edited on
Topic archived. No new replies allowed.