0

I am tasked to write a simple program that reads in a file, and determines if the characters '(' '[' and '{' and "balanced". Meaning, There is a matching closing bracket/brace/parentheses for each opening one.

I am having trouble with the input. We are to use input redirection, not an iostream object, taking advantage of the command line. We utilize a makefile and when I run the executable it tells me 2 errors: not using argc and argv[].

    struct Contents {
    int numP;
    int numS;
    int numC;
};

int main(int argc, const char* argv[]) {
    char word;
    Contents c;
 
    
    while(cin >> word) {
        if(word == '{' || word == '}') {
            c.numC++;
        }
        if(word == '(' || word == ')') {
            c.numP++;
        }
        if(word == '[' || word == ']') {
            c.numS++;
        }
    }
    
    if(c.numC % 2 == 0 || c.numP % 2 == 0 || c.numS % 2 == 0) {
        cout << "Balanced" << endl;
    }
    else {
        cout << "Not Balanced" << endl;
    }
    
    return 0;
}

What am I missing here to make the file redirection input work? When I run this in XCode I just enter a bunch of values and it never ends. I can't seem to do the input right.

Thank you!

Marshal
  • 3
  • 3
  • 1
    Your code compiles fine but it has a logic problem in the while loop, have you figured out how you are going to exit the loop? – kks21199 May 14 '21 at 02:34
  • I am not sure how to end the input if I am reading in from a file. Since I am not allowed to use an iostream object and must use the command line to have the file read by cin, I don't get how to end the input – Marshal May 14 '21 at 02:36
  • can you show how you are reading from the file? You can look at the file endings to break out from the loop. – kks21199 May 14 '21 at 02:44
  • 1
    Using redirection your command line would be `./program < input_file` – David C. Rankin May 14 '21 at 04:59
  • Note that your code will think that `{{{{` is balanced and you may need to rethink your algorithm once you're successfully reading input. And I don't know if you're supposed to consider `({)}` balanced or not, but if that's supposed to count as unbalanced you should keep that test case in mind too. – Nathan Pierson May 14 '21 at 05:41
  • Thanks for the help. I understand how cin works with redirection now. I will have to change around the algorithm to see if something is "balanced" or not – Marshal May 14 '21 at 12:07

1 Answers1

1

You need to break out of the while loop, if you are reading from a file then your code should work as it is. I have just made a small change to the logic when checking whether it is balanced or not.

   int main() {
       char word;
       Contents c;

       while (cin >> word) {    
           if (word == '{' || word == '}') {
               c.numC++;
           }
           if (word == '(' || word == ')') {
               c.numP++;
           }
           if (word == '[' || word == ']') {
               c.numS++;
           }
       }
       // Edited here: Check using and instead of or.
       if (c.numC % 2 == 0 && c.numP % 2 == 0 && c.numS % 2 == 0) {
           cout << "Balanced" << endl;
       } else {
           cout << "Not Balanced" << endl;
       }

       return 0;
   }

Update:

I have tested the code, so here is the output.

enter image description here

Here is the output. You can redirect the input to any file and it will scan the file until it reaches EOF.

kks21199
  • 935
  • 1
  • 8
  • 23