0

I wrote this program and it supposed to test for the correct use of the three grouping symbols "(",")";"[","]"; and "{","}". It is using the array implementation of the stacks and supposed to evaluate if it is good string or a bad string. For example: (a+b), [(a-b)+c] would be good and )a+b( etc. would be bad string. When i run the program i get only one error. I thought i am missing a semi-colon or something, but after looking through the code several time,i can't find it. Maybe i got tunnel vision. Can you please see what the problem here is? This is the error: project1.cpp:41: error: expected initializer before 'while'.

#include <string>
#include <iostream>
#include <stdio.h>

using namespace std;

const int DefaultListSize = 100;
typedef char Elem;

class Astack {
private:
    int size;
    int top;
    Elem *listArray;
public:
    Astack (int sz = DefaultListSize)
    {size = sz; top= 0; listArray = new Elem[sz];}
    ~Astack() {delete [] listArray;}
    void clear() {top=0;}
    bool push(const Elem& item) {
            if (top == size) return false;
    else {listArray[top++] = item; return true;}}
    bool pop(Elem& it) {
    if (top==0) return false;
    else {it = listArray[--top]; return true;}}
    bool topValue(Elem& it) const {
    if (top==0) return false;
    else {it = listArray[top-1]; return true;}}
    bool isEmpty() const {if (top==0) return true;
     else return false;}
     int length() const{return top;}
}; //end of class Astack

Astack s;

const string LEFTGROUP="([{";
const string RIGHTGROUP=")]}";

int main()

while (!EOF) {
  while (!EOL) {
   ch = getc();
   if (ch == LEFTGROUP[0]) {
      s.push(ch);
      }
   if (ch == LEFTGROUP[1] {
      s.push(ch);
      }
   if (ch == LEFTGROUP[2] {
      s.push(ch);
      }
    } //checking for openers

   while (!EOL) {
    ch = getc();
    if (s.top() == LEFTGROUP[0]) {
       if (ch == RIGHTGROUP[0]) {
          s.pop();
          }
         }
    if (s.top() == LEFTGROUP[1]) {
       if (ch == RIGHTGROUP[1]) {
          s.pop();
          }
         }
    if (s.top() == LEFTGROUP[2]) {
       if (ch == RIGHTGROUP[2]) {
          s.pop();
          }
         }
    if (!s.empty()) {
      cout<<"Bad String."<<endl;
    else {
      cout<<"Good String."endl;
     }
    }
   }

 return 0;
9
  • 4
    What have included the headers inside the class? Commented May 3, 2013 at 15:20
  • 2
    wait a minute. there is no while in this code. Commented May 3, 2013 at 15:21
  • oops lemme edit the code Commented May 3, 2013 at 15:23
  • @UmNyobe I'd guess while might be used by some of these obscure macros. Commented May 3, 2013 at 15:25
  • 1
    this is really ironic. A program which purpose is to check for semicolons correctness cannot compile because there is a semicolon missing... Commented May 3, 2013 at 15:28

1 Answer 1

3

You forgot a { at the beginning of int main(). You should also end with }

int main(){
    //your while code
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.