#dash 2 Syntax error redirection unexpected

111 messages · Page 1 of 1 (latest)

placid nacelle
#
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int T;
    string str;
    vector<char> ops = {'<', '[', ']'};
    cin >> T;
    for (int i = 0; i < T; i++) {
        vector<char> chars;
        int pos = 0;
        string result = "";
        getline(cin, str);
        for (int j = 0; j < str.length(); j++) {
            char cChar = str[j];
            if (chars.size() == 0 && find(ops.begin(), ops.end(), cChar) != ops.end()) {
                continue;
            } else if (cChar == '<') {
                chars.erase(chars.begin()+(pos-1));
                pos--;
            } else if (cChar == '[') {
                pos = 0;
            } else if (cChar == ']') {
                pos = chars.size();
            }
            chars.insert(chars.begin()+pos, cChar);
            pos++;
        }
    }
}

When input
1
<<hate<<<<loves[steva<en ] cs2040c< and also cs2040c

It gives the error: dash 2 Syntax error redirection unexpected

gusty isleBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

placid nacelle
#

I''m not sure why it does this, I've searched online for an answer but to no avail

deft sorrel
#

That's not a c++ error

#

It's an error code returned by your task checker

placid nacelle
#

Oh

#

Wait what

#

Why does that happen

#

I'm honestly not sure which part of the code is the error

#

I've tried multiple compilers

deft sorrel
#

Cause your logic must be wrong somewhere?

placid nacelle
#

Hmm

deft sorrel
#

The error is in the logic, not a matter of compiler, how do you run your code that you get such an error?

#

(btw you are not printing anything, right?)

placid nacelle
#

Im not printing anything

deft sorrel
#

Then what is the program supposed to do?

#

Your program consumes input and does nothing, really

placid nacelle
#

So its supposed to take a string

#

Yeah im not actually done with the program yet

#

But it shouldnt have an error

deft sorrel
#

Again

#

The error is returned by whatever you check your program with

placid nacelle
#

Its supposed to take a string, read through every character, '<' means backspace, '[' means move cursor to the start of the line and ']' means move cursor to the end of the line

#
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int T;
    string str;
    vector<char> ops = {'<', '[', ']'};
    cin >> T;
    for (int i = 0; i < T; i++) {
        vector<char> chars;
        int pos = 0;
        string result = "";
        getline(cin, str);
        for (int j = 0; j < str.length(); j++) {
            char cChar = str[j];
            if (chars.size() == 0 && find(ops.begin(), ops.end(), cChar) != ops.end()) {
                continue;
            } else if (cChar == '<') {
                chars.erase(chars.begin()+(pos-1));
                pos--;
            } else if (cChar == '[') {
                pos = 0;
                continue;
            } else if (cChar == ']') {
                pos = chars.size();
                continue;
            }
            chars.insert(chars.begin()+pos, cChar);
            pos++;
        }
        for (char character : chars) {
            cout << character << " ";
        }
    }   
    
}

like this

#

Still doesnt work though

#

I tried hardcoding the inputs

#

And it kind of worked

#

So it's something to do with the getline and the cin stuff

#

But im not sure what it is

#

All I'm getting now with this code is normal program termination or

dash: 2: cannot open en]: No such file
dash: 2: my: not found
#

And I'm using multiple online compilers to test the code

deft sorrel
#

What dash?? Is that the entire code compiled?

placid nacelle
#

Yes

#

This is what i got

#

Just to make it clear

#

But hardcoding the input works for some reason

deft sorrel
#

Hahahahah

#

Ok so

placid nacelle
#

Yeah

deft sorrel
#

What you are doing is something completely unrelated to c++

#

Let me explain

placid nacelle
#

Oh

deft sorrel
#

You need to pass your text to stdin (standard input) of your program

#

But! Your text that you are passing is not actually passed as input! Instead it is treated as a command

placid nacelle
#

Oh

deft sorrel
#

It takes the first word

#

"my"

#

And tries to execute it as a command

placid nacelle
#

Wtf why tho

deft sorrel
#

It can't find a command/ program named like that

placid nacelle
#

This never happens

deft sorrel
#

It all happens after your c++ program already finished

placid nacelle
#

Ohhh

deft sorrel
#

It's a terminal/console

#

That first runs your c++ program

#

Then tries to interpret the thing you posted as a next command

placid nacelle
#

Oh

#

But i dont get why it runs the whole program first

#

Cus I did

cin >> T
#

Oh it did ask for the number

#

Is it the getline then?

deft sorrel
#

I'm not sure you are even connected to stdin there

#

Try with some hello world, cin number cout the same number

#

Anyway gotta go

#

Good luck!

placid nacelle
#

Aw okay

#

Yeah it works tho

#

Def connected to stdin

deft sorrel
#

Oh wait it's that problem again

#

I really need to make a faq about that

placid nacelle
#

Wat problem

deft sorrel
#

Ok so, without explanation what's going on

#

Add one getline right after cin T

#

Before any test cases

placid nacelle
#

Add a getline?

deft sorrel
#

Yup

#

The problem is, after cin T you still have a lingering new line character there

#

Add a dummy getline first to get rid of that

placid nacelle
#

Oh

deft sorrel
#

getline reads all until the end of the line

#

So after reading int

#

The remaining line is empty

placid nacelle
#

Oh

deft sorrel
#

Your first line read is empty

#

Ok that should be it

placid nacelle
#

Alright lemme try

#

So every input

#

Has like a hidden newline behind it?

#

Ohhhhh it worked

#

Thank u so much

deft sorrel
#

It's not hidden

#

It's literally new lines

#

cin>> some_int will move past spaces and new lines until it finds a number

#

But leaves everything else untouched

placid nacelle
#

Oh

#

Oh damn

deft sorrel
#

Imagine your input being

44 text
Hoho

In this case your getline after cin>> some int would be " text"

#

Notice the space in the beginning too

placid nacelle
#

Yeah

#

Wait then what even was the problem just now?

#

Is it cus i cin'd a number so it ignored the newline and input?

#

And then my getline in the loop only got a newline so the whole program ended and the remaining input was run as a command?

deft sorrel
#

Mhm basically yes

placid nacelle
#

Ohhhhh i understand

#

Thank u so much once again!

placid nacelle
#

!solved

gusty isleBOT
#

Thank you and let us know if you have any more questions!

#

[SOLVED] dash 2 Syntax error redirection unexpected

gusty isleBOT
#

@placid nacelle

This question thread is being automatically marked as solved.