1

I'm just so frustrated with this issue. My code is ok. I have checked it in different online compilers. Here is the code:

Not only this code, but all code that I'm trying to run in VS Code terminal, it's showing me this:

Unexpected token 'tut7.4' in expression or statement

My file name is tut7.4.cpp. Yesterday I could run all the code in the VS Code cmd terminal. But today it's not running in the cmd terminal in VS Code. It's running in the Powershell terminal in VS Code. I want to run my code in cmd in vs code. Not a single code is running. Almost all the codes are showing the same result. I am not getting the problem.

    #include <iostream>
    using namespace std;
    int main()
    {
    int i, fact = 1, n;
    cout << "Enter the Number: ";
    cin >> n;

    for (i = 1; i <= n; i++)
    {
        fact = (fact * i);
    }
    cout << "Factorial: " << fact;
    return 0;
    }

Error:

"PowerShell 7.1.4
Copyright (c) Microsoft Corporation.

https://aka.ms/powershell
Type 'help' to get help.

PS D:\D DRIVE\programming VS\Code with harry\C++A> cd "d:\D DRIVE\programming VS\Code with harry\C++A\" && g++ tut7.4.cpp -o tut7.4 && "d:\D DRIVE\programming VS\Code with harry\C++A\"tut7.4
ParserError: 
Line
   1 |  …  -o tut7.4 && "d:\D DRIVE\programming VS\Code with harry\C++A\"tut7.4
     |                                                                   ~~~~~~
     | Unexpected token 'tut7.4' in expression or statement.

PS D:\D DRIVE\programming VS\Code with harry\C++A>"
Otter
  • 1,086
  • 7
  • 18

1 Answers1

1

There's a bug in version 1.60.0 of Visual Studio Code that causes it not to respect the configured default shell and use the built-in one instead, which is PowerShell:

  • See this answer.

  • If your default shell is properly configured (cmd.exe in your case), you can work around the bug by manually creating a new shell instance by clicking the "+" icon in the top right corner of the integrated terminal.


As for the problem with your PowerShell command:

  • First and foremost, in order to invoke an executable whose path is given in quoted form, &, the call operator must be used - see this answer for details.

  • Secondly, composing a single string argument from a mix of quoted and unquoted tokens - unfortunately only works if the first token is unquoted; in your case, "d:\D DRIVE\programming VS\Code with harry\C++A\"tut7.4 becomes two arguments, because the first token is quoted, causing a syntax error - see this answer for details.

Therefore, your PowerShell command must be (note how the whole path is quoted and preceded by &):

cd "d:\D DRIVE\programming VS\Code with harry\C++A\" && g++ tut7.4.cpp -o tut7.4 && & "d:\D DRIVE\programming VS\Code with harry\C++A\tut7.4"

However, given that you've already changed to the directory in which the executable is located, you can simplify the invocation to .\tut7.4 (note that the & operator is then not strictly needed).

cd "d:\D DRIVE\programming VS\Code with harry\C++A\" && g++ tut7.4.cpp -o tut7.4 && .\tut7.4
mklement0
  • 382,024
  • 64
  • 607
  • 775